Showing posts with label RadioGroup. Show all posts
Showing posts with label RadioGroup. Show all posts

Wednesday, June 26, 2013

How to put an Image at the end of RadioButton - Android?


use the following code to add an image to top,right, left, bottom of a RadioButton (or any Button)


setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.tick), null);

someRadioButton.setCompoundDrawablesWithIntrinsicBounds(nullnull, getResources().getDrawable(R.drawable.tick), null);
If you pass null as a paramater for anything that are not needed.

How to set OnClickListener for a RadioButton - Android?


It is better to set OnClickListener for a RadioGroup and update the View accordingly,

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.someRadioGroup);        
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
            // Do something here..

        }
    });

How to get Selected Index or text of RadioGroup - Android?


The following code will get the selected Index of the RadioGroup and also get the text of the selected RadioButton,

int radioButtonID = someRadioGroup.getCheckedRadioButtonId();
View radioButton = someRadioGroup.findViewById(radioButtonID);
int idx = someRadioGroup.indexOfChild(radioButton);
RadioButton clickedRadioBut = (RadioButton) someRadioGroup.getChildAt(idx);
String selection = (String) clickedRadioBut.getText();