Home » Android » android – Button changes position when text in two lines

android – Button changes position when text in two lines

Posted by: admin June 15, 2020 Leave a comment

Questions:

Hi I have this simple Layout

<?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:orientation="horizontal" >

<Button
    android:id="@+id/btn_trigger_left"
    style="@style/triggerButton"
    android:layout_marginRight="5dip"
    android:text="Mold" />

<Button
    android:id="@+id/btn_trigger_right"
    style="@style/triggerButton"
    android:text="Fums" />

</LinearLayout>

and style is here.

<style name="triggerButton">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">50dip</item>
    <item name="android:textSize">12sp</item>
    <item name="android:gravity">center</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:layout_weight">2</item>
    <item name="android:background">@drawable/symptom_bg</item>
    <item name="android:layout_marginBottom">0dip</item>
</style>

and out put is like this.
enter image description here

it is ok, but when My text is long enough to two lines button comes down like this.

enter image description here

please help me when I am doing wrong ?
Thanks!

How to&Answers:

Put

android:baselineAligned=”false”

in your LinearLayout.

LinearLayout aligns the baselines of all its child controls by default and here you need to disable the behaviour.

Answer:

 <item name="android:gravity">center</item>

to

<item name="android:gravity">top</item> 

Set gravity in your style.xml as this. Hope this will help.

Answer:

Fix maximum length of your text in Button

android:maxLength="20"

Hope this will help.