Let's Learnit Let's Learnit - 2 months ago 17
Java Question

Not able to get id of radiobutton via getCheckedRadioButtonId()

I am trying to make a custom dialog box which gives radio buttons to select from n then diplay text of radio button in the textfield. This is what i tried..

My Layout for dialog box is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RadioGroup
android:id="@+id/rg_carType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">

<RadioButton
android:id="@+id/rb_hatchD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hatchback(Diesel)" />

<RadioButton
android:id="@+id/rb_hatchP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hatchback(Petrol)" />

<RadioButton
android:id="@+id/rb_sedanD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sedan(Diesel)" />

<RadioButton
android:id="@+id/rb_sedanP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sedan(Petrol)" />

<RadioButton
android:id="@+id/rb_suv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUV" />
</RadioGroup>

<ImageView
android:id="@+id/iv_carType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_toRightOf="@+id/rg_carType"
/>

<Button
android:id="@+id/btn_confirmCar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rg_carType"
android:layout_centerHorizontal="true"
android:background="@drawable/button_2"
android:text="Confirm"
android:textColor="#fff" />




And the function for custom dialog box is

private void diplayCartype() {
final Dialog dialog_car = new Dialog(FinalActivity.this);
final RadioButton rb_select;
RadioGroup rg_carType;
ImageView iv_carType;
Button btn_confirmCar;
String sCar="?";
dialog_car.setTitle("Select Car Type");

dialog_car.setContentView(R.layout.dialog_cartype);

iv_carType=(ImageView)dialog_car.findViewById(R.id.iv_carType);
btn_confirmCar=(Button)dialog_car.findViewById(R.id.btn_confirmCar);
rg_carType=(RadioGroup)dialog_car.findViewById(R.id.rg_carType);

int selected=rg_carType.getCheckedRadioButtonId();
rb_select=(RadioButton)dialog_car.findViewById(selected);

switch(selected)
{
case R.id.rb_hatchD:
iv_carType.setImageResource(R.drawable.hatch_d);
sCar=rb_select.getText().toString();
break;
case R.id.rb_hatchP:
iv_carType.setImageResource(R.drawable.hatch_p);
sCar=rb_select.getText().toString();
break;
case R.id.rb_sedanD:
iv_carType.setImageResource(R.drawable.sedan_d);
sCar=rb_select.getText().toString();
break;
case R.id.rb_sedanP:
iv_carType.setImageResource(R.drawable.sedan_p);
sCar=rb_select.getText().toString();
break;
case R.id.rb_suv:
iv_carType.setImageResource(R.drawable.suv);
sCar=rb_select.getText().toString();
break;
default:
iv_carType.setImageResource(R.drawable.suv);
sCar="default";
}
final String finalSCar = sCar;
btn_confirmCar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv_carType.setText(finalSCar);
dialog_car.dismiss();
}
});
dialog_car.show();
}


The problem is that the switch case executes only default condition, that means its not getting id to check. Thanks in advance.. :)

Answer

About getCheckedRadioButtonId()

Returns the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.

There is no selected RadioButton at the moment when you are trying to check for selected one.

dialog_car.setContentView(R.layout.dialog_cartype);

// at this point you have newly created view hierarchy and
// RadioGroup has no selected items

iv_carType=(ImageView)dialog_car.findViewById(R.id.iv_carType);
btn_confirmCar=(Button)dialog_car.findViewById(R.id.btn_confirmCar);
rg_carType=(RadioGroup)dialog_car.findViewById(R.id.rg_carType);

// still has no selected items, next call returns -1 as well
int selected=rg_carType.getCheckedRadioButtonId();

You should use getCheckedRadioButtonId() from handler of some extra button (e.g. "OK") to be sure that user has seen your radio buttons and has enough time to select something.

Comments