Android Radio Button color change after click action

100 views Asked by At
RadioGroup rg = new RadioGroup(context);
for (int i = 0; i < formElement.getRadioOptions().size(); i++) {
                RadioButton radiobutton1 = new RadioButton(context);
                radiobutton1.setText(formElement.getRadioOptions().get(i).getValue());
                radiobutton1.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.meetingNoteCL));
                radiobutton1.setHighlightColor(context.getResources().getColor(R.color.text_color));
                rg.addView(radiobutton1);
            }
            textInputLayout.setTypeface(FontUtils.getFontTypeRegular(context));
            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // checkedId is the RadioButton selected
                   
                    int radioButtonID = group.getCheckedRadioButtonId();
                    View radioButton = group.findViewById(radioButtonID);
                    int idx = group.indexOfChild(radioButton);
                    formElement.setKey(formElement.getRadioOptions().get(idx).getKey());
                    formElement.setValue(formElement.getRadioOptions().get(idx).getValue());

                }
            });

above code for radio button ., but after choose I need to change the particular radio button color alone to blue. can u help on this

1

There are 1 answers

1
return 0 On

The onCheckedChanged callback has a checkedId which you can use to get that particular button

You just need to assign your buttons unique ids before adding them to the group. Here is your your code modified to achieve that.

RadioGroup rg = new RadioGroup(context);
for (int i = 0; i < formElement.getRadioOptions().size(); i++) {
                RadioButton radiobutton1 = new RadioButton(context);

//set a unique id

radiobutton1.setId(View.generateViewId());
              radiobutton1.setText(formElement.getRadioOptions().get(i).getValue());
                radiobutton1.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.meetingNoteCL));
                radiobutton1.setHighlightColor(context.getResources().getColor(R.color.text_color));
                rg.addView(radiobutton1);
            }
            textInputLayout.setTypeface(FontUtils.getFontTypeRegular(context));
            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // checkedId is the RadioButton selected
                   
                    int radioButtonID = group.getCheckedRadioButtonId();
                    RadioButton radioButton = (RadioButton)group.findViewById(radioButtonID);

//if its checked then change it's tintlist

if(radioButton.isChecked()){
radioButton.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor(context,R.color.yourcolor)));
}
                    int idx = group.indexOfChild(radioButton);
                    formElement.setKey(formElement.getRadioOptions().get(idx).getKey());
                    formElement.setValue(formElement.getRadioOptions().get(idx).getValue());

                }
            });