Use Custom adapterwith separate layouts for sender and recipient messages. He is called Heterogeneous ListView.
Something like that
public class MyAdapter extends BaseAdapter {
ArrayList<HashMap<String,String>> messages;
int SENDER_MESSAGE = 0;
int RECEIVER_MESSAGE = 1;
Context context;
@Override
public int getCount() {
return messages.size();
}
@Override
public Object getItem(int position) {
return messages.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return SENDER_MESSAGE;
}
else {
return RECEIVER_MESSAGE;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (getItemViewType(position) == SENDER_MESSAGE) {
convertView = inflater.inflate(R.layout.sender_message_layout, null);
}
else {
convertView = inflater.inflate(R.layout.received_message_layout, null);
}
}
}
}
For more information, Custom adapteryou can refer to this
http://www.vogella.com/articles/AndroidListView/article.html#adapterown_example
For a heterogeneous ListView guide (Various row layouts in ListView) you can refer to this
http://chrislee.kr/wp/tag/getitemviewtype-tutorial/