Reputation: 1103
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>
Upvotes: 0
Views: 449
Reputation: 4336
Use the following layout technique. Use Linear Layout
. Assign layout_width
of your buttons to 0dip
. Assign layout_weight
as 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.
Upvotes: 2
Reputation: 57672
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>
Upvotes: 3