Radio Button Set Margins and Alignment Programmatically





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am setting up a RadioGroup with RadioButtons, and they are all populated dynamically based on data in Firebase. I set the parameters as follows:



 mParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mParams.setMargins(0,0,4,6);


I then instantiate my RadioButton objects:



public void addRadioButtonsWithFirebaseAnswers(List<DocumentSnapshot> answers) {
mPollAnswerArrayList = new ArrayList<RadioButton>();
int indexCreated = 0;
for (DocumentSnapshot answerSnapshot : answers) {
Answer answer = answerSnapshot.toObject(Answer.class);
mPollAnswerArrayList.add((indexCreated), new RadioButton(mContext));
RadioButton radioButton = mPollAnswerArrayList.get(indexCreated);
radioButton.setTag(indexCreated);
radioButton.setText(answer.getAnswer().toString());
radioButton.setTextColor(getResources().getColor(R.color.black));
radioButton.setButtonDrawable(R.drawable.custom_btn_radio);
//TODO: Investigate if this line is necessary
radioButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.radio_button_answer_text_size));

mPollQuestionRadioGroup.addView(radioButton, mParams);
indexCreated++;
}
}


Below is how it is appearing. I want to add space between the radio button and the text. I also want space between each button. Finally, I would like the text centered with the button if possible.



enter image description here










share|improve this question





























    0















    I am setting up a RadioGroup with RadioButtons, and they are all populated dynamically based on data in Firebase. I set the parameters as follows:



     mParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    mParams.setMargins(0,0,4,6);


    I then instantiate my RadioButton objects:



    public void addRadioButtonsWithFirebaseAnswers(List<DocumentSnapshot> answers) {
    mPollAnswerArrayList = new ArrayList<RadioButton>();
    int indexCreated = 0;
    for (DocumentSnapshot answerSnapshot : answers) {
    Answer answer = answerSnapshot.toObject(Answer.class);
    mPollAnswerArrayList.add((indexCreated), new RadioButton(mContext));
    RadioButton radioButton = mPollAnswerArrayList.get(indexCreated);
    radioButton.setTag(indexCreated);
    radioButton.setText(answer.getAnswer().toString());
    radioButton.setTextColor(getResources().getColor(R.color.black));
    radioButton.setButtonDrawable(R.drawable.custom_btn_radio);
    //TODO: Investigate if this line is necessary
    radioButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.radio_button_answer_text_size));

    mPollQuestionRadioGroup.addView(radioButton, mParams);
    indexCreated++;
    }
    }


    Below is how it is appearing. I want to add space between the radio button and the text. I also want space between each button. Finally, I would like the text centered with the button if possible.



    enter image description here










    share|improve this question

























      0












      0








      0








      I am setting up a RadioGroup with RadioButtons, and they are all populated dynamically based on data in Firebase. I set the parameters as follows:



       mParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      mParams.setMargins(0,0,4,6);


      I then instantiate my RadioButton objects:



      public void addRadioButtonsWithFirebaseAnswers(List<DocumentSnapshot> answers) {
      mPollAnswerArrayList = new ArrayList<RadioButton>();
      int indexCreated = 0;
      for (DocumentSnapshot answerSnapshot : answers) {
      Answer answer = answerSnapshot.toObject(Answer.class);
      mPollAnswerArrayList.add((indexCreated), new RadioButton(mContext));
      RadioButton radioButton = mPollAnswerArrayList.get(indexCreated);
      radioButton.setTag(indexCreated);
      radioButton.setText(answer.getAnswer().toString());
      radioButton.setTextColor(getResources().getColor(R.color.black));
      radioButton.setButtonDrawable(R.drawable.custom_btn_radio);
      //TODO: Investigate if this line is necessary
      radioButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.radio_button_answer_text_size));

      mPollQuestionRadioGroup.addView(radioButton, mParams);
      indexCreated++;
      }
      }


      Below is how it is appearing. I want to add space between the radio button and the text. I also want space between each button. Finally, I would like the text centered with the button if possible.



      enter image description here










      share|improve this question














      I am setting up a RadioGroup with RadioButtons, and they are all populated dynamically based on data in Firebase. I set the parameters as follows:



       mParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      mParams.setMargins(0,0,4,6);


      I then instantiate my RadioButton objects:



      public void addRadioButtonsWithFirebaseAnswers(List<DocumentSnapshot> answers) {
      mPollAnswerArrayList = new ArrayList<RadioButton>();
      int indexCreated = 0;
      for (DocumentSnapshot answerSnapshot : answers) {
      Answer answer = answerSnapshot.toObject(Answer.class);
      mPollAnswerArrayList.add((indexCreated), new RadioButton(mContext));
      RadioButton radioButton = mPollAnswerArrayList.get(indexCreated);
      radioButton.setTag(indexCreated);
      radioButton.setText(answer.getAnswer().toString());
      radioButton.setTextColor(getResources().getColor(R.color.black));
      radioButton.setButtonDrawable(R.drawable.custom_btn_radio);
      //TODO: Investigate if this line is necessary
      radioButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.radio_button_answer_text_size));

      mPollQuestionRadioGroup.addView(radioButton, mParams);
      indexCreated++;
      }
      }


      Below is how it is appearing. I want to add space between the radio button and the text. I also want space between each button. Finally, I would like the text centered with the button if possible.



      enter image description here







      android xml radio-button






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 27 '18 at 1:26









      tccpg288tccpg288

      1,20211536




      1,20211536
























          2 Answers
          2






          active

          oldest

          votes


















          1














          this is an easy for how you can add a space between the button and text:



          radioButton.setText("   " + answer.getAnswer().toString());


          To change the distance between buttons, you can do:



          LayoutParams params = new LayoutParams(
          LayoutParams.WRAP_CONTENT,
          LayoutParams.WRAP_CONTENT
          );
          params.setMargins(leftMargin, topMargin, rightMargin, 15);
          radioButton.setLayoutParams(params);


          The 15 is the bottomMargin which you can change as you wish. I hope this helps.






          share|improve this answer
























          • Thanks - know how to align text center with button?

            – tccpg288
            Nov 27 '18 at 1:56











          • I'm sorry, I don't completely understand what you mean by that. In the picture the text is in the center.

            – Ishaan Javali
            Nov 27 '18 at 2:10











          • Right now, the bottom of the text is aligned with the bottom of the button. I would like for the middle of the text to be aligned with the middle of the button, if that clarifies

            – tccpg288
            Nov 27 '18 at 2:12



















          0














          Try this.



          LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
          ViewGroup.LayoutParams.WRAP_CONTENT);
          layoutParams.setMargins(10, 10, 10, 10); // leftMargin, topMargin, rightMargin, buttomMargin
          RadioGroup radioGroup = new RadioGroup(getContext());
          RadioButton radioButton = new RadioButton(getContext());
          radioButton.setLayoutParams(params1);
          radioButton.setId(1);
          radioButton.setText("text");
          radioButton.setPadding(0, 5, 0, 5); // leftMargin, topMargin, rightMargin, buttomMargin
          radioGroup.addView(radioButton);


          use this for check/select the radio button,



          radioGroup.check(3);  


          use this for unchecked the radio button,



          radioGroup.clearCheck();





          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',
            autoActivateHeartbeat: false,
            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%2f53491475%2fradio-button-set-margins-and-alignment-programmatically%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            this is an easy for how you can add a space between the button and text:



            radioButton.setText("   " + answer.getAnswer().toString());


            To change the distance between buttons, you can do:



            LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT
            );
            params.setMargins(leftMargin, topMargin, rightMargin, 15);
            radioButton.setLayoutParams(params);


            The 15 is the bottomMargin which you can change as you wish. I hope this helps.






            share|improve this answer
























            • Thanks - know how to align text center with button?

              – tccpg288
              Nov 27 '18 at 1:56











            • I'm sorry, I don't completely understand what you mean by that. In the picture the text is in the center.

              – Ishaan Javali
              Nov 27 '18 at 2:10











            • Right now, the bottom of the text is aligned with the bottom of the button. I would like for the middle of the text to be aligned with the middle of the button, if that clarifies

              – tccpg288
              Nov 27 '18 at 2:12
















            1














            this is an easy for how you can add a space between the button and text:



            radioButton.setText("   " + answer.getAnswer().toString());


            To change the distance between buttons, you can do:



            LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT
            );
            params.setMargins(leftMargin, topMargin, rightMargin, 15);
            radioButton.setLayoutParams(params);


            The 15 is the bottomMargin which you can change as you wish. I hope this helps.






            share|improve this answer
























            • Thanks - know how to align text center with button?

              – tccpg288
              Nov 27 '18 at 1:56











            • I'm sorry, I don't completely understand what you mean by that. In the picture the text is in the center.

              – Ishaan Javali
              Nov 27 '18 at 2:10











            • Right now, the bottom of the text is aligned with the bottom of the button. I would like for the middle of the text to be aligned with the middle of the button, if that clarifies

              – tccpg288
              Nov 27 '18 at 2:12














            1












            1








            1







            this is an easy for how you can add a space between the button and text:



            radioButton.setText("   " + answer.getAnswer().toString());


            To change the distance between buttons, you can do:



            LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT
            );
            params.setMargins(leftMargin, topMargin, rightMargin, 15);
            radioButton.setLayoutParams(params);


            The 15 is the bottomMargin which you can change as you wish. I hope this helps.






            share|improve this answer













            this is an easy for how you can add a space between the button and text:



            radioButton.setText("   " + answer.getAnswer().toString());


            To change the distance between buttons, you can do:



            LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT
            );
            params.setMargins(leftMargin, topMargin, rightMargin, 15);
            radioButton.setLayoutParams(params);


            The 15 is the bottomMargin which you can change as you wish. I hope this helps.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 27 '18 at 1:30









            Ishaan JavaliIshaan Javali

            1,3783821




            1,3783821













            • Thanks - know how to align text center with button?

              – tccpg288
              Nov 27 '18 at 1:56











            • I'm sorry, I don't completely understand what you mean by that. In the picture the text is in the center.

              – Ishaan Javali
              Nov 27 '18 at 2:10











            • Right now, the bottom of the text is aligned with the bottom of the button. I would like for the middle of the text to be aligned with the middle of the button, if that clarifies

              – tccpg288
              Nov 27 '18 at 2:12



















            • Thanks - know how to align text center with button?

              – tccpg288
              Nov 27 '18 at 1:56











            • I'm sorry, I don't completely understand what you mean by that. In the picture the text is in the center.

              – Ishaan Javali
              Nov 27 '18 at 2:10











            • Right now, the bottom of the text is aligned with the bottom of the button. I would like for the middle of the text to be aligned with the middle of the button, if that clarifies

              – tccpg288
              Nov 27 '18 at 2:12

















            Thanks - know how to align text center with button?

            – tccpg288
            Nov 27 '18 at 1:56





            Thanks - know how to align text center with button?

            – tccpg288
            Nov 27 '18 at 1:56













            I'm sorry, I don't completely understand what you mean by that. In the picture the text is in the center.

            – Ishaan Javali
            Nov 27 '18 at 2:10





            I'm sorry, I don't completely understand what you mean by that. In the picture the text is in the center.

            – Ishaan Javali
            Nov 27 '18 at 2:10













            Right now, the bottom of the text is aligned with the bottom of the button. I would like for the middle of the text to be aligned with the middle of the button, if that clarifies

            – tccpg288
            Nov 27 '18 at 2:12





            Right now, the bottom of the text is aligned with the bottom of the button. I would like for the middle of the text to be aligned with the middle of the button, if that clarifies

            – tccpg288
            Nov 27 '18 at 2:12













            0














            Try this.



            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(10, 10, 10, 10); // leftMargin, topMargin, rightMargin, buttomMargin
            RadioGroup radioGroup = new RadioGroup(getContext());
            RadioButton radioButton = new RadioButton(getContext());
            radioButton.setLayoutParams(params1);
            radioButton.setId(1);
            radioButton.setText("text");
            radioButton.setPadding(0, 5, 0, 5); // leftMargin, topMargin, rightMargin, buttomMargin
            radioGroup.addView(radioButton);


            use this for check/select the radio button,



            radioGroup.check(3);  


            use this for unchecked the radio button,



            radioGroup.clearCheck();





            share|improve this answer




























              0














              Try this.



              LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
              ViewGroup.LayoutParams.WRAP_CONTENT);
              layoutParams.setMargins(10, 10, 10, 10); // leftMargin, topMargin, rightMargin, buttomMargin
              RadioGroup radioGroup = new RadioGroup(getContext());
              RadioButton radioButton = new RadioButton(getContext());
              radioButton.setLayoutParams(params1);
              radioButton.setId(1);
              radioButton.setText("text");
              radioButton.setPadding(0, 5, 0, 5); // leftMargin, topMargin, rightMargin, buttomMargin
              radioGroup.addView(radioButton);


              use this for check/select the radio button,



              radioGroup.check(3);  


              use this for unchecked the radio button,



              radioGroup.clearCheck();





              share|improve this answer


























                0












                0








                0







                Try this.



                LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(10, 10, 10, 10); // leftMargin, topMargin, rightMargin, buttomMargin
                RadioGroup radioGroup = new RadioGroup(getContext());
                RadioButton radioButton = new RadioButton(getContext());
                radioButton.setLayoutParams(params1);
                radioButton.setId(1);
                radioButton.setText("text");
                radioButton.setPadding(0, 5, 0, 5); // leftMargin, topMargin, rightMargin, buttomMargin
                radioGroup.addView(radioButton);


                use this for check/select the radio button,



                radioGroup.check(3);  


                use this for unchecked the radio button,



                radioGroup.clearCheck();





                share|improve this answer













                Try this.



                LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(10, 10, 10, 10); // leftMargin, topMargin, rightMargin, buttomMargin
                RadioGroup radioGroup = new RadioGroup(getContext());
                RadioButton radioButton = new RadioButton(getContext());
                radioButton.setLayoutParams(params1);
                radioButton.setId(1);
                radioButton.setText("text");
                radioButton.setPadding(0, 5, 0, 5); // leftMargin, topMargin, rightMargin, buttomMargin
                radioGroup.addView(radioButton);


                use this for check/select the radio button,



                radioGroup.check(3);  


                use this for unchecked the radio button,



                radioGroup.clearCheck();






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 1 '18 at 7:48









                GayathriGayathri

                539




                539






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491475%2fradio-button-set-margins-and-alignment-programmatically%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







                    Popular posts from this blog

                    Tonle Sap (See)

                    I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                    Guatemaltekische Davis-Cup-Mannschaft