32

I'm writing an Activity in android where I have two radio buttons under a RadioGroup. One of them is checked by default. But I can't trigger the event in onCreate method so that I can do something in that. The onCheckedChanged is running fine when clicked on.

RadioGroup ItemtypeGroup = (RadioGroup) findViewById(R.id.rechargeItemtype);
    RadioButton providerRadio = (RadioButton) findViewById(R.id.a);
    providerRadio.performClick();

    ItemtypeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged (RadioGroup group,int checkedId){

            Log.d("chk", "id" + checkedId);

            if (checkedId == R.id.a) {
                //some code
            } else if (checkedId == R.id.b) {
                //some code
            }
        }
    });
musica
  • 1,373
  • 3
  • 15
  • 34
user2534310
  • 459
  • 1
  • 5
  • 10

6 Answers6

48

To fire a radio check box for the default when initializing. Set everything to unchecked with the clearCheck method, then set the handler, then set the default and your handler will fire.

itemtypeGroup.clearCheck();

then same as usual add a listener...

itemtypeGroup
        .setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.d("chk", "id" + checkedId);

                if (checkedId == R.id.a) {
                    //some code
                } else if (checkedId == R.id.b) {
                    //some code
                }

            }

        });

Then check the default radio button and your listener will fire.

rb = (RadioButton) view.findViewById(R.id.a);
rb.setChecked(true);

Good Luck

danny117
  • 5,581
  • 1
  • 26
  • 35
  • What does this "rb = (RadioButton) view.findViewById(R.id.a);" mean? Do we have to declare inside onCheckedChanged? – Karan Thakkar May 09 '17 at 05:59
  • Three steps. Clear all, wire up handler, set default. Because the default is set after the listener is wired up the listener code will run. – danny117 May 09 '17 at 11:37
  • I am sorry for the downvote - it was inadvertent. I tried to correct it by clicking upvote, but I could not. If you know of a way to correct the downvote, please let me know. – stevehs17 Jul 30 '17 at 07:14
  • NP don't worry about it. – danny117 Aug 01 '17 at 17:40
  • Suppose I have three radio groups, then how would I handle the listener for three of them? – Jorvis Jul 14 '18 at 07:32
  • 1
    @Jorvis For three groups+ I might use a switch statement on checkedID. Plan all of the checkedIds are unique in the APP. I might use separate handlers depends on app I suppose. – danny117 Jul 27 '18 at 18:24
10
 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if (checkedId == R.id.radioButton1) {
             //do work when radioButton1 is active
            } else  if (checkedId == R.id.radioButton2) {
             //do work when radioButton2 is active
          } else  if (checkedId == R.id.radioButton3) {
             //do work when radioButton3 is active
          }

        }
    });

this work for me . hope is helpfull

Maysam R
  • 915
  • 10
  • 12
  • Suppose I have three radio groups, then how would I handle the listener for three of them? – Jorvis Jul 14 '18 at 07:31
  • So you mean I should implement three different listeners along with three onCheckedChanged methods for three radio groups. – Jorvis Jul 16 '18 at 06:04
  • no. you must be use one Listener and check which one selected in listener. – Maysam R Jul 20 '18 at 10:01
6

Try this Code.

Declare this code in your onCreate method.

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
    radioGroup.clearCheck();
    radioGroup.setOnCheckedChangeListener(new  RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            RadioButton rb = (RadioButton) group.findViewById(checkedId);
            if (null != rb && checkedId > -1) {

                // checkedId is the RadioButton selected
                switch (checkedId) {
                    case R.id.one:
                     // Do Something
                      break;

                    case R.id.two:
                    // Do Something
                      break;   

                    case R.id.three:
                    // Do Something   
                        break;
                }
            }
        }

    });

Hope this helps.

The Hawk
  • 121
  • 2
  • 4
3

The problem is because, you are setting OnCheckedChangeListener on RadioGroup after the providerRadio.performClick(); method call.

That's why when providerRadio.performClick(); method executes till then their is no OnCheckedChangeListener available to call. So, to make it work, you need to set Listener before the call to performClick() method.

And instead of using RadioButton.performClick() you should use RadioGroup.check() method to change the current checked RadioButton.

So you can use this code to change current selection of RadioGroup

RadioGroup ItemtypeGroup = (RadioGroup) findViewById(R.id.rechargeItemtype);
ItemtypeGroup
        .setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Log.d("chk", "id" + checkedId);

                if (checkedId == R.id.a) {
                    //some code
                } else if (checkedId == R.id.b) {
                    //some code
                }

            }

        });
ItemtypeGroup.check(R.id.a);

Note:- Make sure to call RadioGroup.check() method after adding OnCheckedChangeListener as above in code snippet.

Akhilesh Kumar
  • 796
  • 1
  • 5
  • 12
0

I guess you are performing click before setting listener to the group, that's why event is not triggered.

Ruman
  • 191
  • 1
  • 8
0

Even though @dannys answer is ok I would like to have the default value taken from the XML layout definition file rather then setting it in code.

That's why I use this approach to:

  1. store the default value form the radio group
  2. clear all check's
  3. initialize the OnCheckedChangeListener
  4. check the default again (which was stored previously) which triggers the listener

This could look like this in code with the first_radio_button as the default radio button:

// call this in any init method
_myRadioGroup    = _myViewContainingTheRadioGroup.findViewById(R.id.my_radio_group);
int defaultValue = _myRadioGroup.getCheckedRadioButtonId();
_myRadioGroup.clearCheck();
_myRadioGroup.setOnCheckedChangeListener(_myRadioGroupCheckedChangeListener);
_myRadioGroup.check(defaultValue);

Place the OnCheckedChangeListener somewhere in your class:

private RadioGroup.OnCheckedChangeListener _myRadioGroupCheckedChangeListener = new RadioGroup.OnCheckedChangeListener()
{
  @Override
  public void onCheckedChanged(RadioGroup group, int checkedId)
  {
    switch(group.findViewById(checkedId).getId())
    {
      case R.id.first_radio_button:
        // do stuff
        break;
      case R.id.second_radio_button:
        // do stuff
        break;
      // ...
    }
  }
};

This is my radio group xml

  <RadioGroup
    android:id="@+id/my_radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
      android:id="@+id/first_radio_button"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:checked="true"
      android:text="@string/first_radio_button_text"/>

    <RadioButton
      android:id="@+id/second_radio_button"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:text="@string/second_radio_button_text"/>

    // ...

  </RadioGroup>
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92