Wrong TextView position in LinearLayout

Viewed 98

I have two TextView in LinearLayout:

<?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="wrap_content">

    <TextView
        android:id="@+id/left_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#eeeeee"
        android:gravity="center"
        android:text="Yes"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/right_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#eeeeee"
        android:gravity="center"
        android:text="Very long long long long long text"
        android:textColor="#000000" />

</LinearLayout>

It looks like this: enter image description here

If I remove android:gravity="center" it looks fine. But I need to use it. How to fix it?

4 Answers

Wrong TextView position in LinearLayout

You need to set android:gravity="center" in your LinearLayout

SAMPLE CODE

<?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="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/feedback_binary_left_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#eeeeee"
        android:gravity="center"
        android:text="Yes"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/feedback_binary_right_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#eeeeee"
        android:gravity="center"
        android:includeFontPadding="false"
        android:text="Very long long long long long text"
        android:textColor="#000000" />

</LinearLayout>

OUTPUT

enter image description here

Also check this if you want to shaw all the text

Try this

<?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="wrap_content"
    android:gravity="center">

    <TextView
        android:id="@+id/left_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#eeeeee"
        android:gravity="center"
        android:text="Yes"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/right_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#eeeeee"
        android:gravity="center"
        android:text="Very long long long long long text"
        android:textColor="#000000" />

</LinearLayout>

I added android:gravity="center" to parent LinearLayout

I suggest you use weight_sum in your LinearLayout and then use a match_parent width and height , than giving both EditText a weight=50 value or more or less , depends on the text you want .

Try this -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="16dp">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:text="@string/yes"
            android:textSize="18sp"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="@string/very_long_text"
            android:textSize="18sp"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


    </LinearLayout>

</LinearLayout>
Related