tccpg288
tccpg288

Reputation: 3352

Radio Button Set Margins and Alignment Programmatically

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

Upvotes: 0

Views: 1345

Answers (2)

Gayathri
Gayathri

Reputation: 249

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();

Upvotes: 0

Ishaan Javali
Ishaan Javali

Reputation: 1713

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.

Upvotes: 1

Related Questions