Issue
I've a xml file in drawable which looks like :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/green_button_unchecked" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="@color/green_button_unchecked" android:state_focused="true"/>
<!-- focused -->
<item android:drawable="@color/green_button_unchecked" android:state_selected="true"/>
<!-- selected -->
<item android:drawable="@color/gray_6"/>
<!-- default -->
</selector>
I can use this in setBackgroundResource(drawable.xmlName);
As we know , setBackgroundResource() need resouceId of int type...
How can i use setBackgroundResource() by creating xml programatically if i am not allowed to use resources ??
I hope you understood my problem... Thankyou
Solution
I'm not sure how you're creating XML Programmatically, as if you want to create a Drawable you can do that without needing XML.
Any XML file has a name, and the IDE creates a generated class that contains all the IDs of the files.
If it's a drawable, it's R.drawable.
For colors it's R.color.
For Strings R.string.
So if you want to set background by resource, you just use setBackgroundResource(R.drawable.file_name)
If you don't want to use XML, you'll have to manually create the Drawable object, and use setBackground or setBackgroundDrawable instead. It is a big hassle, since there are many types of Drawables.
Vector Drawable, Gradient Drawable, Layered Drawable, Color Drawable, etc.
You'll have to figure out what you want and create it manually
Answered By - Ahmad Sattout
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.