2

sou um estudante e desenvolvedor iniciante e tenho um projeto que tenho que fazer que consiste basicamente em trocar a cor do Action bar(Menu Superior) selecionando um dos 3 radiobutton (botões de seleção única) aonde existe as cores vernelho, verde e azul e quando o usuário apertar o botão selecionar ele troca a cor e volta para um tela com um Hello World. Como deve funcionar:

A tela na qual o aplicativo inicializa é o Hello World então o usuário clica sobre o botão share e é redirecionando para outra tela contendo as 3 opções de cores (Verde,Vermelho e Azul) então o usuário seleciona uma delas e clica sobre o botão selecionar, então é retornado a tela inicial do Hello World com a cor alterada.

segue abaixo o código:

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";


}

}

1
  • O evento onSelecionarClick não foi registrado a nenhum dos botões. No caso teria que usar o RadioGroup com setOnCheckedChangeListener para detectar quando mudar.
    – Syzoth
    8/04/2020 às 13:36

0

Pesquise outras perguntas com a tag .