android custom view obtainStyledAttributes -


i'm pretty new android , java forgive me if i'm missing obvious.

i want write custom view, , have problems getting custom attributes via otainstyledattributes. have simple testcase. in eclipse create empty project (package com.example.customview) default activity. added "attrs.xml" file under res/values following content:

  <resources>     <declare-styleable name="testview">        <attr name="test" format="string"/>     </declare-styleable>   </resources> 

then add new class "testview.java" in new package "com.example.views".

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 = context.gettheme().obtainstyledattributes(attrs, r.styleable.testview, 0, 0);      tv.settext(a.getstring(r.styleable.testview_test));      a.recycle();  } } 

and in activity_main.xml have:

<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" >      <com.example.views.testview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/hello_world"          my:test="test"         />  </relativelayout> 

i able my:test attribute

attrs.getattributevalue("http://schemas.android.com/apk/res/com.example.customview","test") 

but works if put attribute in random namespace.

but a.getstring(r.styleable.testview_test) allways null.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -