I am developing a messenger app in Android and for this I have a RelativeLayout containing the text message and some info about it. My current xml code for this looks as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/text_message"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp">
<RelativeLayout
android:id="@+id/chat_message_inner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bubble_a1"
android:padding="1dp">
<TextView
android:id="@+id/text_message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="Hello World"
android:textColor="@android:color/black"
android:textSize="@dimen/font_size"/>
<RelativeLayout
android:id="@+id/text_message_info"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text_message_text"
android:layout_toEndOf="@id/text_message_text"
android:padding="3dp">
<TextView
android:id="@+id/chat_timeStamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/chat_status"
android:text="00:00"
android:textColor="@color/colorTimeStamp"/>
<ImageView
android:id="@+id/chat_status"
android:layout_width="19dp"
android:layout_height="13dp"
android:layout_alignEnd="@+id/chat_timeStamp"
android:layout_alignRight="@+id/chat_timeStamp"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="6dp"
android:src="@drawable/two_grey_hook"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
Small messages are looking good (Hello World Message), large messages reaching over multiple lines, however, make the information layout disappear (Longer Message). Is there a way of avoiding this problem? It doesn't really matter if I stick to RelativeLayouts or not, it just seemed for me to be the most customizable one.
Also, I know in this code extraction it seems like the two surrounding layouts can be merged into one, however, as I want to set the layout's gravity depending on who sent the message but don't want the background bubble to stretch over the whole width.
I would appreciate any kind of help and can also provide any further code snippets if you would like them.
Thanks GG15
EDIT:
The layout where I am using the message is just a ListView and then I have extended an ArrayAdapter in order to add single messages. My getView function is basically the following (I have shorten it a bit):
public View getView(int position, View ConvertView, ViewGroup parent){
View v = ConvertView;
MessageArrayContent Obj = getItem(position);
if (Obj.getClass() == TextMessage.class){
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.text_message, parent, false);
TextMessage msgObj = (TextMessage) Obj;
RelativeLayout layoutOuter = (RelativeLayout) v.findViewById(R.id.text_message);
RelativeLayout layoutInner = (RelativeLayout) v.findViewById(R.id.chat_message_inner);
layoutInner.setBackgroundResource(msgObj.left ? R.drawable.bubble_a1 : R.drawable.bubble_b1);
TextView chatText = (TextView) v.findViewById(R.id.text_message_text);
chatText.setText(msgObj.message);
TextView chatTime = (TextView) v.findViewById(R.id.chat_timeStamp);
chatTime.setText(new SimpleDateFormat("HH:mm", Locale.GERMANY).format(msgObj.time));
//here I am doing some irrelevant stuff concerning the message status
}
}else
//I also have some other types of messages, not being relevant here
return v;
}