Android Custom Radio Not Checked

I created a custom radio group, i.e. a custom button.

<?xml version="1.0" encoding="utf-8"?> 

 <RadioGroup android:id="@+id/radio_group_rating" android:layout_width="match_parent" android:layout_height="150dp" android:orientation="horizontal" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal" android:weightSum="3" > <RadioButton android:id="@+id/radio_platinum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:clickable="true" android:drawablePadding="10dp" android:drawableTop="@drawable/star_small_dis" android:gravity="center" android:text="@string/platinum" /> <RadioButton android:id="@+id/radio_gold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:clickable="true" android:drawablePadding="10dp" android:drawableTop="@drawable/star_small_dis" android:gravity="center" android:text="@string/gold" /> <RadioButton android:id="@+id/radio_silver" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:clickable="true" android:drawablePadding="10dp" android:drawableTop="@drawable/star_small_dis" android:gravity="center" android:text="@string/silver" /> </LinearLayout> </RadioGroup> <Button android:id="@+id/btn_submit" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@id/radio_group_rating" android:text="@string/submit" /> 

And in action, I have the code below,

 final Dialog dialog = new Dialog(mActivity); dialog.setContentView(R.layout.rating_cust_dialog_layout); radioGroupRating = (RadioGroup) dialog.findViewById(R.id.radio_group_rating); Button btnSubmit = (Button) dialog.findViewById(R.id.btn_submit); btnSubmit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int selectedRadioId = radioGroupRating.getCheckedRadioButtonId(); View radioButton = radioGroupRating.findViewById(selectedRadioId); Integer selctedPosition = radioGroupRating.indexOfChild(radioButton); dialog.dismiss(); } }); dialog.show(); 

My problem is with disabled buttons. I thought about it because of android:button="@null" So, I replaced it with android:button="@drawable/star_dis" , but still didn't get a click.

0
android radio-button click onclick android-custom-view
Jan 20 '15 at 18:41
source share
2 answers

Its android: button = "@null", completely remove it from the xml file. Try using this simple xml layout.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RadioGroup android:id="@+id/radio_group_rating" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radio_platinum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="platinum" /> <RadioButton android:id="@+id/radio_gold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="gold" /> <RadioButton android:id="@+id/radio_silver" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="silver" /> </RadioGroup> <Button android:id="@+id/btn_submit" android:layout_width="match_parent" android:layout_height="50dp" android:text="submit" /> </LinearLayout> 
0
Jan 20 '15 at 7:20
source share

The only difference from my code is that I don't put clickable="true" in xml, if RadioGroup handles the touch event on its own to control the child switches, you should remove this attribute, since clickable is usually set to true automatically when you install OnClickListener .

0
Jan 21 '15 at 8:23
source share



All Articles