5

I have a custom android view class which amongst other things is going to be drawing a lot of text directly into the canvas supplied to its onDraw override.

What I'd like to do is have a attribute which could be set to something like "?android:attr/textAppearanceLarge" and pick up the regular text settings without further styling.

In my custom view's attrs.xml, I have

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView" >
        ...

        <attr name="textAppearance" format="reference" />

        ...
    </declare-styleable>
</resources>

and then in CustomView.java

final int[] bogus = new int[] { android.R.attr.textColor, android.R.attr.textSize, android.R.attr.typeface, android.R.attr.textStyle, android.R.attr.fontFamily };
final int ap = styledAttributes.getResourceId(com.test.R.styleable.MyView_textAppearance, -1);
final TypedArray textAppearance = ap != -1 ? context.obtainStyledAttributes(ap, bogus) : null;

if (textAppearance != null) {
    for (int i = 0; i < textAppearance.getIndexCount(); i++) {
        int attr = textAppearance.getIndex(i);

        switch (attr) {
        case android.R.attr.textColor:  textColor = textAppearance.getColor(attr, textColor); break;
        case android.R.attr.textSize:   textSize = textAppearance.getDimensionPixelSize(attr, textSize); break;
        case android.R.attr.typeface:   typefaceIndex = textAppearance.getInt(attr, typefaceIndex); break;
        case android.R.attr.textStyle:  textStyle = textAppearance.getInt(attr, textStyle);  break;
        case android.R.attr.fontFamily: fontFamily = textAppearance.getString(attr); break;         
        }
    }

    textAppearance.recycle();
}

I've tried all sorts of variations on the switch variable, on the case constants etc and I never end up getting anything even remotely useful.

What am I doing wrong here?

2 Answers 2

3

I know this is an old question but just stumbled into this issue and this solution works for me:

in attrs.xml

<resources>
    <declare-styleable name="CustomView">
        ....
        <attr name="textAppearance" format="reference" />
    </declare-styleable>
</resources>

in CustomView.java

textAppearance = a.getResourceId(R.styleable.CustomView_textAppearance, -1);
TextViewCompat.setTextAppearance(textView, textAppearance);

then in activity.xml

<CustomView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:textAppearance="?android:attr/textAppearanceMedium"
Sign up to request clarification or add additional context in comments.

Comments

0

I think you access to different resources:

com.test.R.styleable.MyView_textAppearance

is your own,

android.R.attr.textColor

and others are Android's.

So I managed to make it defining my own attributes:

    <com.test.TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        myns:textColor="@android:color/darker_gray" 
        myns:textSize="18sp"
        myns:textStyle="normal"/>

and attrs.xml:

<declare-styleable name="MyView_textAppearance">
    <attr name="textColor" format="reference|color" />

    <attr name="textSize" format="dimension" />
    <attr name="textStyle">
        <flag name="normal" value="0" />
        <flag name="bold" value="1" />
        <flag name="italic" value="2" />
    </attr>

</declare-styleable>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.