android - Can I override some attribute of drawable shape? -


i have 2 buttons, same shape, color different

b1.xml

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle">     <corners android:radius="10dp" />     <stroke android:width="5px" android:color="#000000" />     <solid         android:color="#ff0000"/> </shape> 

b2.xml

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle">     <corners android:radius="10dp" />     <stroke android:width="5px" android:color="#000000" />     <solid         android:color="#00ff00"/> </shape> 

layout.xml

<button     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="@drawable/b1"     android:text="b1" />   <button     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="@drawable/b2"     android:text="b2" /> 

if want create 100 button different colors, need create 100 drawable xml.

can create 1 drawable xml, , override color or other attributes in layout xml?

via xml no can't. xml fixed elements if need dynamic processing, use java.

in specific case can try achieve need using drawable paint , colorfilter, that:

button b1 = (button) findviewbyid(r.id.button1); shapedrawable sd = (shapedrawable) b1.getbackground(); sb.getpaint().setcolor(color); sb.setcolorfilter(... something); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -