Search code examples
androidcolorsbackgroundandroid-actionbar

changing Color of an ActionBar in several Activities


I have been stuck with this problem for a few hours. So here it is. I have 2 different activities. The first one has a menu with with the following options:blue,red,green,orange,purple. As soon as I click one of them the background color of the ActionBar of the first Activity changes with no problem. Now the issue starts when I try to do it for the second activity as well. so in other words, with one click of an option in a menu in a single Activity, change both ActionBars background colors in both Activities.

public class MainActivity extends Activity {
    private static String Hex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button all_notes= (Button) findViewById(R.id.All_Notes);
        all_notes.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent =new Intent(MainActivity.this,AllNotes.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    } 

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        ActionBar bar = getActionBar();
        switch (item.getItemId()) {
            case R.id.action_search:
                Toast.makeText(this, Hex, Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_settings:
                Intent intent_1= new Intent(MainActivity.this,Settings.class);
                startActivity(intent_1);
                break;
            case R.id.blue:
                bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0099cc")));
                Hex="#0099cc";
                break;
            case R.id.pruple:
                bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#9933cc")));
                Hex="#9933cc";
                break;
            case R.id.orange:
                bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFBB33")));
                Hex="#FFBB33";
                break; 
            case R.id.red:
                bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#CC0000")));
                Hex="#CC0000";
                break;
            case R.id.green:
                bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#669900")));
                Hex="#669900";
                break;
            default:
                bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFBB33")));
                Hex="#FFBB33";
                break;
        }
        return true;
    }
    public String getHex()
    {
        return Hex;
    }
}


public class Settings extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        MainActivity MA=new MainActivity();
        ActionBar bar=getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(MA.getHex())));
    }
}

So here you can see that I have Mainactivity which deals with the menu and allows the user input to select a color and Setting which takes the user selected color through the getHex method and changes its respective ActionBar background. This obviously does not work. And I have tried to find a simple and pretty code that can do this without success. Any help is welcome.


Solution

  • It isn't a good idea creating a new instance of MainActivity in the onCreate method of the Settings activity. Basically MA.getHex() will always return null.

    If you just need to set the color of the action bar of the Settings activity to the one selected in the MainActivity, pass the hex value to the Settings activity as an extra in the intent (using Intent.putExtra) and get it in Settings.onCreate using getIntent().getStringExtra(...).

    Btw, I think you missed the call to setContentView in Settings.onCreate.