Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add margin or padding to RadioButton into a RadioGroup programmatically

Tags:

java

android

I would like to add margin or padding to a RadioGroup programmatically but it not working.

RadioButton:

<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_fruit"
    android:button="@null"
    android:checked="false" />

RadioGroup:

<RadioGroup
            android:id="@+id/radio_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

</RadioGroup>

Java code:

RadioButton radioButtonView = (RadioButton) getActivity().getLayoutInflater().inflate(R.layout.radio_button, null);

radioGroup.addView(radioButtonView);

I tried to use LayoutParams and dividerPadding but it not works

example

like image 505
Johnny Avatar asked Dec 19 '16 10:12

Johnny


1 Answers

Try this

  RadioButton radioButtonView = (RadioButton) getLayoutInflater().inflate(R.layout.radio_button, null, false);
  RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  params.setMargins(15, 15, 15, 15);
  radioButtonView.setLayoutParams(params);
  radioGroup.addView(radioButtonView);
like image 61
ShekharKG Avatar answered Oct 14 '22 02:10

ShekharKG