How to change the color of the action bar in android studio

Asked

Viewed 131 times

2

I am a student and developer beginner and I have a project that I have to do that basically consists of changing the color of the Action bar (Top Menu) by selecting one of the 3 radiobutton (single selection buttons) where there are the varnished colors, green and blue and when the user presses the select button he changes the color and comes back to a screen with a Hello World. How it should work:

The screen on which the application boots is Hello World so the user clicks on the share button and is redirecting to another screen containing the 3 color options (Green, Red and Blue) then the user selects one of them and clicks on the select button, then the Hello World home screen is returned with the color changed.

follows below the code:

public class MainActivity extends AppCompatActivity {

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    Intent intent = getIntent();

    if(intent != null){
        String cor = intent.getStringExtra("cor");
        if(cor == null) { //não há cor passada no intent

            ;

            cor = "azul";
        }
        else{

        }

        if(cor.equalsIgnoreCase("verde"))
            toolbar.setBackgroundColor(Color.GREEN);
        else if(cor.equalsIgnoreCase("vermelho"))
            toolbar.setBackgroundColor(Color.RED);
        else
            toolbar.setBackgroundColor(Color.BLUE);


    }

}

private void ShareMessage(String msg){
    Intent shareMsgIntent = new Intent();

    shareMsgIntent.setAction(Intent.ACTION_SEND);
    shareMsgIntent.putExtra(Intent.EXTRA_TEXT, msg);
    shareMsgIntent.setType("text/plain");

    if  (shareMsgIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(shareMsgIntent);
    }
}

private void ShowColorPreference(){
    Intent colorPreferenceIntent = new Intent(this, Main2Activity.class);
    colorPreferenceIntent.putExtra("cor", "verde");
    startActivity(colorPreferenceIntent);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void onShareClick(View view) {
    ShareMessage("Olá mundo!");
    ShowColorPreference();
}

}


public class Main2Activity extends AppCompatActivity {

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });


    Intent intent = getIntent();
    String cor = intent.getStringExtra("cor");

    RadioButton rdbVerde = (RadioButton)findViewById(R.id.radioVerde);
    RadioButton rdbAzul = (RadioButton)findViewById(R.id.radioAzul);
    RadioButton rdbVermelho = (RadioButton)findViewById(R.id.radioVermelho);

    if(cor.equalsIgnoreCase("verde"))
        rdbVerde.setChecked(true);
    else if(cor.equalsIgnoreCase("vermelho"))
        rdbVermelho.setChecked(true);
    else
        rdbAzul.setChecked(true);

}

public void onSelecionarClick(View view) {
    String cor = "";

    RadioButton rdbVerde = (RadioButton)findViewById(R.id.radioVerde);
    RadioButton rdbAzul = (RadioButton)findViewById(R.id.radioAzul);
    RadioButton rdbVermelho = (RadioButton)findViewById(R.id.radioVermelho);

    if(rdbVerde.isChecked())
        cor = "verde";
    else if(rdbAzul.isChecked())
        cor = "azul";
    else if (rdbVermelho.isChecked())
        cor = "vermelho";


}

}

  • The onSelecionarClick event was not registered to any of the buttons. In case would have to use Radiogroup with setOnCheckedChangeListener to detect when change.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.