Search code examples
androidandroid-layouttextviewline-breaksmultiline

How to make textview output the same as a multilined edittext?


I have an EditText on an activity that is for multilined comments. I then store this value into a database, which on a ListView activity I use a custom cursor adapter to show the value again.

bindView(View view, Context context, Cursor cursor) {
...
TextView comment = (TextView) view.findViewById(R.id.textView_comment);
comment.setText(cursor.getString(cursor.getColumnIndex("COMMENTS")));
...
}

example EditText value:

//user input into EditText
line 1
line 2
line 3

//stored value
line 1\nline 2\nline 3

//TextView
line 1

However when testing, upon opening the ListView only the first line of the comments TextView is visible. I have used multiple Log.v to verify that the line breaks ("\n") are there yet I can still only read the first line. On another stackoverflow thread, I saw that one solution was to do this:

bindView(View view, Context context, Cursor cursor) {
...
TextView comment = (TextView) view.findViewById(R.id.textView_comment);
String temp = cursor.getString(cursor.getColumnIndex("COMMENTS"));
comment.setText(temp.replace("\\n", "\n"));
...
}

However, the result is still only the first line of the comment (see above). Anyone know any other solution?

Edit: here's a shortened version of the listitem.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="1">

        ...

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Comments: "
            android:id="@+id/textView_raybae"
            android:layout_below="@+id/textView_out"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Thoughts"
            android:singleLine="false"
            android:id="@+id/textView_comment"
            android:layout_below="@+id/textView_out"
            android:layout_alignBottom="@+id/textView_raybae"
            android:layout_alignParentEnd="true"
            android:layout_toEndOf="@+id/textView_raybae" />

        ...

    </RelativeLayout>

</LinearLayout>

Solution

  • android:layout_alignBottom="@+id/textView_raybae"
    

    This line in combination with

    android:layout_below="@+id/textView_out"
    

    causes size of your textView_comment strict between bottom lines of thus TextViews. Try to delete:

    android:layout_alignBottom="@+id/textView_raybae"
    

    and change

    android:layout_below="@+id/textView_out"
    

    to

    android:layout_below="@+id/textView_raybae"