Zeon
Zeon

Reputation: 563

textview remove space after line break

I'm trying to make a chat card. How to remove space (right of green line) on TextView after line break?

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/m"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="102dp"
    android:background="@color/red_700"
    android:padding="8dp"
    android:text="Teaasdasdasdasdasdasdas dasdasdasdasdasdasdas dasasdasdasdasdasdasdasdxt"/>

Upvotes: 1

Views: 3102

Answers (4)

Juan Mendez Escobar
Juan Mendez Escobar

Reputation: 2767

Top Answer in Kotlin. Left and right paddings are needed otherwise the content appears cropped.

class TrimmedTextView(
    context: Context,
    attrs: AttributeSet
) : AppCompatTextView(context, attrs) {

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        var maxWidth = ceil(getMaxLineWidth(layout)).toInt()
        maxWidth += paddingRight + paddingLeft

        setMeasuredDimension(maxWidth, measuredHeight)
    }

    private fun getMaxLineWidth(layout: Layout): Float {
        var maximumWidth = 0.0f
        val lines = layout.lineCount
        for (i in 0 until lines) {
            maximumWidth = max(layout.getLineWidth(i), maximumWidth)
        }

        return maximumWidth
    }
}

Upvotes: 4

Planck Constant
Planck Constant

Reputation: 1524

You have to calculate width manually. So just create your own TextView

import android.content.Context;
import android.text.Layout;
import android.util.AttributeSet;

public class NewTextView extends android.support.v7.widget.AppCompatTextView  {

    public NewTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = (int) Math.ceil(getMaxLineWidth(getLayout()));
        int height = getMeasuredHeight();
        setMeasuredDimension(width, height);
    }

    private float getMaxLineWidth(Layout layout) {
        float maximumWidth = 0.0f;
        int lines = layout.getLineCount();
        for (int i = 0; i < lines; i++) {
            maximumWidth = Math.max(layout.getLineWidth(i), maximumWidth);
        }

        return maximumWidth;
    }
}

and then use it in your xml

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.xxx.NewTextView
        android:id="@+id/m"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="102dp"
        android:background="@color/colorPrimary"
        android:padding="8dp"
        android:text="Teaasdasdasdasdasdasdas dasdasdasdasdasdasdas dasasdasdasdasdasdasdasdxt"/>

</FrameLayout>

Upvotes: 6

jantursky
jantursky

Reputation: 1130

Official solution from Android docs:

Add dependencies:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:27.0.0'
    compile 'com.android.support:percent:27.0.0'
}

And change layout content to this:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/m"
        android:layout_height="wrap_content"
        android:background="@color/red_700"
        android:padding="8dp"
        app:layout_widthPercent="50%"
        android:text="Teaasdasdasdasdasdasdas dasdasdasdasdasdasdas dasasdasdasdasdasdasdasdxt"/>

</FrameLayout>

Upvotes: 0

Arshad
Arshad

Reputation: 1260

You can try this

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        >

        <TextView
            android:id="@+id/m"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_dark"
            android:padding="8dp"
            android:text="Teaasdasdasdasdasdasdas dasdasdasdasdasdasdas dasasdasdasdasdasdasdasdxt"
            />


    </LinearLayout>

</FrameLayout>

Upvotes: 1

Related Questions