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).



Improper Radio Buttons



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.



  1. Copy the instances of all radio_buttons and store in some RadioButton array.

  2. Remove Group (as done in Method 2)

  3. Create a RadioGroup, and add all the RadioButtons to it.

  4. Add this new RadioGroup to navigationView

But none has been helpful. Please tell me what to do?










share|improve this question

























    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).



    Improper Radio Buttons



    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.



    1. Copy the instances of all radio_buttons and store in some RadioButton array.

    2. Remove Group (as done in Method 2)

    3. Create a RadioGroup, and add all the RadioButtons to it.

    4. Add this new RadioGroup to navigationView

    But none has been helpful. Please tell me what to do?










    share|improve this question























      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).



      Improper Radio Buttons



      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.



      1. Copy the instances of all radio_buttons and store in some RadioButton array.

      2. Remove Group (as done in Method 2)

      3. Create a RadioGroup, and add all the RadioButtons to it.

      4. Add this new RadioGroup to navigationView

      But none has been helpful. Please tell me what to do?










      share|improve this question













      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).



      Improper Radio Buttons



      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.



      1. Copy the instances of all radio_buttons and store in some RadioButton array.

      2. Remove Group (as done in Method 2)

      3. Create a RadioGroup, and add all the RadioButtons to it.

      4. Add this new RadioGroup to navigationView

      But none has been helpful. Please tell me what to do?







      android radio-button navigation-drawer radio-group






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 13:51









      Nikhil Wagh

      376421




      376421






















          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>





          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            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

























            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>





            share|improve this answer
























              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>





              share|improve this answer






















                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>





                share|improve this answer












                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>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 15:19









                armen

                20625




                20625



























                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto