Search code examples
androidandroid-layoutuser-interfaceandroid-linearlayout

Layout Issue - Button not adjusting


Update

Now the button are of same size, but the the text in these button doesn't wrap completely and hides See the below screenshot for better understanding :- enter image description here

i am updating the below XML code with the code at I have done at this point of time. If anyone wanna to know about the changes I made in this code for previous question, check answer section, I implemented that.


Updated XML Code

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:padding="5dp"
        android:layout_weight="1">
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/ans_a"
            android:background="@drawable/play_page_btn"
            android:layout_weight="1"
            android:layout_marginEnd="2dp"
            android:textSize="17sp"
            android:padding="5dp"
            android:textAllCaps="false"
            android:drawableLeft="@drawable/alphabet_a"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/ans_b"
            android:background="@drawable/play_page_btn"
            android:layout_weight="1"
            android:layout_marginStart="2dp"
            android:padding="5dp"
            android:textSize="17sp"
            android:textAllCaps="false"/>
    </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:padding="5dp"
            android:layout_weight="1">
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:id="@+id/ans_c"
                android:background="@drawable/play_page_btn"
                android:layout_weight="1"
                android:layout_marginEnd="2dp"
                android:textSize="17sp"
                android:padding="5dp"
                android:textAllCaps="false"
                android:drawableLeft="@drawable/alphabet_a"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:id="@+id/ans_d"
                android:background="@drawable/play_page_btn"
                android:layout_weight="1"
                android:layout_marginStart="2dp"
                android:padding="5dp"
                android:textSize="17sp"
                android:textAllCaps="false"/>
        </LinearLayout>
    </LinearLayout>

Previous Question {Solved} I created a few buttons in which the text is downloaded from a server and then it is displayed on the button. I tried my best but I am not able to make them equal i.e.: whatever the size does the button with largest text takes, other button take the same shape. What it is happening now is, sometimes only upper button takes the shape, & sometimes only the lower ones. Can you help me setting this?

Preview when blank enter image description here

Text Loaded From Server but upper Button changes size and not lower enter image description here

Text Loaded From Server but lower Button changes size and not upper enter image description here


Solution

  • In your inner LinearLayouts, this

    android:layout_height="match_parent"
    

    has to be

    android:layout_height="0dp"
    

    So to make them change their heights according to their weights.