I'm trying to add a button dynamically through code.
But i'm stuck that how to set android:layout_width
of button to 0dp
through code.
Android layout Width & Height In this code you can see layout_width property. This property is used to set the width of the given control. This property has following options:- Wrap_content :- when you set width as wrap_content then it will take the space according to the content.
A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time.
Try this :
LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
myButton.setLayoutParams(lp);
Import,
import android.view.ViewGroup.LayoutParams;
Thanks
Your button have layoutParams. Get those params, update the width and reapply them:
ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams)myButton.getLayoutParams();
lp.width = 0;
myButton.setLayoutParams(lp);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With