Yes it is possible;
Suppose 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 get attributes from the Android namespace as such:
String xmlProvidedSize = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textSize");
The value of xmlProvidedSize will be something like this "14.0sp" and perhaps with a little editing of String you can just extract the numbers.
Another option for declaring your own attribute set will be small, but it is also possible.
So, you have your own custom view, and your TextViews declared something like this:
public class MyCustomView extends RelativeLayout{ private TextView myTextView1; private TextView myTextView2;
big...
Now you also need to make sure that your custom view overrides the constructor, which takes an attribute set as follows:
public MyCustomView(Context context, AttributeSet attrs){ super(context, attrs); init(attrs, context);
ok, let's see what the init () method is now:
private void init(AttributeSet attrs, Context context){
You are probably wondering where R.styleable.MyCustomView, R.styleable.MyCustomView_text1Size and R.styleable.MyCustomView_text2Size come from; let me dwell on them in detail.
You must declare the attribute names in the attrs.xml file (in the values directory) so that wherever you can use your custom view, the values obtained from these attributes will be passed to your constructor.
So, let's see how you declare these user attributes as you requested: 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 the size of your TextViews in your XML, but NOT without declaring a namespace in your layout, here's 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" />
Notice how I declared the “josh” namespace as the first line in your CustomView attribute set.
Hope this helps Josh