user4058730
user4058730

Reputation:

Changing background color using menu(s)

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

Upvotes: 1

Views: 469

Answers (1)

Michael
Michael

Reputation: 815

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

Upvotes: 1

Related Questions