Reputation: 43
I tried to use a LinearLayout
with 4 buttons but the last one was cut. How can I edit my code to get button's width corresponding to all smartphones'resolutions
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RED" />
<Button
android:id="@+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BLUE" />
<Button
android:id="@+id/yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YELLOW" />
<Button
android:id="@+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GREEN" />
</LinearLayout>
Thanks
Upvotes: 0
Views: 54
Reputation: 807
Use weightSum
to get this equally divided layout by adjusting layout_weight
Try this once:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RED" />
<Button
android:id="@+id/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BLUE" />
<Button
android:id="@+id/yellow"
android:layout_width=match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="YELLOW" />
<Button
android:id="@+id/green"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GREEN" />
</LinearLayout>
</LinearLayout>
Upvotes: 0
Reputation: 8237
First,set android:weightSum="4"
in the LinearLayout
.
And set android:layout_width="Odp"
and android:layout_weight="1"
in your Button
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/red"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RED" />
<Button
android:id="@+id/blue"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BLUE" />
<Button
android:id="@+id/yellow"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="YELLOW" />
<Button
android:id="@+id/green"
android:layout_width="Odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GREEN" />
</LinearLayout>
</LinearLayout>
Upvotes: 0
Reputation: 3625
make width="0dp"
in all buttons and add
android:layout_weight="1"
to all buttons
Upvotes: 1