1

I want to change the background colors using menu(s).

Since background color is changed in the activity.xml part whereas I use switch case for the various menu option in the MainActivity.java page.

So, how can I control/change the background color using menu from java ?

I am using the following switch case :

    public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    View someView = findViewById(R.id.rootLayout);
    View root = someView.getRootView();

    switch(item.getItemId()){

    case R.id.options1:root.setBackgroundColor(getResources().getColor(android.R.color.holo_purple));
        break;
    case R.id.option2:root.setBackgroundColor(getResources().getColor(android.R.color.holo_purple));

        break;
    case R.id.option3:root.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));

        break;

    }
    return super.onContextItemSelected(item);
}

EDIT1 : The following code is able to change the background.

PS : New in App development

  • 1
    [Is this what you are looking for](http://stackoverflow.com/questions/4761686/how-to-set-background-color-of-activity-to-white-programmatically) – GermaineJason Nov 26 '14 at 18:26

1 Answers1

1

Get the rootlayout from your current activity with findViewByID(int id) and then set the background with setBackgroundColor(int color) accordingly.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
Michael
  • 815
  • 1
  • 6
  • 23