Supertiger
Supertiger

Reputation: 53

Using wrap_content without content going of the screen and without using layout_weight

So I have something like this:

LinearLayout:
   LinearLayout - wrap_content, background black:
      TextView - wrap_content
   LinearLayout:
      Image - 20dp
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center|start"
    android:padding="5dp"
    android:layout_margin="2dp">


    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="klgjdkljgilfdjgidfhjiogjdio;jhgiju;fgjdhjgu;hdfuighdfuk;ghduk;fhgukldhfgujkdfhyufhglfdhguldfhguklhfdulghldfughyulfhdulghdfulghul"
        android:textColor="#D5D5D5" />

    <LinearLayout
        android:layout_width="15dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_marginBottom="2dp"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_gravity="bottom|center"
            android:src="@drawable/edit" />
    </LinearLayout>
</LinearLayout>

This works perfectly but when the TextView contains text larger than the screen size, the image gets out of the screen. I don't want that to happen.

Here's the example:

Example Does anyone know how to fix this so it doesn't go outside the screen?

enter image description here

Thanks!

Upvotes: 1

Views: 231

Answers (1)

Dinesh
Dinesh

Reputation: 1670

Try this with ConstraintLayout

<androidx.constraintlayout.widget.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">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="klgjdkljgilfdjgidfhjiogjdio;jhgiju;fgjdhjgu;hdfuighdfuk;ghduk;fhgukldhfgujkdfhyufhglfdhguldfhguklhfdulghldfughyulfhdulghdfulghul"
        android:textColor="#D5D5D5"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@id/image"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>


    <ImageView
        android:id="@+id/image"
        android:layout_width="20dp"
        android:layout_height="10dp"
        android:layout_gravity="bottom|center"
        android:src="@drawable/edit"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/message"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Related Questions