0

I'm new to Java, and I'm trying to make a simple calculator for waist and hip ratio. However the ratio has different rules for each gender.

My trouble is to link the radio button gender selection with the method with if statement. How can I do this? I have tried this code below, but it didn't work.

public class CalcActivity1 extends AppCompatActivity {

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

    public void btnCalcularOnClick(View v) {
        TextView resultado = (TextView) findViewById(R.id.lblResultado2);
        EditText txtquadril = (EditText) findViewById(R.id.txtquadril);
        EditText txtcintura = (EditText) findViewById(R.id.txtcintura);
        RadioGroup group = (RadioGroup) findViewById(R.id.radio_group);

        int quadril = Integer.parseInt(txtquadril.getText().toString());
        int cintura = Integer.parseInt(txtcintura.getText().toString());

        //CALC
        double rcq = cintura / quadril;

        int selectedId = group.getCheckedRadioButtonId();

        // I tried here to link them
        RadioButton RadioButton2 = (RadioButton) findViewById(selectedId);
        String chave = RadioButton2.getText().toString().toLowerCase();

        if (chave == "homem"){
            if(rcq <= 0.95){
                resultado.setText("Baixo");
            }
            else if(rcq > 0.96 & rcq <= 1){
                resultado.setText("Moderado");
            }
            else {
                resultado.setText("Alto");
            }
        }
        if (chave == "mulher"){
            if(rcq <= 0.80){
                resultado.setText("Baixo");
            }
            else if(rcq > 0.81 & rcq <= 0.85){
                resultado.setText("Moderado");
            }
            else {
                resultado.setText("Alto");
            }
        }

    }

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Alessander
  • 43
  • 4
  • Very likely your code does not work because you want to compare the value of the text of the selected RadioButton to some String but (since this is Java), String comparison can't be done by using `==` – Bö macht Blau Feb 10 '21 at 21:17

1 Answers1

1

Well your question is lil bit unclear that what you wanna do. If you have only one RadioButton, you do not need to use the RadioGroup rather simply use RadioButton but if you have more the one RadioButton you can place them inside RadioGroup.

Now, I am assuming you have more than one RadioButton inside RadioGroup. Try to do this

if(group.getCheckedRadioButtonId() == R.id.RadioButton1) {
    //RadioButton 1 Selected
    String chave = ((RadioButton) findViewById(R.id.RadioButton1)).getText().toString()
    if (chave.equalIgnoreCase("homem")){ 
       //...
    } else if (chave.equalIgnoreCase("melher")) {
       //...
    } 
} else {
   //RadioButton 2 Selected
}
Amir Raza
  • 2,320
  • 1
  • 23
  • 32
  • I have only one RadioGroup and I want my app to handle input with different parameters for each selection. If "homem" (man) does it one way, if "mulher" (woman) does it another way. – Alessander Feb 10 '21 at 22:41
  • I've tried one solution based with yours but the app is crashing when I check one of the radiobutton. I used this rule to if statement `if (group.getCheckedRadioButtonId() == R.id.radio_man)` – Alessander Feb 10 '21 at 23:07