how to divide the screen,s width with two buttons using match_parent. I give match_parent to a button then it fills the screen. I want two buttons to fill the screen horizontally.
Asked
Active
Viewed 1,202 times
0
-
1please use the searchbar on this site - http://stackoverflow.com/questions/2698817/linear-layout-and-weight-in-android – kellogs Oct 04 '12 at 04:52
1 Answers
4
You need to divide the width using the WEIGHT and not by wrap content / match parent etc...
So first of all take a Linear Layout give width as fill_parent, now add weighSum attribute as 2.
Now add 2 buttons with layout_width = 0dp
and add layout_weight = 1 in both the buttons....
The complete code is here :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Aamir Shah" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Aamir Shah" />
</LinearLayout>

Aamir Shah
- 4,473
- 8
- 21
- 28