0

I have created a string in action bar. What I want here is, when I click on the red button, background will change its color to red. Could anyone tell me how I can achieve this? Here is my Main.java file:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        setFrameVisibility(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.colourred) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

Here is the xml file

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/colourred"
    android:orderInCategory="100"
    android:title="@string/colour_red"
    app:showAsAction="never" />
</menu>
Wilder Pereira
  • 2,249
  • 3
  • 21
  • 31

4 Answers4

0

In onOptionsItemSelected, if id == R.id.colourred then change color by setting the action bar

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color
                    .parseColor("red color code")));

Background of app screen in onOptionsItemSelected

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.colourred) {

         findViewById(R.id.root layout id).setBackgroundColor(Color.RED);

            return true;
        }
hareesh J
  • 520
  • 1
  • 6
  • 21
0

Just add this line to your onOptionsItemSelected:

getWindow().getDecorView().setBackgroundColor(Color.RED);

Your Main.java will look something like this:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
            setFrameVisibility(true);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.colourred) {
                getWindow().getDecorView().setBackgroundColor(Color.RED);
            }

            return super.onOptionsItemSelected(item);
        }
Wilder Pereira
  • 2,249
  • 3
  • 21
  • 31
0
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.colourred) {
        //toolBar.setBackgroundColor(Color.RED);
        getWindow().getDecorView().setBackgroundColor(Color.RED);
        getWindow().getDecorView().setBackgroundColor(Color.parseColor("#FFCC33"));
        //or like below with color code
        //toolBar.setBackgroundColor(Color.parseColor("#FFCC33"));
        return true;
    }else if(id==R.id.color_green){
        getWindow().getDecorView().setBackgroundColor(Color.parseColor("#green_color_code"));
    }else if(id==R.id.other_id){
        getWindow().getDecorView().setBackgroundColor(Color.parseColor("#other_color_code"));
    }
}

And if you wish to change activity background color then visit this How to set background color of Activity to white programmatically?

getWindow().getDecorView().setBackgroundColor(Color.WHITE);//change activity bg color
halfer
  • 19,824
  • 17
  • 99
  • 186
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
0

Try inserting in your code

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    ...
    //noinspection SimplifiableIfStatement
    if (id == R.id.colourred) {
        getWindow().getDecorView().setBackgroundColor(Color.RED);
        return true;
    }
    ...
}

Since this if tells u that your 'colloured' item in menu was selected, so in there you want to handle it.

Paweł.Ch
  • 44
  • 4