Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set two buttons to same width regardless of screen size?

Ok, i have two buttons in linear layout:

<LinearLayout android:id="@+id/linearLayout1"                android:layout_height="wrap_content"                android:layout_width="fill_parent">         <Button android:id="@+id/aktiviraj_paket"                  android:text="Aktiviraj"                  android:layout_height="40sp"                  android:layout_width="160sp"                  android:background="@drawable/my_border3"                  android:onClick="myClickHandle"></Button>         <Button android:id="@+id/deaktiviraj_paket"                  android:text="Deaktiviraj"                  android:layout_height="40sp"                  android:layout_width="fill_parent"                  android:background="@drawable/my_border3"                 android:onClick="myClickHandle">         </Button> </LinearLayout> 

So the thing is, if I use fill parent on both buttons, they are one on each other, so i have made first button 160sp width, and second is fill_parent. If this is shown on 4 inch screen or smaller, buttons are the same size, but if i try this on tablet (10 inch) first button stays 160sp wide, and second is stretched till the end of screen (because fill_parent). Can i make this, so both buttons could be even size in no matter what size is the screen ??

like image 881
Goran Avatar asked Aug 08 '11 12:08

Goran


People also ask

How do I make two buttons the same size in CSS?

you need to set a width and height for your button and to centre text horizontally and vertically use the following: width:120px; height:50px; text-align:center; line-height:1.1em; font-size:1.1em; width and height can be set to match your desired measurements.

How do I make buttons equal width in CSS?

$('. button1'). css('width', w2 + "px");


2 Answers

Use android:layout_weight="1" on both Buttons. Set android:layout_width="0dp" on both. Since both buttons now have equal weighting, they will now each have half the parent's width.

You can find out more here: http://developer.android.com/guide/topics/ui/layout/linear.html

like image 179
Che Jami Avatar answered Sep 19 '22 04:09

Che Jami


If what you're looking to do is to make all the buttons the width of the widest button, setting weights isn't going to do that. Rather, you can put all the buttons in a TableLayout:

   <TableLayout          android:layout_width="wrap_content"         android:layout_height="wrap_content" >          <TableRow             android:layout_width="wrap_content"             android:layout_height="wrap_content" >              <Button                 android:id="@+id/button1"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="@string/short_text" />         </TableRow>          <TableRow             android:layout_width="wrap_content"             android:layout_height="wrap_content" >              <Button                 android:id="@+id/button2"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="@string/enter_manually" />         </TableRow>     </TableLayout> 

This layout will show 2 buttons, one on top of the other, the same width.

like image 36
user3838403 Avatar answered Sep 18 '22 04:09

user3838403