Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid custom view obtainStyledAttributes
    primarykey
    data
    text
    <p>I'm pretty new to Android and to Java so forgive me if i'm missing something obvious.</p> <p>I want to write a custom View, and i have problems getting the custom attributes via otainStyledAttributes. I have a very simple testcase. In Eclipse i create an empty project (package com.example.customview) with the default activity. Then i added an "attrs.xml" file under res/values with the following content:</p> <pre><code> &lt;resources&gt; &lt;declare-styleable name="testview"&gt; &lt;attr name="test" format="string"/&gt; &lt;/declare-styleable&gt; &lt;/resources&gt; </code></pre> <p>then i add a new class "testview.java" in a new package "com.example.views".</p> <pre><code>package com.example.views; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.widget.LinearLayout; import android.widget.TextView; import com.example.customview.R; public class testview extends LinearLayout{ public testview(Context context,AttributeSet attrs){ super(context,attrs); TextView tv = new TextView(context); this.addView(tv); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.testview, 0, 0); tv.setText(a.getString(R.styleable.testview_test)); a.recycle(); } } </code></pre> <p>and finally in activity_main.xml i have:</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:my="http://schemas.android.com/apk/res/com.example.customview" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" &gt; &lt;com.example.views.testview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" my:test="test" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>i am able to get the my:test attribute with </p> <pre><code>attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.customview","test") </code></pre> <p>but that also works if i put the attribute in any random namespace.</p> <p>but a.getString(R.styleable.testview_test) is allways null.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload