Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long TextViews Pushes Element off the screen in LinearLayout

Tags:

android

I have three TextViews in a LinearLayout arranged one beside the other. However, if the text in the middle TextView becomes too long, the left TextView is pushed off the screen.

I do not want to use android:weight since I do not want to predefine a fix value for the ratio between the three TextViews.

How to fix it?

activity_main.xml:

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

    <TextView
        android:id="@+id/item_left"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:text="LEFT"
        android:gravity="center"
        android:layout_gravity="left|center_vertical" />

    <TextView
        android:id="@+id/item_middle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#aaaaaa"
        android:text="This is a very very long text which is so long that it wraps but unfortunatelly also pushes the TextView with ID item_right off the screen."
        android:layout_gravity="left|center_vertical" />

    <TextView
        android:id="@+id/item_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RIGHT"
        android:gravity="center"
        android:layout_gravity="right|center_vertical" />

</LinearLayout>
like image 459
Mark Avatar asked Dec 13 '25 23:12

Mark


1 Answers

Try by setting weights to each TextView on that way each TextView will have own space in LinearLayout and instead of pushing right TextView of screen text will wrap instead for example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
    android:id="@+id/item_left"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_weight="1"
    android:text="LEFT"
    android:gravity="center"
    android:layout_gravity="left|center_vertical" />

<TextView
    android:id="@+id/item_middle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#aaaaaa"
    android:text="This is a very very long text which is so long that it wraps but unfortunatelly also pushes the TextView with ID item_right off the screen."
    android:layout_gravity="left|center_vertical" />

<TextView
    android:id="@+id/item_right"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="RIGHT"
    android:gravity="center"
    android:layout_gravity="right|center_vertical" />

  </LinearLayout>

You don't need to give to each TextView equal space you can for example give to middle TextView more space manipulating weights for example: android:layout_weight="2" And wrapping all in ScrollView you will prevent issue of invisible text if text length is bigger then screen size.

like image 78
Yupi Avatar answered Dec 16 '25 15:12

Yupi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!