0

I tried to look into all the possible answers but really I cannot figure how to proceed. I'm creating radio buttons into a radiogroup dinamically

for (int i = 0; i < keys.length; i++) {
        final RadioButton rdbtn = new RadioButton(this);
        rdbtn.setId(View.generateViewId());
        rdbtn.setText(keys[i]);
        rdbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedWH = rdbtn.getText().toString();
            }
        });
        mRgAllButtons.addView(rdbtn);
    }

and I would like to apply this style to the buttons

<RadioButton

            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginStart="60dp"
            android:layout_marginEnd="60dp"
            android:background="@drawable/radio_pressed"
            android:button="@android:color/transparent"                
            android:textAlignment="center"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp"
            android:textStyle="bold" />

Any help is really appreciated.

2 Answers2

0
ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(yourWidth, yourHeight);
    marginLayoutParams.leftMargin = yourLeftMargin;
    marginLayoutParams.rightMargin = yourRightMargin;
    rdbtn.setLayoutParams(marginLayoutParams);

add this in your code in order to set layout parameters to your view which you are creating dynamically you must create LayoutParams for it. and there you can set margins to whatever you want.

0

This is a simple way. You can create a RadioButton view instance from the XML so it will have the style that you want.

  1. Create XML layout with RadioButton element and the style - just what you have mentioned.
  2. Use that layout to create a new RadioButton View (and add it to the RadioGroup).

 LayoutInflater inflater = LayoutInflater.from(context);

 for (int i = 0; i < keys.length; i++) {
    final RadioButton rdbtn = inflater.inflate(R.layout.layout_name, mRgAllButtons, false);
    // mRgAllButtons - parent of this view.
    // attachToParent - false - add to the parent manually.
    rdbtn.setId(View.generateViewId());
    rdbtn.setText(keys[i]);
    rdbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectedWH = rdbtn.getText().toString();
        }
    });
    mRgAllButtons.addView(rdbtn);
}
Froyo
  • 17,947
  • 8
  • 45
  • 73
  • I'm almost there with your solution, The only problem is that the setText is ignored (at least graphically, because the content of the string is indeed there [I can see it when I click the button]) – Massimiliano Apr 08 '20 at 14:53
  • Try changing the color in your xml file. Maybe it's conflicting with the background color. – Froyo Apr 08 '20 at 15:00
  • tried, but it did not change, I'm trying to find a possible solution following this [link](https://stackoverflow.com/questions/47541214/text-isnt-showing-after-using-settext-to-textview-in-inflated-layout) – Massimiliano Apr 08 '20 at 15:13
  • I think something was in the cache because I closed, reopened, rebuilt and now it works. Thanks again, answer accepted – Massimiliano Apr 08 '20 at 15:19