Set two buttons to same width regardless of screen size?
Asked Answered
R

10

49

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 ??

Reference answered 8/8, 2011 at 12:49 Comment(0)
M
104

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

Misdeem answered 8/8, 2011 at 12:53 Comment(0)
M
24

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.

Monocycle answered 14/7, 2014 at 20:21 Comment(3)
This answer is slept on hardAide
I didn't get weather its appreciation or sarcasm !Sheritasherj
I prefer using this method instead of using the weight attribute. It is definetely underrated.Bunt
P
6

set to each button:

android:layout_weight="0.5"
android:layout_width="0dp"
Primacy answered 8/8, 2011 at 12:52 Comment(0)
S
3
Display display=getWindowManager().getDefaultDisplay();
    int width=display.getWidth();
    btn1.setWidth(width/2);
    btn2.seTwidth(width/2);

Set anything in xml file then first find width of device then set width half to both button Now On every device they will look exactly same

Sheritasherj answered 9/8, 2011 at 10:18 Comment(2)
I think this solution goes against the spirit of defining the layout in an XML resource. Che Jami's solution is better.Antipas
This solution is the best for me better than xml. Thank you.Shipping
W
3
<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:text="One" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:text="Two" />
</LinearLayout>

These is the example for equal size buttons for side by side can be done from above code

android:layout_weight 

is used to assign space for buttons or whatever of equal amount for every child of LinearLayout.

Note: It works only on linear layout.

Watershed answered 9/12, 2017 at 19:26 Comment(2)
please add some more commentry to your answer. what did you cahnge? why is your answer right? what have you corrected? please look over your answer and add some more Details. thanks.Nakano
okay @Nakano tell me if you still want detailed description.Watershed
A
2
android:layout_weight="0.5"
android:layout_width="0dp

it's working

Avaunt answered 8/2, 2015 at 20:35 Comment(0)
M
1

Set android:layout_weight="1" in the containing layout The linear layout should have android:orientation set as horizontal. And then the inside buttons should have the following:

android:layout_width="0dp"

android:layout_weight="0.5"

Manducate answered 2/6, 2017 at 7:5 Comment(0)
B
1

This is working:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:text="my btn 1"/>
    <Button
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:text="my btn 2"/>
    </LinearLayout>
Band answered 13/12, 2018 at 10:58 Comment(1)
A comment on how / why your code solves the problem would go a long way to make it more helpful.Pepillo
E
-1

You can set them in linear layout nested in relative layout like this

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:orientation="vertical">



        <Button
            android:id="@+id/bestBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_marginBottom="5dp"
            android:textAllCaps="false"
            android:textSize="16sp"
            android:textColor="@color/grey_light2"
            android:background="@drawable/sharefb_btn"
            android:text="@string/sharefb" />
        <Button
            android:id="@+id/playBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/grey_light2"
            android:textAllCaps="false"
            android:textSize="16sp"
            android:layout_marginBottom="50dp"
            android:textStyle="bold"
            android:background="@drawable/puzzle_opening_btn_ncvtelen_3"
            android:text="@string/playagain" />

</LinearLayout>

and you will get somethiing like that

enter image description here

Episcopate answered 15/5, 2019 at 11:18 Comment(0)
C
-1

You can set android:layout_width="match_parent". by setting android:layout_marginHorizontal to a fixed value in a Layout android:orientation="vertical" will make the buttons same size.

Conny answered 20/12, 2021 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.