Search code examples
androidtextviewline-breaks

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" />

Solution

  • 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");}