46

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?

Graeme
  • 25,714
  • 24
  • 124
  • 186

2 Answers2

66

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

Reno
  • 33,594
  • 11
  • 89
  • 102
jqpubliq
  • 11,874
  • 2
  • 34
  • 26
  • 1
    What about the case that we need multiple namespaces in one single xml file. For example I've created a custom view as well as some new attributes for that custom view. Now I wanna set attributes using my own defined and android predefined attrs. in such case your approach still returns null. it sounds i can not access android predefined attrs. How may I resolve such this case? – anonim Jun 10 '12 at 20:11
  • 9
    Superb ! finally this worked : `attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "maxLength", 100)` – user1060418 Jun 15 '14 at 18:49
  • Thanks! That wokrs for me! – Lagix Jan 13 '22 at 12:48
-5

First declare required attributes in :

res\attrs.xml

    <declare-styleable name="StatusThumbnail">
        <attr name="statusThumbnailattr" format="string"/>
    </declare-styleable>

then in your XML layout declaration use the same attribute

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

Access using

public StatusThumbnail(Context context, AttributeSet attrs) {
    super(context, attrs);
TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.StatusThumbnail);
this.mdColorDialogTitle=a.getString(R.styleable.StatusThumbnail_statusThumbnailattr);
}
Shardul
  • 27,760
  • 6
  • 37
  • 35
  • 2
    Specifically didn't want custom tags - as mentioned in the question. Far too much pollution on this subject by similar answers. – Graeme Oct 24 '11 at 15:34
  • 1
    I was trying to explain you additional implementation, anyways if feel like down voting I don't mind. – Shardul Oct 29 '11 at 16:03
  • 2
    Remember to do `try { ... } finally { a.recycle(); }` as described on [TypedArray](https://developer.android.com/reference/android/content/res/TypedArray.html) – Wernight Feb 04 '15 at 19:31