0

I want make 2 button take half of the screen width, but what ever i tru only one button came in screen and take full screenwidth. I wanted it for all resolution so dont want to make fix width. one button will take left half and other will take right half how it can do

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:gravity="center">


        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_gravity="left" />


        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button" 
            android:layout_gravity="right" />

    </LinearLayout>

</ScrollView>
Kishor datta gupta
  • 1,103
  • 2
  • 14
  • 42
  • This question has been asked a few times on stackoverflow - one example: http://stackoverflow.com/questions/11286147/android-setting-button-width-half-the-screen/11286169#11286169 - if you use the search you will find many more – Ben Pearson Jan 06 '13 at 18:54

2 Answers2

3

You can define layout_weight to define the size of a view. Using layout_width to be 0dp is the best way if used with weight settings (according to lint). When both buttons have the same value, both are using 50% width. Play around with the values to get a feeling for that.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button" 
        android:layout_weight="1" />

</LinearLayout>
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • perfect it solved the problem, but i cant understand the layout weight property, can u give me useful guide on this? – Kishor datta gupta Jan 06 '13 at 18:51
  • 1
    There are a lot of [questions](http://stackoverflow.com/questions/3995825/what-does-androidlayout-weight-mean) and answers. Just use the search. – WarrenFaith Jan 06 '13 at 18:53
2

Use the following layout technique. Use Linear Layout. Assign layout_width of your buttons to 0dip. Assign layout_weightas 1 to each button, so that they occupy equal of the total width. Assign layout_height of each button as match_parent

    <LinearLayout 
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/ButtonLeft"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="ButtonLeft"
            />
        <Button
            android:id="@+id/ButtonRight"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="ButtonRight"
            />

    </LinearLayout>

PS: If you want them to occupy half screen height instead of width, change orientation of layout to vertical and interchange values between layout_width and layout_height in above XML.

About layout_weight

layout_weight decides how the parent element space is divided between it's children. If you want 1:4 division of your width between your buttons, then you assign layout_weight of 1 button to "1" and the other button to "3" --> 1/(1+3) = 1/4 as you wanted. Again what space does layout_weight work on - height or width?. layout_weight does the distribution from the free or unused space. In the above layout, we assigned 0dip to the widths of the buttons. So the horizontal space or width of the parent is unused or free and that is divided between your buttons depending on layout_weight. Hope this helps.

sanjeev mk
  • 4,276
  • 6
  • 44
  • 69