Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get theme attributes programmatically

How do I get the value of a Theme attribute programmatically?

For example:

Theme:

<style name="Theme">
    ... truncated ....
    <item name="textAppearanceLarge">@android:style/TextAppearance.Large</item>
</style>

Code:

int textSize = ???? // how do I get Theme.textAppearanceLarge? 

EDIT: too many answers that don't address the question.

like image 976
user123321 Avatar asked Nov 19 '11 02:11

user123321


1 Answers

use this function :

myView.setTextAppearance(Context context, int resid)
//Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.

see : http://developer.android.com/reference/android/widget/TextView.html#setTextAppearance%28android.content.Context,%20int%29

From TextView.java this is what the above function does:

public void setTextAppearance(Context context, int resid) {
    TypedArray appearance =
        context.obtainStyledAttributes(resid,
                                       com.android.internal.R.styleable.TextAppearance);

    int color;
    ColorStateList colors;
    int ts;

    .
    .
    .
    ts = appearance.getDimensionPixelSize(com.android.internal.R.styleable.
                                          TextAppearance_textSize, 0);
    if (ts != 0) {
        setRawTextSize(ts);
    }

    int typefaceIndex, styleIndex;

    typefaceIndex = appearance.getInt(com.android.internal.R.styleable.
                                      TextAppearance_typeface, -1);
    styleIndex = appearance.getInt(com.android.internal.R.styleable.
                                   TextAppearance_textStyle, -1);

    setTypefaceByIndex(typefaceIndex, styleIndex);
    appearance.recycle();
}

This is another way of doing this. Do make sure you recycle the appearance (TypedArray obect). Hope this helps!

like image 135
rDroid Avatar answered Sep 27 '22 21:09

rDroid