Reputation: 69
I have 2 buttons, and I need to make them with same (biggest) width. Something like
if(button1.getWidth() > button2.getWidth){
button2.setWidth(button1.getWidth());
}
else{
button1.setWidth(button2.getWidth());
}
But when I try using this way it produce strange result, button height also change, and widths not the same.
When I use LayoutParams it doesn't work too
LinearLayout.LayoutParams = new LinearLayout.LayoutParams(button1.getWidth(),
button2.getHeight());
// Or ViewGroup.LayoutParams
Upvotes: 0
Views: 67
Reputation: 185
Put both buttons in LinearLayout and set buttons android:layout_width="0dp" and android:layout_weight="1"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2" />
</LinearLayout>
Upvotes: 1
Reputation: 4907
Use
Button.getLayoutParams().width
Button.getLayoutParams().height
instead of
Button.getWidth()
Button.getHeight()
to get the width height of Button.
and set the width of button1 equal to button2
button1.getLayoutParams().width = button2.getLayoutParams().width;
and vice versa.
Upvotes: 0
Reputation: 2644
Wrap them in a LinearLayout
and set the width
to 0dp
and the weight
to equal values like 1
. I suppose you can also do this programmatically.
Upvotes: 1