zihadrizkyef
zihadrizkyef

Reputation: 1941

Textview maxlines depends on its height

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>

Upvotes: 2

Views: 2823

Answers (4)

MarkoR
MarkoR

Reputation: 553

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.

Upvotes: 2

yogesh lokhande
yogesh lokhande

Reputation: 1275

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

Upvotes: 1

Nitin Patel
Nitin Patel

Reputation: 1651

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

app:layout_constraintBottom_toBottomOf="@+id/ivPhotoProfile"

Upvotes: 2

Related Questions