Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android textview break line behaviour (how to split the last word)

I'm writing an apply that pops texts on the screen and usually it is longer than 1 line. If the last word of a line is too long to fit in it, textview would just put it at the beginning of the next line. Is there a way to modify this behaviour? When the last word is a long one the results are horrible.

Basically I'd like to achieve something like this

|Lorem ipsum dolor sit amet, consecte|
|tur adipiscing elit. Lorem ipsum dol    |
|or sit amet, consectetur adipiscing     |
|elit.                                                     |

Instead of what I'm getting now.

|Lorem ipsum dolor sit amet,          |
|consectetur adipiscing elit. Lorem |
|ipsum dolor sit amet, consectetur |
| adipiscing elit.                             |

Thank you!

ADDITIONAL INFO

I have this textview that displays random quotes that have different lengths. What I need is a way to split the last word of every line.

I'm looking for a way to do something like this!

Here is the layout xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/title" />

<TextView
    android:id="@+id/quote"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/quote" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="refresh"
    android:text="@string/tellmemore" />

like image 940
pinolo Avatar asked Oct 05 '13 12:10

pinolo


People also ask

How do you strikethrough text in TextView?

If you want to show a strike-through text you can do it programming using PaintFlags. You can set paint flags Paint. STRIKE_THRU_TEXT_FLAG to a TextView and it will add a strike-through to the text. TextView textView = (TextView) findViewById(R.

How do I add a line break in android?

Add Line Breaks to a 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 I limit text in TextView?

you can extend the TextView class and overwrite the setText() function. In this function you check for text length or word cound. Better than counting the text length or the word cound a better way would be to use the "maxLines" attribute along with "ellipsize" attribute to attain the desired effect.


2 Answers

I decided to switch to a WebView because with HTML I'd have access to justification. You can read about it here and here and if you want to inject css this is a good tutorial.
If you are looking for hypenation maybe you can look into the Google's hypenator.js

So, to answer to myself here is the solution!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFD0" >

<WebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="refresh"
    android:text="@string/tellmemore" />

and in the activity

    ...
    WebView mWebView;
    @Override
    protected void onCreate(Bundle savedIstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedIstanceState);
        setContentView(R.layout.webview);
        mWebView = (WebView) findViewById(R.id.webview);
        load();}

        public void load() {
        // some other code to parse the string from a database
        String text = "<html> <head>" + tokens[1].toString()"</head><body>" + "<p align=\"justify\">"+ tokens[0].toString() + "</p> " + "</body></html>";       
        mWebView.loadData(text, "text/html", "utf-8");}
like image 51
pinolo Avatar answered Oct 02 '22 14:10

pinolo


Try this

 private String getWidthFitString(String input) {
    Paint paint = text.getPaint();
    // you can define max width by yourself
    int maxWidth = getContentMaxWidth();
    float width = paint.measureText(input);
    if (width > maxWidth) {
        List<String> words = Arrays.asList(input.split("\\s"));
        int breakLinePosition = 0;
        String toBreakLineText;
        List<String> toBreakLineWords = new ArrayList<>();
        while (breakLinePosition < words.size()) {
            toBreakLineWords.add(words.get(breakLinePosition));
            toBreakLineText = TextUtils.join(" ", toBreakLineWords);
            float currentWidth = paint.measureText(toBreakLineText);
            if (currentWidth > maxWidth) {
                break;
            }
            breakLinePosition ++;
        }
        if (breakLinePosition > 1) {
            toBreakLineWords.remove(toBreakLineWords.size() - 1);
            toBreakLineText = TextUtils.join(" ", toBreakLineWords);
            List<String> fromBreakLineWords = new ArrayList<>();
            for (int i = breakLinePosition; i < words.size(); i++) {
                fromBreakLineWords.add(words.get(i));
            }
            return toBreakLineText + "\n" + getWidthFitString(TextUtils.join(" ", fromBreakLineWords));
        } else {
            return input;
        }
    }
    return input;
}
like image 37
knighthedspi Avatar answered Oct 02 '22 14:10

knighthedspi