1

//this is my code below. i want to be able to get the user to tick one of the checkboxes 'left, 'centre', right' before they are able to go to "TEST 3". the button "NEXT" should not be pressed until one checkbox is ticked basically

public class Test2 extends AppCompatActivity {

private static Button button102;
private CheckBox checkBox1, checkBox2, checkBox3;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test2);

    checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
    checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
    checkBox3 = (CheckBox) findViewById(R.id.checkBox3);

    OnClickButtonListener102();
}

public void onCheckboxClicked(View view) {

    switch(view.getId()) {

        case R.id.checkBox1:

            checkBox2.setChecked(false);
            checkBox3.setChecked(false);

            break;

        case R.id.checkBox2:

            checkBox3.setChecked(false);
            checkBox1.setChecked(false);

            break;

        case R.id.checkBox3:

            checkBox1.setChecked(false);
            checkBox2.setChecked(false);

            break;
    }
}

//by clicking 'next' you reset the page
public void OnClickButtonListener102() {

    button102 = (Button) findViewById(R.id.button1002);
    button102.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("com.example.Test2");
                    startActivity(intent);
                }
            }
    );

}
}

1 Answers1

0

Declare the button as disabled

@Override
protected void onCreate(Bundle savedInstanceState) {
    button102 = (Button) findViewById(R.id.button1002);
    button102.setEnabled(false);
}

And enable the button when a radio button is clicked with Button.setEnabled(boolean):

button102 = (Button) findViewById(R.id.button1002);
button102.setEnabled(true);
switch(view.getId()) {
    case R.id.checkBox1:
        checkBox2.setChecked(false);
        checkBox3.setChecked(false);
        break;
    case R.id.checkBox2:
        checkBox3.setChecked(false);
        checkBox1.setChecked(false);
        break;
    case R.id.checkBox3:
        checkBox1.setChecked(false);
        checkBox2.setChecked(false);
        break;
}

Clarifications:

  • You can disable button by default in the XML layout
  • I don't see how you declare listener for radiobuttons but I'm guessing it can be only triggered by valid elements thats why I enable button always and not inside case statements
  • If you want to use button or radiobuttons several times you can declare it as class attributes and get object in onCreate or onStart steps

UPDATE:

there is one more problem, if i tick the box, and then untick it, it allows me to go to the next activity as the button is not dissabled anymore...how can i fix that?

FAST WAY? don't allow deselection if other is not selected.

button102 = (Button) findViewById(R.id.button1002);
button102.setEnabled(true);
switch(view.getId()) {
    case R.id.checkBox1:
        checkBox1.setChecked(true);
        checkBox2.setChecked(false);
        checkBox3.setChecked(false);
        break;
    case R.id.checkBox2:
        checkBox3.setChecked(false);
        checkBox2.setChecked(true);
        checkBox1.setChecked(false);
        break;
    case R.id.checkBox3:
        checkBox1.setChecked(false);
        checkBox2.setChecked(false);
        checkBox3.setChecked(true);
        break;
}

But if you want to select just ONE checkbox, you can make the logic like in this answer, but I would strongly recommend to change your CheckBox elements per RadioButtons that are designed for this,

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • thanks a million, you're a genious! there is only one problem tho, if i tick the box, and then untick it, it allows me to go to the next activity as the button is not dissabled anymore...how can i fix that? thanks again – Esidor Pashaj Nov 30 '15 at 14:44
  • yes it works perfectly now, thanks a million for all your help Jordi. im guessing you're spanish from your name...so HALA MADRID :D gracias amigo – Esidor Pashaj Nov 30 '15 at 14:59
  • Catalan and from Barcelona jejeje... but you're welcome anyway, also you must know, if this answer solved your issue you can mark it as accepted by clicking checkmark, both will receive some reputation ;) – Jordi Castilla Nov 30 '15 at 15:01
  • haha didn't know you are from barcelona! :p i ticked it, thanks for letting me know as i didnt know you could do that, im only new to this – Esidor Pashaj Nov 30 '15 at 15:04
  • glat to help again! sometimes is a bit hard to start here at stackoverflow hehehe.... best regards! – Jordi Castilla Nov 30 '15 at 15:08