Amit D
Amit D

Reputation: 11

How can i change background color of my layout by clicking a Button

How can i change background color of my layout by clicking on a Button ?

This is my code :

Button color_change;
LinearLayout layout;
int value = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    color_change = (Button)findViewById(R.id.color_btn);
    layout = (LinearLayout)findViewById(R.id.LL);

    color_change.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (value == 1) {
                 layout.setBackgroundColor(Color.RED);
            }
            else if (value == 2) {
                 layout.setBackgroundColor(Color.BLUE);
            }
            else if (value == 3) {
                 layout.setBackgroundColor(Color.MAGENTA);
            }
            else if (value == 4) {
                 layout.setBackgroundColor(Color.DKGRAY);
                 value = 0;
            }
            value++;
        }
    });

But i want replace if else condition with other code , because this code is complex and i want short code. So any one suggest me, how can i short my code ?

Upvotes: 1

Views: 412

Answers (5)

Christopher
Christopher

Reputation: 10259

You could use an array to store the data:

int[] colors = {Color.RED, Color.BLUE, Color.BLACK};
int index = value % colors.length;
layout.setBackgroundColor(colors[index]);
value++;

Upvotes: 1

kstachniuk
kstachniuk

Reputation: 358

A list of colors you want to use

List<int> colorsList = new List<int>();
colorsList.add(Color.RED);
colorsList.add(Color.WHITE);
colorsList.add(Color.BLUE);
colorsList.add(Color.GREEN);
//here you can add other colors to list

Iterator<int> colorIterator = colorsList.iterator();

function to get next color

int getNextColor()
{
   if(colorIterator.hasNext())
   {
      return colorIterator.next();
   }
   else
   {
      colorIterator = colorsList.iterator();
      return colorIterator.next();
   }
}

and here is your listener

color_change.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

             int color = getNextColor();

             layout.setBackgroundColor(color);
    }
});

Upvotes: 0

Naveen Kumar Kuppan
Naveen Kumar Kuppan

Reputation: 1442

You will use like this...

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                switch(value)
                {
                case 1:layout.setBackgroundColor(Color.RED);
                    break;
                case 2:layout.setBackgroundColor(Color.BLUE);
                    break;
                case 3: layout.setBackgroundColor(Color.MAGENTA);
                    break;
                case 4:layout.setBackgroundColor(Color.DKGRAY);
                 value = 0;
                    break;              
                }

                value++;
            }
        });

Upvotes: 0

Jitendra Prajapati
Jitendra Prajapati

Reputation: 1163

You can try this

put all color into a array and in onclick method get particular color from the array and set it like

 int color[]=new int[]{Color.BLUE,Color.RED,Color.GRAY};

  color_change.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        if (value <color.length) {
             layout.setBackgroundColor(color[value]);
        }
        value++;
    }
});

Upvotes: 2

Sergi Pasoevi
Sergi Pasoevi

Reputation: 2851

There is no magic. Use the switch instead of else ifs. For the added clarity, you might consider having constants or something as values to value, so that you don't have to deal with hard-coded integers.

Upvotes: 1

Related Questions