Haris Hasan
Haris Hasan

Reputation: 30107

How set width of one element equal to other in android?

I have two buttons defined in XML

 <Button
            android:layout_alignParentBottom="true"
            android:id="@+id/takepic_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Take_Pic"
            android:layout_gravity="left"
            android:layout_alignParentLeft="true"           
     />


     <Button
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:id="@+id/cancel_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Cancel"
            android:layout_gravity="right"    
     />

My question is how can I tell android that set the width of second button equal to whatever is the width of first button.

Update: I am looking for XML solution

Upvotes: 1

Views: 2920

Answers (4)

Android
Android

Reputation: 9033

btn1=(Button) findViewById(R.id.button1);
        btn2=(Button) findViewById(R.id.button2);     
        int i=btn1.getWidth();
        btn2.setWidth(i);

Upvotes: 2

martincm
martincm

Reputation: 13

In xml for two buttons sideby side in a horizontal layout use

android:layout_weight="1"

hope this helps

Upvotes: 0

grunk
grunk

Reputation: 14938

Use the android:layout_weight properties set to the same value for both of your button.

<Button
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"  
 />


 <Button
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"  
 />

Upvotes: 1

ngesh
ngesh

Reputation: 13501

If in code.. just do

button2.set(button1.getWidth());

Upvotes: 0

Related Questions