Textview With Too Long Strings

TextView with too long Strings

you can set

android:maxLines="1"
android:ellipsize="end"

to your textview, so the text which long than your textview will be hide.

TextView shows STRING_TOO_LARGE

As pointed out by the link that @HB. gave me, you cannot have a string that is larger than 32,767 bytes (encoded in UTF-8) in your APK file.

So, what I did, was to create a txt file in the assets folder named terms.txt and I put the string in it. Then, using the following function I converted the file to a String:

private String getTermsString() {
StringBuilder termsString = new StringBuilder();
BufferedReader reader;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("terms.txt")));

String str;
while ((str = reader.readLine()) != null) {
termsString.append(str);
}

reader.close();
return termsString.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

And I just assigned it to the TextView.

TextView and too long text

For example

1) you can popup your own custom dialog with whole text,

2) you can create a new activity with transparent BG with containing text,

3) you can expand you bar and show the whole text,

4) you can implement runnable\scrollable string in your header: Scrolling Textview in android

ConstraintLayout cuts off too long text in TextView

you need to change

  1. Change right text view width with 0dp. You are using wrap_content and because of that this issue generate.
  2. Change top to bottom constraint to all right textview, because when text increase it will overlap in your constraint.

    Sample Image

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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">

    <TextView
    android:id="@+id/tv_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:text="date"
    app:layout_constraintBaseline_toBaselineOf="@+id/tv_date_val"
    app:layout_constraintStart_toStartOf="parent" />

    <TextView
    android:id="@+id/tv_date_val"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:gravity="start"
    android:text="date text date text date text date text date text date text date text date text"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/tv_from_vall"
    app:layout_constraintTop_toTopOf="parent" />

    <TextView
    android:id="@+id/tv_from"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="start_point"
    app:layout_constraintStart_toStartOf="@+id/tv_date"
    app:layout_constraintTop_toTopOf="@+id/tv_from_vall" />

    <TextView
    android:id="@+id/tv_from_vall"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="8dp"
    android:gravity="start"
    android:text="start_point start_point start_point start_point start_point start_point start_point"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="@+id/tv_date_val"
    app:layout_constraintStart_toEndOf="@+id/tv_from"
    app:layout_constraintTop_toBottomOf="@+id/tv_date_val" />

    <TextView
    android:id="@+id/tv_to"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="end_point"
    app:layout_constraintStart_toStartOf="@+id/tv_from"
    app:layout_constraintTop_toBottomOf="@+id/tv_from_vall" />

    <TextView
    android:id="@+id/tv_to_vall"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:gravity="start"
    android:text="end_point end_point end_point end_point end_point end_point end_point"
    android:textSize="18sp"
    app:layout_constraintBaseline_toBaselineOf="@+id/tv_to"
    app:layout_constraintEnd_toEndOf="@+id/tv_date_val"
    app:layout_constraintStart_toStartOf="@+id/tv_from_vall" />

    </androidx.constraintlayout.widget.ConstraintLayout>

Move two side by side textviews to be one under another if text is too long

I have found a better solution. Changed my textviews into autoresizable textviews (more info here)

Also, each textview is in a separate layout, to make sure both textviews are resized to the same value.
My xml looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/value_linear_layout"
android:gravity="center">

<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
<com.mihaela.view.AutoResizeTextView
android:id="@+id/my_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">

<com.mihaela.view.AutoResizeTextView
android:id="@+id/my_text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

</LinearLayout>

and I have implemented the OnTextResizeListener from AutoResizeTextView to do this:

public class TextWidthResizeListener implements OnTextResizeListener {

@Override
public void onTextResize(TextView textView, float oldSize, float newSize) {
TextPaint paint = textView.getPaint();
if (paint.measureText(textView.getText().toString()) > (valueLinearLayout.getWidth() / 2)){
valueLinearLayout.setOrientation(LinearLayout.VERTICAL);
}
}
}

where valueLinearLayout is:

valueLinearLayout = (LinearLayout)findViewById(R.id.value_linear_layout);

This solution best fits for me, as the textviews are dimensioned when they are side by side until a minimum size. When the minimum size is reached, and the text still does not fit, the textviews will be aligned one under another.

Also, this idea with the listener can be applied to non-resizable textviews also.
I will set this answer as the correct one.

Too long TextView makes other Views disappear

I would use weight. Add android:weightSum to your LinearLayout with value 1.

For each element in your LinearLayout add weight. For example 0.8 for textview and 0.2 for Checkbox.

Then set width to 0dp for each element !

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">

<TextView
android:id="@+id/tv"
style="@style/Text"
android:layout_weight="0.8"
android:text="@string/multiple_sounds" />

<CheckBox
android:id="@+id/cb"
style="@style/Text"
android:layout_gravity="right"
android:layout_weight="0.2"/>

</LinearLayout>

And update your style :

    <style name="Text">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">0dp</item>
<item name="android:padding">@dimen/margin</item>
<item name="android:textSize">@dimen/text</item>
<item name="android:textAlignment">center</item>
</style>

Android TextView not show Complete long Text

Your text is too long try to put the text view in scroll

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.NestedScrollView    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_marginTop="40dp"    android:layout_marginBottom="40dp"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context="com.bala.computer.thenaliramanstories.ScrollingActivity">

<WebView android:id="@+id/txtStory" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/text_margin"/>
</android.support.v4.widget.NestedScrollView>

Best approach to set some long text on a TextView

Use mTextSample.setText(Html.fromHtml(text)); Where you can show anything in your TextView using Html.fromHtml().

Example (Some uses):

String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);

.

myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));

And some uses :

Android Html.fromHtml takes too long

Display HTML Formatted String

Happy coding :)



Related Topics



Leave a reply



Submit