0

I have many XMLs that use background color from shape drawables like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:background="@drawable/background"
/>

My shape background xml is:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient 
        android:type="linear"
        android:startColor="#FFffffff"
        android:endColor="#FFE8E8E8"
        android:angle="315" />    
</shape>

Now I want to add a feature where I give the user the option to change the background color. Is there a quick way that I can change the source shape drawable based on some value instead of going to Each activity that loads xml and change that?

Thank you.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Snake
  • 14,228
  • 27
  • 117
  • 250

2 Answers2

0

You can create a list or buttons for user with different names. Based on the button clicked you can change the layout background programmatically. For example:

 @Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.drawable1Button:
             final int sdk = android.os.Build.VERSION.SDK_INT;
             if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
             layout.setBackgroundDrawable( getResources().getDrawable(R.drawable.drawable1) );
             } else {
               layout.setBackground( getResources().getDrawable(R.drawable.drawable1));
             }

            break;

        case R.id.drawable2Button:
             final int sdk = android.os.Build.VERSION.SDK_INT;
             if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
             layout.setBackgroundDrawable( getResources().getDrawable(R.drawable.drawable2) );
             } else {
               layout.setBackground( getResources().getDrawable(R.drawable.drawable2));
             }
             break;
        case R.id.drawable3Button:
             final int sdk = android.os.Build.VERSION.SDK_INT;
             if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
             layout.setBackgroundDrawable( getResources().getDrawable(R.drawable.drawable3) );
             } else {
               layout.setBackground( getResources().getDrawable(R.drawable.drawable3));
             }
            break;
        }
       }
Aayush Thakur
  • 634
  • 1
  • 10
  • 22
  • Thanks but please read my email carefuly. I "don't" want to go and change it for activity as I have many layout xml. I want it to be done in one place – Snake Oct 27 '16 at 14:21
  • Then make a common method in some class and you will have to access it from each activity. Which will only take a single line of code. – Aayush Thakur Oct 27 '16 at 18:01
  • Accessing it from every activity is effectively changing the code of every activity. It is a big project with at least 40 activities and fragments. I don't to change each one. I want to change the source (the xml drawable) if possible – Snake Oct 28 '16 at 03:05
0

Change your background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/shape_id">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="line">
            <gradient
                android:angle="315"
                android:endColor="#FFE8E8E8"
                android:startColor="#FFffffff"
                android:type="linear" />

        </shape>
    </item>
</layer-list>

Now on button click just find out which color combinations you want to change and put that color in array and change on click.

 final LinearLayout l =(LinearLayout)findViewById(R.id.linearLayout);
        l.setBackgroundResource(R.drawable.background);
      final  View v = findViewById(R.id.linearLayout);
        Button b =(Button)findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int colors[] = { 0xff255779, 0xffa6c0cd };
                GradientDrawable gradientDrawable = new GradientDrawable(
                        GradientDrawable.Orientation.TOP_BOTTOM, colors);
                l.setBackground(gradientDrawable);

            }
        });
MIkka Marmik
  • 1,101
  • 11
  • 29