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:io.plaidapp.core.util.ColorUtils.java

/**
 * Queries the theme of the given {@code context} for a theme color.
 *
 * @param context            the context holding the current theme.
 * @param attrResId          the theme color attribute to resolve.
 * @param fallbackColorResId a color resource id tto fallback to if the theme color cannot be
 *                           resolved.//from www  .  j av a  2s.  c  o m
 * @return the theme color or the fallback color.
 */
@ColorInt
public static int getThemeColor(@NonNull Context context, @AttrRes int attrResId,
        @ColorRes int fallbackColorResId) {
    final TypedValue tv = new TypedValue();
    if (context.getTheme().resolveAttribute(attrResId, tv, true)) {
        return tv.data;
    }
    return ContextCompat.getColor(context, fallbackColorResId);
}

From source file:com.philliphsu.bottomsheetpickers.Utils.java

public static int getColorFromThemeAttr(Context context, int resid) {
    TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { resid });
    final int color = a.getColor(0/*index*/, 0/*defValue*/);
    a.recycle();//from ww w . j a  va  2  s . c o m
    return color;
}

From source file:com.ww.administrator.demotest.util.ViewUtil.java

public static int getActionBarSize(Context context) {
    if (actionBarSize < 0) {
        TypedValue value = new TypedValue();
        context.getTheme().resolveAttribute(android.R.attr.actionBarSize, value, true);
        actionBarSize = TypedValue.complexToDimensionPixelSize(value.data,
                context.getResources().getDisplayMetrics());
    }/*from   w w  w.  j  a  v  a  2  s .co  m*/
    return actionBarSize;
}

From source file:com.hangulo.powercontact.util.Utils.java

public static int getAttributeHeight(Context ctx, int resid) {
    int retHeight = 0;
    TypedValue tv = new TypedValue();
    if (ctx.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
        retHeight = TypedValue.complexToDimensionPixelSize(tv.data, ctx.getResources().getDisplayMetrics());

    }//from  w  w w.  j av a 2 s . co  m
    return retHeight;
}

From source file:com.bw.luzz.monkeyapplication.View.DateTimePicker.Utils.java

/**
 * Gets the colorAccent from the current context, if possible/available
 * @param context The context to use as reference for the color
 * @return the accent color of the current context
 *//* w  w w .  j ava 2s.  c  om*/
public static int getAccentColorFromThemeIfAvailable(Context context) {
    TypedValue typedValue = new TypedValue();
    // First, try the android:colorAccent
    if (Build.VERSION.SDK_INT >= 21) {
        context.getTheme().resolveAttribute(android.R.attr.colorAccent, typedValue, true);
        return typedValue.data;
    }
    // Next, try colorAccent from support lib
    int colorAccentResId = context.getResources().getIdentifier("colorAccent", "attr",
            context.getPackageName());
    if (colorAccentResId != 0 && context.getTheme().resolveAttribute(colorAccentResId, typedValue, true)) {
        return typedValue.data;
    }
    // Return the value in mdtp_accent_color
    return ContextCompat.getColor(context, R.color.mdtp_accent_color);
}

From source file:com.sonymobile.androidapp.gridcomputing.utils.ViewUtils.java

/**
 * Returns the accent color.//from w w w .jav  a  2  s .c o  m
 *
 * @param context Context used to get the resources.
 * @return The accent color or holo_blue_light (if the accent color isn't
 * available).
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static int getAccentColor(final Context context) {
    int color = ContextCompat.getColor(context, android.R.color.holo_blue_light);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(android.R.attr.colorPrimary, typedValue, true);
        color = typedValue.data;
    } else {
        final int resId = context.getResources().getIdentifier("somc_theme_accent_color_light", "color",
                "com.sonyericsson.uxp");
        if (resId != 0) {
            color = ContextCompat.getColor(context, resId);
        }
    }
    return color;
}

From source file:com.agenmate.lollipop.util.ColorUtils.java

public static int getAccentColorFromThemeIfAvailable(Context context) {
    TypedValue typedValue = new TypedValue();
    // First, try the android:colorAccent
    if (Build.VERSION.SDK_INT >= 21) {
        context.getTheme().resolveAttribute(android.R.attr.colorAccent, typedValue, true);
        return typedValue.data;
    }/*from w  w  w . j ava 2 s .c  o  m*/
    // Next, try colorAccent from support lib
    int colorAccentResId = context.getResources().getIdentifier("colorAccent", "attr",
            context.getPackageName());
    if (colorAccentResId != 0 && context.getTheme().resolveAttribute(colorAccentResId, typedValue, true)) {
        return typedValue.data;
    }
    // Return the value in mdtp_accent_color
    return ContextCompat.getColor(context, R.color.colorAccent);
}

From source file:me.henrytao.mdcore.core.MdCompat.java

public static int getActionBarSize(Context context) {
    TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { android.R.attr.actionBarSize });
    int size = (int) a.getDimension(0, 0);
    a.recycle();//from  ww w.  j av  a  2 s . co m
    return size;
}

From source file:android.support.v7.app.AppCompatDialog.java

private static int getThemeResId(Context context, int themeId) {
    if (themeId == 0) {
        // If the provided theme is 0, then retrieve the dialogTheme from our theme
        TypedValue outValue = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.dialogTheme, outValue, true);
        themeId = outValue.resourceId;/*from   ww  w.  j  av a 2  s . c  om*/
    }
    return themeId;
}

From source file:com.weihuoya.bboo._G.java

public static int getThemeColor(int id, int defaultValue) {
    TypedValue value = new TypedValue();
    try {/*from   www .j a  v  a  2s.c  o m*/
        Context context = getContext();
        Resources.Theme theme = context.getTheme();
        if (theme != null && theme.resolveAttribute(id, value, true)) {
            if (value.type >= TypedValue.TYPE_FIRST_INT && value.type <= TypedValue.TYPE_LAST_INT) {
                return value.data;
            } else if (value.type == TypedValue.TYPE_STRING) {
                ContextCompat.getColor(context, value.resourceId);
            }
        }
    } catch (Exception e) {
        _G.log(e.toString());
    }
    return defaultValue;
}