Reputation: 91
I have 2 static radio buttons in radio group in xml like this:
<RadioGroup
android:id="@id/rdg_feed_event"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/txt_feed_calendar"
android:layout_centerHorizontal="true"
**android:checkedButton="@id/rdb_feed_event"**
android:background="@drawable/radio_group_title"
android:divider="@drawable/divider_title"
android:orientation="horizontal"
android:showDividers="middle">
<RadioButton
android:id="@id/rdb_feed_event"
style="@style/Theme.RadioButton"
android:background="@drawable/radio_btn_left_title"
android:fontFamily="@string/font_family_normal"
android:text="@string/feed_event"
android:textColor="@drawable/radio_btn_text_gray_black"/>
<RadioButton
android:id="@id/rdb_feed_event_my"
style="@style/Theme.RadioButton"
android:background="@drawable/radio_btn_right_title"
android:fontFamily="@string/font_family_normal"
android:text="@string/feed_event_my"
android:textColor="@drawable/radio_btn_text_gray_black"/>
</RadioGroup>
This all is set to fragment in its onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = super.onCreateView(inflater, container, savedInstanceState);
mRdbEvent = (RadioButton) view.findViewById(R.id.rdb_feed_event);
mRdbEventMy = (RadioButton) view.findViewById(R.id.rdb_feed_event_my);
mRdgEvent = (RadioGroup) view.findViewById(R.id.rdg_feed_event);
..
When I firstly load app - the button "events" is checked and its shown in view and the expression mRdgEvent.getCheckedRadioButtonId() == mRdbEvent.getId()
returns true
. Then I tap on the second radio button and after that I tap to other menu which leads to another fragment(with FragmentTransaction.replace). After that I return to first fragment - again its onCreateView
method is launched and again mRdgEvent.getCheckedRadioButtonId() == mRdbEvent.getId()
in onCreateView
returns true
. But for this time the second radio button in view is checked, not the first. But the code shows true that the 1 one is checked - as it is declared in xml to be checked by default. How this can be? Thx in advance.
Upvotes: 0
Views: 917
Reputation: 91
I delayed the configuration of the button and it worked in onCreateView like this:
view.post(new Runnable() {
public void run() {
mRdgEvent.check(mRdbEvent.getId());
}
});
Upvotes: 1
Reputation: 1636
Add android:checked="true" in RadioButton not in RadioGroup
<RadioButton
android:id="@id/rdb_feed_event"
style="@style/Theme.RadioButton"
android:background="@drawable/radio_btn_left_title"
android:fontFamily="@string/font_family_normal"
android:text="@string/feed_event"
android:checked="true"
android:textColor="@drawable/radio_btn_text_gray_black"/>
Upvotes: 0