Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Inflated RadioButton layout_width does not match the layout file

I'm adding RadioButtons programmatically:

RadioButton radioButton = (RadioButton) inflater.inflate(R.layout.radio_button, null);
...
radioGroup.addView(radioButton);

In the RadioButton layout file, the width is match_parent:

         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:button="@null"
         android:drawableLeft="@drawable/check_selector"
         android:drawablePadding="@dimen/large"
         android:paddingBottom="@dimen/large"
         android:paddingTop="@dimen/large"
         android:textColor="@drawable/text_selector"

however after adding it to the RadioGroup it's width is wrap_content: "width: -2"

Timber.e("width: %s", radioGroupReason.getChildAt(radioGroupReason.getChildCount() -1).getLayoutParams().width);
like image 636
vvbYWf0ugJOGNA3ACVxp Avatar asked Jul 23 '26 13:07

vvbYWf0ugJOGNA3ACVxp


1 Answers

before adding view use it:

radioButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
like image 78
Haniyeh Khaksar Avatar answered Jul 25 '26 06:07

Haniyeh Khaksar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!