Reputation: 800
I was trying to check if a radiobutton was either checked or not and do some stuff in consequence when I found this weird behaviour which I can't understand. I must say the radiogroup is inside a PreferenceDialog which is my only explanation for the problem.
RadioGroup colorRG = (RadioGroup)view.findViewById(R.id.colorRG);
colorRG.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.w("ColorRG: ", "checkedId" + checkedId);
if (checkedId == (R.id.color_box1)){
Log.d("ColorRG: ","first if");
}
if (checkedId == (R.id.color_box1+5)){
Log.d("ColorRG: ","second if");
}
}
});
Well, the Log shows that the checkedId value is 2131165319 and R.id.color_box1 has a value of 2131165314. Obviously, it doesn't enter the first if. It shows the second if log.
So, what happening here?
I hope someone can help
EDIT: Adding xml code:
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:gravity="center_horizontal" >
<ImageView
android:id="@+id/color_box1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:contentDescription="@string/selector_descript"
android:src="@drawable/settings_selector_color" />
<ImageView
android:id="@+id/color_box2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:contentDescription="@string/selector_descript"
android:src="@drawable/settings_selector_color" />
<ImageView
android:id="@+id/color_box3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:contentDescription="@string/selector_descript"
android:src="@drawable/settings_selector_color" />
<ImageView
android:id="@+id/color_box4"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:contentDescription="@string/selector_descript"
android:src="@drawable/settings_selector_color" />
</LinearLayout>
<RadioGroup
android:id="@+id/colorRG"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:background="?android:attr/dividerVertical"/>
<RadioButton
android:id="@+id/color_radio1"
android:layout_marginLeft="40dp"
android:paddingLeft="70dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/selector_RB1" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:background="?android:attr/dividerVertical"/>
<RadioButton
android:id="@+id/color_radio2"
android:layout_marginLeft="40dp"
android:paddingLeft="70dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/selector_RB2" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:background="?android:attr/dividerVertical"/>
<RadioButton
android:id="@+id/color_radio3"
android:layout_marginLeft="40dp"
android:paddingLeft="70dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/selector_RB3" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:background="?android:attr/dividerVertical"/>
<RadioButton
android:id="@+id/color_radio4"
android:layout_marginLeft="40dp"
android:paddingLeft="70dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/selector_RB4" />
</RadioGroup>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:background="?android:attr/dividerVertical"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/pincancel_but"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackground"
android:text="@string/cancelar" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="4dip"
android:background="?android:attr/dividerVertical" />
<Button
android:id="@+id/pinok_but"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackground"
android:text="@string/ok" />
</LinearLayout>
</LinearLayout>
Upvotes: 0
Views: 317
Reputation: 288
try this it will help you
RadioGroup colorRG = (RadioGroup)view.findViewById(R.id.colorRG);
colorRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup,
int radioButtonID) {
if(radioButtonID==colorRg.getCheckedRadioButtonId)
{
Log.i("tt","firstlog"+radioButtonID);//you will get id now
}
}
}
});
Upvotes: 1
Reputation: 413
this is not the correct way
checkedId == (R.id.color_box1+5)
because id
is Auto-generated field R.java
which is generated automatically.You can not create id like that.
So, change the id
of second radio button
in xml
file. Suppose you give the id is
android:id="@+id/color_box5"
JAVA code will be
colorRG.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.w("ColorRG: ", "checkedId" + checkedId);
if (checkedId == (R.id.color_box1)){
Log.d("ColorRG: ","first if");
}
if (checkedId == (R.id.color_box5)){
Log.d("ColorRG: ","second if");
}
}
});
This will work for you.
Upvotes: 1