Search code examples
androidandroid-themexml-drawable

How can i allow user to change button's color in an android app


I am developing a calculator app for android and i use drawable files to manage the color of sets of buttons. i need to allow user to change the calculator theme by providing several drawable files then the user can choose between them. I have no idea how to do this except making multiple layout then i can use (if) statement in the activity. is there an easy way to this i use somthing like this to define the button background

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="9dip" />
            <stroke android:width="1dip" android:color="#009e9e9e" />
            <gradient android:angle="-90" android:startColor="#ae104f" android:endColor="#8e0d40"  />
        </shape>
    </item>
    <item>
        <layer-list>
            <item android:right="5dp" android:top="5dp">
                <shape>
                    <corners android:radius="8dp" />
                    <solid android:color="#bd848484" />
                </shape>
            </item>
            <item android:bottom="1dp" android:left="2dp">
                <shape>
                    <gradient android:angle="270"
                        android:endColor="#ae104f" android:startColor="#8e0d40" />
                    <stroke android:width="1dp" android:color="#BABABA" />
                    <corners android:radius="9dp" />
                    <padding android:bottom="10dp" android:left="10dp"
                        android:right="10dp" android:top="10dp" />
                </shape>
            </item>
        </layer-list>
    </item>

</selector>

Thanks


Solution

  • You can store user's color file or sharedpreferences. and you app starting read this color, and store static value so set backcolor(static color value) for all button. If i were you, i do that. It'll work.