Android - How to add Radio Buttons to a Navigation Drawer
up vote
0
down vote
favorite
I'm trying to add some radio buttons to a Navigation Drawer.
This is what I've been trying so far :
activity_main_drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<group
android:id="@+id/radio_button_group"
android:checkableBehavior="single"
android:visible="true">
<item
android:id="@+id/radio_buttonX-axis"
android:icon="@drawable/ic_x_axis"
android:title="Show x-axis"
app:actionViewClass="android.widget.RadioButton"/>
<item
android:id="@+id/radio_buttonY-axis"
android:icon="@drawable/ic_y_axis"
android:title="Show y-axis"
app:actionViewClass="android.widget.RadioButton" />
<item
android:id="@+id/radio_buttonZ-axis"
android:icon="@drawable/ic_z_axis"
android:title="Show z-axis"
app:actionViewClass="android.widget.RadioButton" />
</group>
</menu>
Reference this and this answers.
Method 1: Add Radio Group after the Radio Buttons are created
Is this even possible?
int ids = {R.id.radio_buttonX_axis, R.id.radio_buttonY_axis, R.id.radio_buttonZ_axis};
RadioGroup radioGroup = new RadioGroup(navigationView.getContext());
radioGroup.setOrientation(RadioGroup.VERTICAL);
for (int id : ids) {
MenuItem menuItem = navigationView.getMenu().findItem(id);
RadioButton radioButton = (RadioButton) menuItem.getActionView();
radioGroup.addView(radioButton);
}
But it gives an error of :
The specified child already has a parent. You must call
removeView()
on the child's parent first.
But I wasn't able to figure out how to call it
Method 2: Add Radio Buttons to Navigation Drawer dynamically
navigationView.getMenu().removeGroup(R.id.radio_button_group);
RadioGroup radioGroup = new RadioGroup(navigationView.getContext());
for (int i = 0; i < 3; i++) {
RadioButton radioButton = new RadioButton(navigationView.getContext());
radioButton.setText("Show X - Axis");
radioGroup.addView(radioButton);
}
navigationView.addView(radioGroup);
But it doesn't works as expected (See Image).
I tried to add the radioGroup
to Menu
, but I couldn't figure out how to do it either. This is what I was trying to do :
Menu testMenu = navigationView.getMenu();
testMenu.add(R.id.radio_button_group, testMenu.findItem(R.id.radio_buttonX_axis).getActionView().getId(), 1, "Show X - Axis");
But it just adds an option to NavigationDrawer
, not a radio button.
Method 3
This is just hypothetical, I don't know if it's even possible or not.
- Copy the instances of all
radio_buttons
and store in someRadioButton
array. - Remove Group (as done in Method 2)
- Create a
RadioGroup
, and add all theRadioButtons
to it. - Add this new
RadioGroup
tonavigationView
But none has been helpful. Please tell me what to do?
android radio-button navigation-drawer radio-group
add a comment |
up vote
0
down vote
favorite
I'm trying to add some radio buttons to a Navigation Drawer.
This is what I've been trying so far :
activity_main_drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<group
android:id="@+id/radio_button_group"
android:checkableBehavior="single"
android:visible="true">
<item
android:id="@+id/radio_buttonX-axis"
android:icon="@drawable/ic_x_axis"
android:title="Show x-axis"
app:actionViewClass="android.widget.RadioButton"/>
<item
android:id="@+id/radio_buttonY-axis"
android:icon="@drawable/ic_y_axis"
android:title="Show y-axis"
app:actionViewClass="android.widget.RadioButton" />
<item
android:id="@+id/radio_buttonZ-axis"
android:icon="@drawable/ic_z_axis"
android:title="Show z-axis"
app:actionViewClass="android.widget.RadioButton" />
</group>
</menu>
Reference this and this answers.
Method 1: Add Radio Group after the Radio Buttons are created
Is this even possible?
int ids = {R.id.radio_buttonX_axis, R.id.radio_buttonY_axis, R.id.radio_buttonZ_axis};
RadioGroup radioGroup = new RadioGroup(navigationView.getContext());
radioGroup.setOrientation(RadioGroup.VERTICAL);
for (int id : ids) {
MenuItem menuItem = navigationView.getMenu().findItem(id);
RadioButton radioButton = (RadioButton) menuItem.getActionView();
radioGroup.addView(radioButton);
}
But it gives an error of :
The specified child already has a parent. You must call
removeView()
on the child's parent first.
But I wasn't able to figure out how to call it
Method 2: Add Radio Buttons to Navigation Drawer dynamically
navigationView.getMenu().removeGroup(R.id.radio_button_group);
RadioGroup radioGroup = new RadioGroup(navigationView.getContext());
for (int i = 0; i < 3; i++) {
RadioButton radioButton = new RadioButton(navigationView.getContext());
radioButton.setText("Show X - Axis");
radioGroup.addView(radioButton);
}
navigationView.addView(radioGroup);
But it doesn't works as expected (See Image).
I tried to add the radioGroup
to Menu
, but I couldn't figure out how to do it either. This is what I was trying to do :
Menu testMenu = navigationView.getMenu();
testMenu.add(R.id.radio_button_group, testMenu.findItem(R.id.radio_buttonX_axis).getActionView().getId(), 1, "Show X - Axis");
But it just adds an option to NavigationDrawer
, not a radio button.
Method 3
This is just hypothetical, I don't know if it's even possible or not.
- Copy the instances of all
radio_buttons
and store in someRadioButton
array. - Remove Group (as done in Method 2)
- Create a
RadioGroup
, and add all theRadioButtons
to it. - Add this new
RadioGroup
tonavigationView
But none has been helpful. Please tell me what to do?
android radio-button navigation-drawer radio-group
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to add some radio buttons to a Navigation Drawer.
This is what I've been trying so far :
activity_main_drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<group
android:id="@+id/radio_button_group"
android:checkableBehavior="single"
android:visible="true">
<item
android:id="@+id/radio_buttonX-axis"
android:icon="@drawable/ic_x_axis"
android:title="Show x-axis"
app:actionViewClass="android.widget.RadioButton"/>
<item
android:id="@+id/radio_buttonY-axis"
android:icon="@drawable/ic_y_axis"
android:title="Show y-axis"
app:actionViewClass="android.widget.RadioButton" />
<item
android:id="@+id/radio_buttonZ-axis"
android:icon="@drawable/ic_z_axis"
android:title="Show z-axis"
app:actionViewClass="android.widget.RadioButton" />
</group>
</menu>
Reference this and this answers.
Method 1: Add Radio Group after the Radio Buttons are created
Is this even possible?
int ids = {R.id.radio_buttonX_axis, R.id.radio_buttonY_axis, R.id.radio_buttonZ_axis};
RadioGroup radioGroup = new RadioGroup(navigationView.getContext());
radioGroup.setOrientation(RadioGroup.VERTICAL);
for (int id : ids) {
MenuItem menuItem = navigationView.getMenu().findItem(id);
RadioButton radioButton = (RadioButton) menuItem.getActionView();
radioGroup.addView(radioButton);
}
But it gives an error of :
The specified child already has a parent. You must call
removeView()
on the child's parent first.
But I wasn't able to figure out how to call it
Method 2: Add Radio Buttons to Navigation Drawer dynamically
navigationView.getMenu().removeGroup(R.id.radio_button_group);
RadioGroup radioGroup = new RadioGroup(navigationView.getContext());
for (int i = 0; i < 3; i++) {
RadioButton radioButton = new RadioButton(navigationView.getContext());
radioButton.setText("Show X - Axis");
radioGroup.addView(radioButton);
}
navigationView.addView(radioGroup);
But it doesn't works as expected (See Image).
I tried to add the radioGroup
to Menu
, but I couldn't figure out how to do it either. This is what I was trying to do :
Menu testMenu = navigationView.getMenu();
testMenu.add(R.id.radio_button_group, testMenu.findItem(R.id.radio_buttonX_axis).getActionView().getId(), 1, "Show X - Axis");
But it just adds an option to NavigationDrawer
, not a radio button.
Method 3
This is just hypothetical, I don't know if it's even possible or not.
- Copy the instances of all
radio_buttons
and store in someRadioButton
array. - Remove Group (as done in Method 2)
- Create a
RadioGroup
, and add all theRadioButtons
to it. - Add this new
RadioGroup
tonavigationView
But none has been helpful. Please tell me what to do?
android radio-button navigation-drawer radio-group
I'm trying to add some radio buttons to a Navigation Drawer.
This is what I've been trying so far :
activity_main_drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<group
android:id="@+id/radio_button_group"
android:checkableBehavior="single"
android:visible="true">
<item
android:id="@+id/radio_buttonX-axis"
android:icon="@drawable/ic_x_axis"
android:title="Show x-axis"
app:actionViewClass="android.widget.RadioButton"/>
<item
android:id="@+id/radio_buttonY-axis"
android:icon="@drawable/ic_y_axis"
android:title="Show y-axis"
app:actionViewClass="android.widget.RadioButton" />
<item
android:id="@+id/radio_buttonZ-axis"
android:icon="@drawable/ic_z_axis"
android:title="Show z-axis"
app:actionViewClass="android.widget.RadioButton" />
</group>
</menu>
Reference this and this answers.
Method 1: Add Radio Group after the Radio Buttons are created
Is this even possible?
int ids = {R.id.radio_buttonX_axis, R.id.radio_buttonY_axis, R.id.radio_buttonZ_axis};
RadioGroup radioGroup = new RadioGroup(navigationView.getContext());
radioGroup.setOrientation(RadioGroup.VERTICAL);
for (int id : ids) {
MenuItem menuItem = navigationView.getMenu().findItem(id);
RadioButton radioButton = (RadioButton) menuItem.getActionView();
radioGroup.addView(radioButton);
}
But it gives an error of :
The specified child already has a parent. You must call
removeView()
on the child's parent first.
But I wasn't able to figure out how to call it
Method 2: Add Radio Buttons to Navigation Drawer dynamically
navigationView.getMenu().removeGroup(R.id.radio_button_group);
RadioGroup radioGroup = new RadioGroup(navigationView.getContext());
for (int i = 0; i < 3; i++) {
RadioButton radioButton = new RadioButton(navigationView.getContext());
radioButton.setText("Show X - Axis");
radioGroup.addView(radioButton);
}
navigationView.addView(radioGroup);
But it doesn't works as expected (See Image).
I tried to add the radioGroup
to Menu
, but I couldn't figure out how to do it either. This is what I was trying to do :
Menu testMenu = navigationView.getMenu();
testMenu.add(R.id.radio_button_group, testMenu.findItem(R.id.radio_buttonX_axis).getActionView().getId(), 1, "Show X - Axis");
But it just adds an option to NavigationDrawer
, not a radio button.
Method 3
This is just hypothetical, I don't know if it's even possible or not.
- Copy the instances of all
radio_buttons
and store in someRadioButton
array. - Remove Group (as done in Method 2)
- Create a
RadioGroup
, and add all theRadioButtons
to it. - Add this new
RadioGroup
tonavigationView
But none has been helpful. Please tell me what to do?
android radio-button navigation-drawer radio-group
android radio-button navigation-drawer radio-group
asked Nov 11 at 13:51
Nikhil Wagh
376421
376421
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You can add a radiobuttons in the menu drawer. First create an xml layout (radio_button.xml):
<RadioGroup android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
</RadioGroup>
Then add it to the drawer:
<item android:title="Radios">
<menu>
<item
app:actionLayout="@layout/radio_button"
android:id="@+id/nav_radios"
android:title=""
/>
</menu>
</item>
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can add a radiobuttons in the menu drawer. First create an xml layout (radio_button.xml):
<RadioGroup android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
</RadioGroup>
Then add it to the drawer:
<item android:title="Radios">
<menu>
<item
app:actionLayout="@layout/radio_button"
android:id="@+id/nav_radios"
android:title=""
/>
</menu>
</item>
add a comment |
up vote
1
down vote
You can add a radiobuttons in the menu drawer. First create an xml layout (radio_button.xml):
<RadioGroup android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
</RadioGroup>
Then add it to the drawer:
<item android:title="Radios">
<menu>
<item
app:actionLayout="@layout/radio_button"
android:id="@+id/nav_radios"
android:title=""
/>
</menu>
</item>
add a comment |
up vote
1
down vote
up vote
1
down vote
You can add a radiobuttons in the menu drawer. First create an xml layout (radio_button.xml):
<RadioGroup android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
</RadioGroup>
Then add it to the drawer:
<item android:title="Radios">
<menu>
<item
app:actionLayout="@layout/radio_button"
android:id="@+id/nav_radios"
android:title=""
/>
</menu>
</item>
You can add a radiobuttons in the menu drawer. First create an xml layout (radio_button.xml):
<RadioGroup android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
</RadioGroup>
Then add it to the drawer:
<item android:title="Radios">
<menu>
<item
app:actionLayout="@layout/radio_button"
android:id="@+id/nav_radios"
android:title=""
/>
</menu>
</item>
answered Nov 11 at 15:19
armen
20625
20625
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249412%2fandroid-how-to-add-radio-buttons-to-a-navigation-drawer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown