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>
Use the following layout technique. Use
Linear Layout. Assignlayout_widthof your buttons to0dip. Assignlayout_weightas1to each button, so that they occupy equal of the total width. Assignlayout_heightof each button asmatch_parentPS: If you want them to occupy half screen height instead of width, change
orientationof layout toverticaland interchange values betweenlayout_widthandlayout_heightin above XML.About layout_weight
layout_weightdecides 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 assignlayout_weightof 1 button to “1” and the other button to “3” –> 1/(1+3) = 1/4 as you wanted. Again what space doeslayout_weightwork on – height or width?.layout_weightdoes the distribution from the free or unused space. In the above layout, we assigned0dipto 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 onlayout_weight. Hope this helps.