Long Android TextView pushes other views off-screen

I have two TextViews side by side. TextView1 has a different length of text, and TextView2 always says "+ #". When TextView1 becomes long, it forces TextView2 off the screen. Any ideas how to fix this? Here is my layout code:

<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/TextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="end" android:textSize="13sp"/> <TextView android:id="@+id/TextView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textSize="13sp"/> </RelativeLayout> 
+4
source share
3 answers

Actually this is what I tried to solve for a while. Unfortunately, the method others suggested - using layout_weight inside LinearLayout - doesn't actually work; however i found a solution for you!

 <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="left"> <TextView android:id="@+id/TextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/TextView2" android:singleLine="true" android:ellipsize="end" android:textSize="13sp"/> <TextView android:id="@+id/TextView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:singleLine="true" android:textSize="13sp"/> </RelativeLayout> 

In the block above, we use RelativeLayout to align the first TextView left of the second TextView . We will also align the second TextView on the right side of the parent ViewGroup . Finally, we add android:gravity="left" to the parent ViewGroup to align all TextView left.

As a result, both TextView will be side by side - regardless of the first length of the TextView . If you want the first TextView have multiple lines, just remove the android:ellipsize="end" tag.

Hope this is your expected result!

+23
source

Use LinearLayout with weight attributes in child views

 <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/TextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="end" android:textSize="13sp" android:layout_weight="1"/> <TextView android:id="@+id/TextView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textSize="13sp" android:layout_weight="0"/> </LinearLayout> 
0
source

This problem can be solved with android.support.constraint.ConstraintLayout

Here are the screenshots, so you can see how it behaves with small and long text.

Example 1: Small text

Example 2: Very long text

Here is the XML:

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="15dp"> <ScrollView android:id="@+id/scrollView" android:background="@drawable/white_rounded_rectangle" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:scrollbars="none"> <TextView android:id="@+id/messageTv" android:layout_width="match_parent" android:layout_height="wrap_content" android:fontFamily="sans-serif" android:lineSpacingExtra="6sp" android:paddingTop="15dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingBottom="70dp" android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever PageMaker including versions of Lorem Ipsum." android:textColor="#252525" android:textIsSelectable="true" android:textSize="@dimen/text_size16" /> </ScrollView> <android.support.constraint.ConstraintLayout android:id="@+id/bottomLayout" android:layout_width="match_parent" android:layout_height="84dp" android:background="@drawable/white_rounded_rectangle" app:layout_constraintBottom_toBottomOf="@+id/scrollView" app:layout_constraintEnd_toEndOf="@+id/scrollView" app:layout_constraintStart_toStartOf="@+id/scrollView"> <android.support.v7.widget.AppCompatButton android:id="@+id/btnAction1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginBottom="19dp" android:layout_weight="1" android:text="Action 1" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <android.support.v7.widget.AppCompatButton android:id="@+id/btnAction2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="17dp" android:layout_marginBottom="16dp" android:text="Action 2" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/btnAction1" /> </android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout> 
0
source

Source: https://habr.com/ru/post/987623/


All Articles