Accessing attrs in AttributeSet for custom components

Josh Kovach picture Josh Kovach · Nov 28, 2011 · Viewed 24.8k times · Source

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?

Answer

serkanozel picture serkanozel · Nov 28, 2011

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,