Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get "android:" tag values in a Custom View

There seems to be a lot of "similar" questions and answers to this scattered around which all refer to how to get a custom attribute from an AttributeSet. What I haven't been able to find so far is how to get an android: namespace tag:

    <com.custom.view.StatusThumbnail         android:id="@+id/statusThumbnailContainer"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_margin="5dp"         android:layout_weight="1"/> 

I would like to pull back the layout_height attribute from this custom component. Unfortunately from what I've read the closest I've got to how to do this is:

public StatusThumbnail(Context context, AttributeSet attrs) {     super(context, attrs);      String height = attrs.getAttributeValue("android", "layout_height"); 

But this returns null.

Surely this isn't a rare thing to try and do?

like image 578
Graeme Avatar asked Oct 24 '11 14:10

Graeme


People also ask

What is custom Attribute in android?

This allows you to define attributes a custom view can use. You do this by specifying an <attr> element, if it was previously defined you do not specify the format . If you wish to reuse an android attr, for example, android:gravity, then you can do that in the name , as follows.

What is a custom view?

Custom Views is just a way to make an android developer a painter. When you need to create some custom and reuse the views when it is not provided by the Android Ecosystem. Custom Views can be used as widgets like TextView, EditText etc.


1 Answers

The namespace should be "http://schemas.android.com/apk/res/android" android is an alias declared in your xml file

like image 108
jqpubliq Avatar answered Sep 24 '22 19:09

jqpubliq