1

I have a textview which is its height depends on the size of height of imageview, and the image's width is 30% of screen size. I set the textview with maxlines=2 and ellipsize=end. It works good in Nexus S but not good in larger screen such us Nexus 7 because maxlines is keep 2 not larger. How to make maxlines larger depends on screen size?

Nexus S

Nexus S

Nexus 7

enter image description here

And this is the code

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_orientation="vertical">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Nama"
        android:textSize="24sp"
        android:textStyle="bold"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/ivPhotoProfile"
        android:layout_marginLeft="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintHorizontal_bias="0.0"/>

    <TextView
        android:id="@+id/tvUsername"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="-1dp"
        android:text="Username"
        android:textSize="12sp"
        app:layout_constraintLeft_toLeftOf="@+id/tvName"
        app:layout_constraintRight_toRightOf="@+id/tvName"
        app:layout_constraintTop_toBottomOf="@+id/tvName"/>

    <TextView
        android:id="@+id/tvBio"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="2dp"
        android:ellipsize="end"
        android:maxLines="2"
        android:text="@string/lorem_ipsum"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="@+id/ivPhotoProfile"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/tvUsername"
        app:layout_constraintRight_toRightOf="@+id/tvUsername"
        app:layout_constraintTop_toBottomOf="@+id/tvUsername"/>

    <ImageView
        android:id="@+id/ivPhotoProfile"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="8dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/guideline2"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/profile_picture_default"/>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.30625"/>

</android.support.constraint.ConstraintLayout>
zihadrizkyef
  • 1,849
  • 3
  • 23
  • 46

4 Answers4

2

Try with to remove below line from <TextView android:id="@+id/tvBio" ... xml:-

app:layout_constraintBottom_toBottomOf="@+id/ivPhotoProfile"
Nitin Patel
  • 1,605
  • 13
  • 31
2

Here is best solution for this problem that I found: https://stackoverflow.com/a/46904071/6476816

Worked for me, I had similar layout as you.

MarkoR
  • 543
  • 4
  • 12
1

For different Screen size you have to go for support multiple screens.

References :

https://developer.android.com/guide/practices/screens_support.html

Creating multi-screen support app android

http://mrbool.com/how-to-handle-multiple-screen-sizes-in-android/31291

Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
1

Don't set you maxlines just make your textview android:ellipsize="end" and align your textview bottom to imageview bottom it will automatically takes maxlines according to size of your textview and keep your textview aligned to your imageview

yogesh lokhande
  • 1,245
  • 1
  • 11
  • 20
  • alignedToBottom of image view so that it will change accordingly to imageviews height. – Nikhil Jadhav Aug 22 '17 at 06:04
  • 1
    if i don't set maxlines, then ellipsize doesn't work – zihadrizkyef Aug 24 '17 at 01:05
  • 1
    Then try to do something like this : ** android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:maxLines="5" android:minLines="2"** in which maxlines are the lines you want to set on large devices – yogesh lokhande Aug 24 '17 at 04:43