Example usage for android.content Context getTheme

List of usage examples for android.content Context getTheme

Introduction

In this page you can find the example usage for android.content Context getTheme.

Prototype

@ViewDebug.ExportedProperty(deepExport = true)
public abstract Resources.Theme getTheme();

Source Link

Document

Return the Theme object associated with this Context.

Usage

From source file:android.support.v7.widget.ThemeUtils.java

public static int getDisabledThemeAttrColor(Context context, int attr) {
    final ColorStateList csl = getThemeAttrColorStateList(context, attr);
    if (csl != null && csl.isStateful()) {
        // If the CSL is stateful, we'll assume it has a disabled state and use it
        return csl.getColorForState(DISABLED_STATE_SET, csl.getDefaultColor());
    } else {/*from   w  w  w  . j  a v  a 2s.co m*/
        // Else, we'll generate the color using disabledAlpha from the theme

        final TypedValue tv = getTypedValue();
        // Now retrieve the disabledAlpha value from the theme
        context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, tv, true);
        final float disabledAlpha = tv.getFloat();

        return getThemeAttrColor(context, attr, disabledAlpha);
    }
}

From source file:io.github.hidroh.materialistic.AppUtils.java

public static float getDimension(Context context, @StyleRes int styleResId, @AttrRes int attr) {
    TypedArray a = context.getTheme().obtainStyledAttributes(styleResId, new int[] { attr });
    float size = a.getDimension(0, 0);
    a.recycle();/* w  w w .  jav  a2 s  .c  om*/
    return size;
}

From source file:com.fa.mastodon.fragment.ComposeOptionsFragment.java

private static void setRadioButtonDrawable(Context context, RadioButton button, @DrawableRes int id) {
    ColorStateList list = new ColorStateList(
            new int[][] { new int[] { -android.R.attr.state_checked },
                    new int[] { android.R.attr.state_checked } },
            new int[] { ThemeUtils.getColor(context, R.attr.compose_image_button_tint),
                    ThemeUtils.getColor(context, R.attr.colorAccent) });
    Drawable drawable = VectorDrawableCompat.create(context.getResources(), id, context.getTheme());
    if (drawable == null) {
        return;/*from ww w  . j  a  v a  2s . co  m*/
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        button.setButtonTintList(list);
    } else {
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(drawable, list);
    }
    button.setButtonDrawable(drawable);
}

From source file:com.amaze.carbonfilemanager.fragments.ZipViewer.java

public static int getToolbarHeight(Context context) {
    final TypedArray styledAttributes = context.getTheme()
            .obtainStyledAttributes(new int[] { android.R.attr.actionBarSize });
    int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
    styledAttributes.recycle();/*ww  w .  j  a  va  2 s.  c om*/

    return toolbarHeight;
}

From source file:com.androidinspain.deskclock.Utils.java

/**
 * @return a vector-drawable inflated from the given {@code resId}
 *//*from ww  w.  j  av a  2 s  . c  om*/
public static VectorDrawableCompat getVectorDrawable(Context context, @DrawableRes int resId) {
    return VectorDrawableCompat.create(context.getResources(), resId, context.getTheme());
}

From source file:com.vuze.android.remote.AndroidUtilsUI.java

public static int getStyleColor(Context context, int r_attr_theme_color) {
    TypedValue typedValue = new TypedValue();
    if (context == null) {
        return 0;
    }//from  w w  w. j  a v  a  2s  .com
    Resources.Theme theme = context.getTheme();
    if (!theme.resolveAttribute(r_attr_theme_color, typedValue, true)) {
        Log.e(TAG, "Could not get resolveAttribute " + r_attr_theme_color + " for "
                + AndroidUtils.getCompressedStackTrace());
        return 0;
    }

    try {
        TypedArray arr = context.obtainStyledAttributes(typedValue.data, new int[] { r_attr_theme_color });
        int c = arr.getColor(0, -1);
        //         Log.d(TAG,
        //               "Color for " + r_attr_theme_color + ", type " + typedValue.type +
        //                     ";" + typedValue.coerceToString());// + " from " + arr);
        arr.recycle();
        if (c == -1) {
            if (AndroidUtils.DEBUG) {
                Log.e(TAG, "Could not get obtainStyledAttributes " + r_attr_theme_color + " for "
                        + AndroidUtils.getCompressedStackTrace());
            }

            // Sometimes TypedArray.getColor fails, but using TypedValue.resourceId
            // seems to work fine.  Could be related to Leanback?
            // TypedArray.getColor:
            // - failed | API 22 | DarkTheme  | Android TV
            // - failed | API 22 | DarkTheme  | Nexus 7
            // - Ok     | API 22 | LightTheme | Android TV
            // - Ok     | API 22 | LightTheme | Nexus 7
            // - Ok     | API 19 | LightTheme | GT 3
            // - Ok     | API 19 | DarkTheme  | GT 3
            // - Ok     | API 18 | LightTheme | Smartphone
            // - Ok     | API 18 | DarkTheme  | Smartphone
            // - Ok     | API 17 | DarkTheme  | FireTV
            return ContextCompat.getColor(context, typedValue.resourceId);
        } else {
            return c;
        }
    } catch (Resources.NotFoundException ignore) {
    }

    return typedValue.data;
}

From source file:com.linkbubble.util.Util.java

public static Integer getSystemActionBarHeight(Context context) {
    // Calculate ActionBar height
    try {/*from   w ww  . jav a 2s.c  o  m*/
        TypedValue tv = new TypedValue();
        if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            return TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
        }
    } catch (Exception e) {
    }

    return null;
}

From source file:info.papdt.blacklight.support.Utility.java

public static int getActionBarHeight(Context context) {
    TypedValue v = new TypedValue();

    if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, v, true)) {
        return TypedValue.complexToDimensionPixelSize(v.data, context.getResources().getDisplayMetrics());
    } else {//from  w w  w.j a  v a  2s  . c o m
        return 0;
    }
}

From source file:com.google.samples.apps.iosched.util.UIUtils.java

/**
 * Calculates the Action Bar height in pixels.
 *///from  w  w  w .j  av a 2  s .c  o m
public static int calculateActionBarSize(Context context) {
    if (context == null) {
        return 0;
    }

    Resources.Theme curTheme = context.getTheme();
    if (curTheme == null) {
        return 0;
    }

    TypedArray att = curTheme.obtainStyledAttributes(RES_IDS_ACTION_BAR_SIZE);
    if (att == null) {
        return 0;
    }

    float size = att.getDimension(0, 0);
    att.recycle();
    return (int) size;
}

From source file:com.apptentive.android.sdk.util.Util.java

public static int getThemeColor(Context context, int attr) {
    if (context == null) {
        return 0;
    }//from   w ww  .j av  a2 s  .c  o m
    return getThemeColor(context.getTheme(), attr);
}