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 :(