java - Dynamically change drawable colors -


in application have lot of drawables defined xml files. example have button defined that:

button.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- bottom 3dp shadow --> <item android:top="3dp">     <shape android:shape="rectangle">           <corners android:radius="3dp" />           <solid android:color="@color/black_30" />        </shape> </item> <!-- green top color --> <item android:top="3dp" android:bottom="3dp" android:id="@+id/background">     <shape android:shape="rectangle">         <corners android:radius="3dp" />         <solid android:color="@color/green1" />                </shape>  </item> </layer-list> 

and display button that:

layout.xml <button         android:id="@+id/button"         android:layout_gravity="center"         android:layout_height="60dp"         android:layout_width="fill_parent"         android:textsize="17sp"         android:gravity="center"         android:background="@drawable/button" /> 

when navigate in app, want "theme" views (some colors changing in function of context) , able change dynamically color of button (green1) @ runtime.

1) 1 first nice approach change color definition in button.xml ?attr/my_color. , define different color values need in theme file style.xml. @ runtime, can switch desired theme , work. complete steps here:

how reference colour attribute in drawable?

the issue works on android 5 not on android 4 ( , need support version) (we android.view.inflateexception: binary xml file line #2: error inflating class <unknown>)

2) second approach load drawable in code , use setcolorto change color of drawable: (written in xamarin.android sure understand corresponding java version)

layerdrawable button = (layerdrawable)resources.getdrawable(resource.drawable.normal_question_button); gradientdrawable background = (gradientdrawable)button.finddrawablebylayerid(resource.id.background); background.setcolor(android.graphics.color.red.toargb()); 

the things it's works... randomly... when displaying button again, it's still original green displayed. sometimes, it's new color... , once have 1 of both behaviour, same color can stay many time , changes again correct one.

someone explain this? there caching on drawables can gives kind of issue?

3) thinking third solution: dynamically change color defined in colors.xml (where green1 defined) doesn't seem possible

in fact 2) 1 simple solution is:

instead of trying customize drawable coming xml file:

 layerdrawable button = (layerdrawable)resources.getdrawable(resource.drawable.normal_question_button); gradientdrawable background = (gradientdrawable)button.finddrawablebylayerid(resource.id.background); background.setcolor(android.graphics.color.red.toargb()); 

we can change directly color on each button once have instance them:

layerdrawable buttondrawable = _button.background; gradientdrawable background = (gradientdrawable)buttondrawable.finddrawablebylayerid(resource.id.background); background.setcolor(android.graphics.color.red.toargb()); 

Comments

Popular posts from this blog

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -

How to run C# code using mono without Xamarin in Android? -