Ad

Flexible Button Width According Another Buttons Width

- 1 answer

I try to make 3 buttons with vertical orientation, the problem is the first button have the widest than others, How I can make two others button automatic follow the first button width?

enter image description here Should like this >enter image description here

Below is the example of my code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
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_horizontal|center_vertical"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="THIS IS JUST A EXAMPLE BUTTON"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BUTTON 2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BUTTON 3"/>
</LinearLayout>

</RelativeLayout>
Ad

Answer

Change your LinearLayout Like this, it will work fine

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="THIS IS JUST A EXAMPLE BUTTON" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="BUTTON 2" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="BUTTON 3" />
</LinearLayout>
Ad
source: stackoverflow.com
Ad