Skip to content

Instantly share code, notes, and snippets.

@granoeste
Created May 2, 2012 05:48
Show Gist options
  • Save granoeste/2574198 to your computer and use it in GitHub Desktop.
Save granoeste/2574198 to your computer and use it in GitHub Desktop.
[Android] Get the color from the theme.
// ---------------------------------------------------
// Get the color and more attributes from the theme.
// ---------------------------------------------------
TypedArray ta = context.getTheme().obtainStyledAttributes(R.styleable.ViewStyle);
int labelColor = ta.getColor(R.styleable.ViewStyle_labelColor, defValue);
int dividerColor = ta.getColor(R.styleable.ViewStyle_dividerColor, defValue);
float textSize = ta.getDimension(R.styleable.ViewStyle_textSize, defValue);
<resources>
<declare-styleable name="ViewStyle">
<attr name="dividerColor" format="color" />
<attr name="labelColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
<resources>
<dimen name="font_size">16sp</dimen>
</resources>
<resources>
<style name="MyTheme" parent="Theme.Sherlock">
<item name="dividerColor">@android:color/darker_gray</item>
<item name="labelColor">@android:color/primary_text_light</item>
<item name="textSize">@dimen/font_size</item>
</style>
</resources>
@nbtk123
Copy link

nbtk123 commented May 10, 2016

Very helpful

@danmelt
Copy link

danmelt commented Nov 18, 2020

I agree--very helpful. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment