0

Basically I am inserting text along with new line character in mysql db like this:

$message = 'Hello World' . "\n";
$message .= 'This is a test' . "\n";
$message .= 'Thanks;

When saving message in db, I have tried setting value of new line as \n, \\n, \\\n, \\\\n and even <br> instead of \n then used fromHtml on android app but nothing working out.

In db, I can see there is new line.

On Android app I want to have new line character if any showed in TextView, I have tried various things but nothing is working, I've tried things like:

String message = m.getMessage().toString();
  message = message.replaceAll("\\n",
      System.getProperty("line.separator"));

Here instead of "\\n", I have also tried "\\\n", "\n" and even "\\\\n"

And:

Spanned html = Html.fromHtml(m.getMessage());

And:

message.replaceAll("\n","<br />");
message.replaceAll("\r\n","<br />");
message.replaceAll("\r","<br />");

And:

android:singleLine="false"

With various things tried, in TextView I get text like these permutations:

Hello WorldThis is a testThanks

Hello World\This is a test\Thanks

Hello World\nThis is a test\nThanks

Here is my complete code:

public class MessageListAdapter extends BaseAdapter {

    private Context context;
    private List<ListMessage> messagesItems;

    public MessageListAdapter(Context context, List<ListMessage> navDrawerItems) {
        this.context = context;
        this.messagesItems = navDrawerItems;
    }

    @Override
    public int getCount() {
        return messagesItems.size();
    }

    @Override
    public Object getItem(int position) {
        return messagesItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        /**
         * The following list not implemented reusable list items as list items
         * are showing incorrect data Add the solution if you have one
         * */

        ListMessage m = messagesItems.get(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        // Identifying the message owner
        if (messagesItems.get(position).isSelf()) {
            // message belongs to you, so load the right aligned layout
            convertView = mInflater.inflate(R.layout.list_item_message_right,
                    null);
        } else {
            // message belongs to other person, load the left aligned layout
            convertView = mInflater.inflate(R.layout.list_item_message_left,
                    null);
        }

        TextView lblFrom = (TextView) convertView
                .findViewById(R.id.lblMsgFromListMessage);
        TextView lblTo = (TextView) convertView
                .findViewById(R.id.lblMsgToListMessage);

        lblFrom.setText(m.getFromName());

        // Spanned html = Html.fromHtml(m.getMessage());
        // String message = html.toString();

        String message = m.getMessage().toString();
        message = message.replaceAll("\\n",
                System.getProperty("line.separator"));

        try {
            lblTo.setText(message);

        } catch (Exception e) {
            lblTo.setText(message);
        }

        return convertView;
    }
}

TextView in layout:

<TextView
    android:id="@+id/lblMsgToListMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="50dp"
    android:background="@drawable/chat_bg_msg_from"
    android:paddingBottom="@dimen/five_dp"
    android:paddingLeft="@dimen/ten_dp"
    android:paddingRight="@dimen/ten_dp"
    android:paddingTop="@dimen/five_dp"
    android:textColor="@color/chat_title_gray"
    android:textSize="@dimen/eighteen_sp" />

Is something wrong with baseAdaptor? Somewhere I saw this link, it suggested SimpleAdapter has problem setting html but in my case this doesn't help either since I am using BaseAdapter.

I will be really realy thankful for your help as I have wasted five hours on this new line issue :(

Dev01
  • 4,082
  • 5
  • 29
  • 45

1 Answers1

1

Try:

message = message.replaceAll("\\n", "&#10;");

or (only if there really are double backslashes in the input)

message = message.replaceAll("\\n", "&#10;");


Update: Replacements were not working as the search string (\n) had been stripped out (escaped) by the sql database. This answer supplied a workaround which involves running unescape on the string prior to setting it on the TextView.

Community
  • 1
  • 1
JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
  • Didnt work. I sent `\n` in db. Do i need to change to `\\n` or `\\\n` when saving to db from php ? – Dev01 Dec 23 '14 at 09:25
  • Print the message string before the replace call so we can see what's incoming. – JASON G PETERSON Dec 23 '14 at 09:28
  • On the search side of the command maybe you need to use Unicode/HTML values instead of the direct slash symbol (http://unicode-table.com/en/#005C), but first see how the incoming string prints. – JASON G PETERSON Dec 23 '14 at 09:32
  • `12-23 14:31:16.259: I/MessageLOG(1957): Hey, Welcome to App - Android for World. Enjoy :)` – Dev01 Dec 23 '14 at 09:34
  • In db I see lines split on new lines but log cat showed one single line like that – Dev01 Dec 23 '14 at 09:35
  • There's a dup to your issue here: http://stackoverflow.com/questions/3586763/new-line-character-n-not-displaying-properly-in-textview-android. DB is most likely stripping out your `\n`s. – JASON G PETERSON Dec 23 '14 at 09:47