java – Accessing attrs in AttributeSet for custom components-ThrowExceptions

Exception or error:

I’ve got a custom component containing two TextViews that have a custom size-setting method on (the two text views are proportional by about a 1:2 ratio). Being that this is a subclass of RelativeLayout, it doesn’t have a textSize attribute, but I’m wondering if it is possible to still set the android:textSize attribute in the XML instantiation for this component, and then grab the textSize attribute from the AttributeSet to use with my custom setSize() method in the constructor.

I’ve seen the technique to do this with custom attributes, but what if I want to grab an attribute that’s already in the android lexicon?

How to solve:

yes it is possible;

let’s assume your RelativeLayout declaration (in xml) has textSize defined with 14sp:

android:textSize="14sp"

In the constructor of your custom view (the one that takes in the AttributeSet), you can retrieve the attributes from Android’s namespace as such:

String xmlProvidedSize = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textSize");

The value of the xmlProvidedSize will be something like this “14.0sp” and maybe with little bit of String editing you can just extract the numbers.


Another option to declare your own attribute set would be little lengthy, but it is also possible.

So, you have your custom view and your TextViews declared something like this right:

public class MyCustomView extends RelativeLayout{

    private TextView myTextView1;
    private TextView myTextView2;

// rest of your class here

great…

Now you need to also make sure your custom view overrides the constructor that takes in the AttributeSet like this:

public MyCustomView(Context context, AttributeSet attrs){   
    super(context, attrs);
    init(attrs, context);  //nice, clean method to instantiate your TextViews// 
}

ok, let’s see that init() method now:

private void init(AttributeSet attrs, Context context){
    // do your other View related stuff here //


    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyCustomView);
    int xmlProvidedText1Size = a.int(R.styleable.MyCustomView_text1Size);
    int xmlProvidedText2Size = a.int(R.styleable.MyCustomView_text2Size);

    myTextView1.setTextSize(xmlProvidedText1Size);
    myTextView2.setTextSize(xmlProvidedText2Size);

    // and other stuff here //
}

You’re probably wondering where does R.styleable.MyCustomView, R.styleable.MyCustomView_text1Size and R.styleable.MyCustomView_text2Size are coming from; allow me to elaborate on those.

You have to declare the attribute name(s) in your attrs.xml file (under values directory) so that where ever you get to use your custom view, the values gathered from these attributes will be handed in your constructor.

So let’s see how you declare the these custom attributes like you’ve asked:
Here is my whole attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyCustomView">
        <attr name="text1Size" format="integer"/>
        <attr name="text2Size" format="integer"/>
    </declare-styleable>
</resources>

Now you can set your TextViews’ size in your XML, but NOT without declaring the namespace in your Layout, here is how:

<com.my.app.package.MyCustomView
    xmlns:josh="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_custom_view_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    josh:text1Size="15"
    josh:text2Size="30"     
    />

Please pay attention how I declared the namespace to be “josh” as the first line in your CustomView’s attribute set.

I hope this helps Josh,

###

The accepted answer is a little long. Here is a condensed version that I hope will be easy to follow. In order to add a textSize attribute (or anything that uses dp/sp) to your custom view, do the following steps.

1. Create a custom attribute

Create (or add the following section) to attrs.xml. Note the dimension format.

<resources>
    <declare-styleable name="CustomView">
        <attr name="textSize" format="dimension" />
    </declare-styleable>
</resources>

2. Set the attribute in your xml layout.

Note the custom app namespace.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <com.example.myproject.CustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:textSize="20sp" /> 

</RelativeLayout>

3. Get the attribute value in your custom view

Note the use of getDimensionPixelSize. Within your custom view just work with pixels. See this answer if you need to convert to a different dimension.

public class CustomView extends View {

    private float mTextSize; // pixels

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs, R.styleable.CustomView, 0, 0);
        try {
            mTextSize = a.getDimensionPixelSize(R.styleable.CustomView_textSize, 0);
        } finally {
            a.recycle();
        }
    }

    /**
     * @param size in SP units
     */
    public void setTextSize(int size) {
        mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                size, getResources().getDisplayMetrics());
        mTextPaint.setTextSize(mTextSize);
        invalidate();
        requestLayout();
    }

    // ...
}

Notes

Leave a Reply

Your email address will not be published. Required fields are marked *