72

I wrote a custom view that extends RelativeLayout. My view has text, so I want to use the standard android:text without the need to specify a <declare-styleable> and without using a custom namespace xmlns:xxx every time I use my custom view.

this is the xml where I use my custom view:

<my.app.StatusBar android:id="@+id/statusBar" android:text="this is the title"/> 

How can I get the attribute value? I think I can get the android:text attribute with

TypedArray a = context.obtainStyledAttributes(attrs, ???); 

but what is ??? in this case (without a styleable in attr.xml)?

4
  • read the documentation: developer.android.com/reference/android/content/… Commented Aug 2, 2013 at 9:56
  • 4
    Not very useful, can you give me something more? :) Commented Aug 2, 2013 at 9:59
  • Just omit the 2nd parameter Commented Aug 2, 2013 at 10:01
  • 1
    I was confused because my attrs is AttributeSet and not int[] Commented Aug 2, 2013 at 10:05

2 Answers 2

112

use this:

public YourView(Context context, AttributeSet attrs) { super(context, attrs); int[] set = { android.R.attr.background, // idx 0 android.R.attr.text // idx 1 }; TypedArray a = context.obtainStyledAttributes(attrs, set); Drawable d = a.getDrawable(0); CharSequence t = a.getText(1); Log.d(TAG, "attrs " + d + " " + t); a.recycle(); } 

i hope you got an idea

Sign up to request clarification or add additional context in comments.

7 Comments

i used same thing but it seems like not working for some attributes like 'textColor'. do you have any idea?
@pskink what's you thoughts about stackoverflow.com/questions/24650879/… ?
One question. Since that reused attribute doesn't belong to a custom view (in this case android:text doesn't belong to RelativeLayout) it is not showing in 'suggestions' in IDE (Android Studio in my case) when declaring the custom view in XML. Is there a way to make android:text appear in suggestions as well so user knows this is also available attribute in the view?
This does not work anymore, at least in my version of Android Studio (2.2). When trying to call getText(1) on the TypedArray with a plain int, the inspection complains: "Expected resource of type styleable". Using getText() with something like R.styleable.MyCustomView_android_text works however. I guess Android Studio became "smarter"
@sebkur did you actually run the code? if so, what does getText() return?
|
49

EDIT

Another way to do it (with specifying a declare-styleable but not having to declare a custom namespace) is as follows:

attrs.xml:

<declare-styleable name="MyCustomView"> <attr name="android:text" /> </declare-styleable> 

MyCustomView.java:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView); CharSequence t = a.getText(R.styleable.MyCustomView_android_text); a.recycle(); 

This seems to be the generic Android way of extracting standard attributes from custom views.

Within the Android API, they use an internal R.styleable class to extract the standard attributes and don't seem to offer other alternatives of using R.styleable to extract standard attributes.

Original Post

To ensure that you get all the attributes from the standard component, you should use the following:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextView); CharSequence t = a.getText(R.styleable.TextView_text); int color = a.getColor(R.styleable.TextView_textColor, context.getResources().getColor(android.R.color.darker_gray)); // or other default color a.recycle(); 

If you want attributes from another standard component just create another TypedArray.

See http://developer.android.com/reference/android/R.styleable.html for details of available TypedArrays for standard components.

9 Comments

':' is not a valid resource name character. damn it.
@SashaNos. The ':' is used in XML but for the resource name in code use '_' in place of it. See the difference in the post. I'll delete the original post if it is confusing or unhelpful
yes, I understand the difference. Android Studio just doesn't accept this name <attr name="android:text" /> because of colon :(
It does. I am using android:orientation in one of my projects now. However, you may need to ensure that you are using the correct attribute name. The list of valid names is listed in the attribute reference.
This should be the accepted answer. It requires minimal change to existing code and reads a lot cleaner.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.