How to enable a button if two RadioGroups are checked?

44 views Asked by At

I really want to know how to enable a button and move on to the next activity but my problem is that i have 2 radioGroup and i don't know where do i have to set the Intent and setEnabled(). I think i should use a conditional but i'm not sure.

Code

So far I have this code

      radioGroup = findViewById(R.id.RadioGroup);
      radioGroupGM = findViewById(R.id.radioGroupGM);
    //Button
    siguiente = findViewById(R.id.buttonSiguiente);
     //RadioButton
      b1 = findViewById(R.id.radioButton);
      b2 = findViewById(R.id.radioButton2);
      b3 = findViewById(R.id.radioButton3);
      b4 = findViewById(R.id.radioButton4);
      b5 = findViewById(R.id.radioButto11);
      b6 = findViewById(R.id.radioButto22);
      b7 = findViewById(R.id.radioButto33);
      b8 = findViewById(R.id.radioButto44);
    cliente = new AsyncHttpClient();
       getSupportActionBar().setTitle("REPORTE");
       getSupportActionBar().setDisplayHomeAsUpEnabled(true);
       
    //---------------------------------------------------------------------------------------
    siguiente.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(  !b1.isChecked() || !b2.isChecked() || !b3.isChecked() || !b5.isChecked() || 
                !b6.isChecked() || !b7.isChecked() || !b8.isChecked()   ){

                Toast.makeText(Inventory.this, "choose a button", Toast.LENGTH_SHORT).show();

            }

        }
    });


    //---------------------------------------------------------------------------------------

 //---- --  ---   ---  --           THE PROBLEM ---  - - - -  ---- -- - -- -------
   radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(  b1.isChecked() || b2.isChecked() || b3.isChecked() || b4.isChecked()  ){

                siguiente.setEnabled(true);                                          // THIS
                  Intent siguiente = new Intent(Inventory.this, Siguiente.class);     // IS
                   startActivity(siguiente);                                       // THE PROBLEM
            }
        }
    });
    radioGroupGM.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(  b5.isChecked() || b6.isChecked() || b7.isChecked() || b8.isChecked()  ){

                siguiente.setEnabled(true);                                    //THIS
                Intent siguiente = new Intent(Inventory.this, Siguiente.class);// IS
                   startActivity(siguiente);                                   // THE PROBLEM

            }
        }
    });
2

There are 2 answers

0
unownsp On BEST ANSWER

You can check if any of the radio group has checked radio button.

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
    
                    if (radioGroup.getCheckedRadioButtonId() != -1 && radioGroupGM.getCheckedRadioButtonId() != -1) {
                        siguiente.setEnabled(true);
                    }
//do other activities
                }
            });

also do same in your next radio group

    radioGroupGM.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
        
                        if (radioGroup.getCheckedRadioButtonId() != -1 && radioGroupGM.getCheckedRadioButtonId() != -1) {
                            siguiente.setEnabled(true);
                        }
//do other activities
                    }
                });

now you can set your onclick listener for your button

siguiente.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //go to next intent
 Intent siguiente = new Intent(Inventory.this, Siguiente.class);// IS
                   startActivity(siguiente);
            }
        });

i think this should solve your problem. Happy Coding!!

0
Abhnav Maurya On

use && not ||

if you use && it will check all the condition and do stuff only when all is true. But in the case of || it will run when any of them is true.