swordguy8
swordguy8

Reputation: 157

Android: pass checkbox status to another activity and use in if statement

In my main activity, I have 6 checkboxes for selecting which parts of a test the person will want to take. I've looked at a bunch of pages trying to figure out what to do, but I couldn't gain a clear understanding of what I was supposed to do.

Let's say, in my main activity, I have the checkboxes:

            CheckBox grade1 = (CheckBox)findViewById(R.id.grade1);
            CheckBox grade2 = (CheckBox)findViewById(R.id.grade2);
            CheckBox grade3 = (CheckBox)findViewById(R.id.grade3);
            CheckBox grade4 = (CheckBox)findViewById(R.id.grade4);
            CheckBox grade5 = (CheckBox)findViewById(R.id.grade5);
            CheckBox grade6 = (CheckBox)findViewById(R.id.grade6);

And if they're checked, I want to pass them to my next activity using this onClick (with whatever code needs to be added to make it work):

private void setupMessageButton() {
    Button messageButton = (Button) findViewById(R.id.main_button);

    messageButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            startActivity(new Intent(MainActivity.this, Test.class));
        }
    });
}

Then in my new activity, I have if statements which are currently written this way:

if (grade1.isChecked()){
      // Do stuff
        }

I'm looking for the simplest way to do this. TL:DR - If a checkbox is checked in one activity, how can I use that status to do something in another activity with an if statement?

Upvotes: 0

Views: 8555

Answers (3)

poss
poss

Reputation: 1789

You can use Intent extras for this.

For example, if checkBox 1 is checked, you can put extra into intent like this.

Intent intent = new Intent(MainActivity.this, Test.class);
intent.putExtra("check1", true);
startActivity(intent);

In your Test.class you just retrieve the value.

Boolean check1 = getIntent().getExtras().getBoolean("check1");

Upvotes: 0

swordguy8
swordguy8

Reputation: 157

Using the answer from poss, I was able to figure this out. I have 6 checkboxes in my main activity in the onClick.

CheckBox grade1 = (CheckBox)findViewById(R.id.grade1);
CheckBox grade2 = (CheckBox)findViewById(R.id.grade2);
CheckBox grade3 = (CheckBox)findViewById(R.id.grade3);
CheckBox grade4 = (CheckBox)findViewById(R.id.grade4);
CheckBox grade5 = (CheckBox)findViewById(R.id.grade5);
CheckBox grade6 = (CheckBox)findViewById(R.id.grade6);

So I just need to pass them using the following code (also in the onClick):

Intent intent = new Intent(MainActivity.this, Test.class);
            intent.putExtra("grade1", grade1.isChecked());
            intent.putExtra("grade2", grade2.isChecked());
            intent.putExtra("grade3", grade3.isChecked());
            intent.putExtra("grade4", grade4.isChecked());
            intent.putExtra("grade5", grade5.isChecked());
            intent.putExtra("grade6", grade6.isChecked());
            startActivity(intent);

Then in my second activity, just like poss stated, these need to be at the top:

Boolean check1 = getIntent().getExtras().getBoolean("grade1");
Boolean check2 = getIntent().getExtras().getBoolean("grade2");
Boolean check3 = getIntent().getExtras().getBoolean("grade3");
Boolean check4 = getIntent().getExtras().getBoolean("grade4");
Boolean check5 = getIntent().getExtras().getBoolean("grade5");
Boolean check6 = getIntent().getExtras().getBoolean("grade6");

And for my if statements, I just do:

if (check1){
        // Do stuff
    }
if (check2){
        // Do more stuff
    }
if (check2){
        // Etc...
    }

Thanks for the help in solving this!!

Upvotes: 3

Raymond Lukanta
Raymond Lukanta

Reputation: 505

You can use intent.

In your MainActivity.java:

private void setupMessageButton() {
    Button messageButton = (Button) findViewById(R.id.main_button);

    messageButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, Test.class);
        intent.putExtra("selectedGrade", v.getId());              
        startActivity(intent);
    }
}

In your Test.java:

int selectedGrade = getIntent().getExtras().getint("selectedGrade");
switch (selectedGrade) {
    case R.id.grade1: //do something
        break;
    case R.id.grade2: //do something
        break;
    ....
}

Upvotes: 0

Related Questions