android - Get the resource id for the drawable reference used in styled attribute -
having custom view myview
define custom attributes:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="myview"> <attr name="normalcolor" format="color"/> <attr name="backgroundbase" format="integer"/> </declare-styleable> </resources>
and assign them follows in layout xml:
<com.example.test.myview android:id="@+id/view1" android:text="@string/app_name" . . . app:backgroundbase="@drawable/logo1" app:normalcolor="@color/blue"/>
at first thought can retrieve custom attribute backgroundbase
using:
typedarray = context.gettheme().obtainstyledattributes(attrs, r.styleable.myview, defstyle, 0); int base = a.getinteger(r.styleable.myview_backgroundbase, r.drawable.blank);
which works when attribute not assigned , default r.drawable.blank
returned.
when app:backgroundbase
assigned exception thrown "cannot convert integer type=0xn" because, though custom attribute format declares integer, references drawable
, should retrieved follows:
drawable base = a.getdrawable(r.styleable.myview_backgroundbase); if( base == null ) base = bitmapfactory.decoderesource(getresources(), r.drawable.blank);
and works.
question:
i don't want drawable
typedarray, want integer id corresponding app:backgroundbase
(in example above r.drawable.logo1
). how can it?
it turns out answer right there:
typedarray = context.gettheme().obtainstyledattributes(attrs, r.styleable.myview, defstyle, 0); int base = a.getresourceid(r.styleable.myview_backgroundbase, r.drawable.blank);
Comments
Post a Comment