Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force next word to a new line if the word is too long for the textview

I have a TextView, that when filled programmatically will not properly line break on words.

Current Output:

This is what's happening:

| Hi I am an examp |
| le of a string t |
| hat is not break |
| ing correctly on |
| words            |

Expected Output:

I want this:

| Hi I am an       |
| example of a     |
| string that is   |
| breaking         |
| correctly on     |
| words            |

Java:

String mQuestion = "Hi I am an example of a string that is breaking correctly on words";
mTextView.setText(mQuestion);

XML:

<LinearLayout
    android:id="@+id/questionContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/questionHeaderTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/header_bg"
        android:paddingBottom="10dp"
        android:paddingLeft="25dp"
        android:paddingTop="10dp"
        android:text="@string/category_hint"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

        <Space
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".05" />

        <!-- THIS ONE WILL NOT WRAP !-->
        <TextView 
            android:id="@+id/questionHolderTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".9"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:text="@string/question_holder"
            android:singleLine="false"
            android:scrollHorizontally="false"
            android:ellipsize="none"
            android:textSize="20sp" />

        <Space
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".05" />

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginTop="5dp"
        android:background="@color/black" />
</LinearLayout>

<LinearLayout
    android:id="@+id/answerContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="visible" >
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="25dp"
    android:gravity="center" >

    <Button
        android:id="@+id/nextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:text="@string/next" />
</LinearLayout>

like image 206
i_me_mine Avatar asked Sep 03 '14 05:09

i_me_mine


People also ask

How do you get to the next line in TextView?

Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.

How do you start a new line in a text on Android?

Shift-Enter does work. Both android.messages.com and Verizon Wireless PC messaging software accept it.

How do I limit the number of lines in a TextView?

gpetuhov/textview_limit_text. txt. This will limit number of lines to 1 and if text exceeds limit, TextView will display ... in the end of line.


3 Answers

First you can get the text paint using TextView.getPaint(), then each time you add a new word(Hi, I, am, etc), call measureText on the paint. If the result length is longer than the available width of your TextView, add a \n before the new word.Reset the data and repeat the steps.

like image 177
suitianshi Avatar answered Oct 10 '22 04:10

suitianshi


I ended up using

private void initView() {
        Paint paint = new Paint();
        float width = paint.measureText(mQuestion);
        int maxLength = 300; // put whatever length you need here
        if (width > maxLength) {
            List<String> arrayList = null;
            String[] array = (mQuestion.split("\\s"));
            arrayList = Arrays.asList(array);
            int seventyPercent = (int) (Math.round(arrayList.size() * 0.70)); // play with this if needed
            String linebreak = arrayList.get(seventyPercent) + "\n";
            arrayList.set(seventyPercent, linebreak);
            mQuestion = TextUtils.join(" ", arrayList);
            mQuestion.replace(",", " ");
        }
        mQuestionHolderTextView.setText(mQuestion);
    }

I measure the string, turn it into a List, then split it at 70% and make a new line. Then I turn the List back into a String and remove the commas. As long as the word is no more than 30% of the remaining line you're in the clear, otherwise adjust accordingly.

It's quick and dirty, but it worked for me.

like image 2
i_me_mine Avatar answered Oct 10 '22 04:10

i_me_mine


android:breakStrategy="simple"

Add this in your Textview. It worked for me!

like image 7
Annie Aye Myat Avatar answered Oct 10 '22 04:10

Annie Aye Myat