0


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;
  }
Nik-Sch
  • 102
  • 3
  • 6
  • The width will ofcourse span as long as there is space for it to span. So you can try to set the width to the half of the screenview and set it to allow multiple lines in XML to let it auto-break in lines when it reach the end of the size of the width. –  Dec 17 '15 at 23:56
  • That would actually be the best possibility but my problem with this approach was that I was unable to set the width to half the screen width. As far as I know this is only possible with the LinearLayout which wouldn't allow me to align the information correctly if I am not mistaken. – Nik-Sch Dec 18 '15 at 00:25

1 Answers1

0

If you want you messages to stay within a certain line, try using Linear Layout. I am not sure if it will do everything you need it to as I don't know all the requirements for your app, but it may be a good place to start.

Linear Layout and weight in Android has some good advice as well as the official android documentation at http://developer.android.com/guide/topics/ui/layout/linear.html

Good luck.

Community
  • 1
  • 1
Theyna
  • 317
  • 4
  • 12
  • Yeah, I knew about the LinearLayout with it weight, but I am not quite sure, how to add it here or which layout to replace :/ – Nik-Sch Dec 18 '15 at 00:08
  • Replace your relative layouts with linear ones, and if you want to be able to scroll the screen then wrap the entire thing in a scrollview, in this case that would just be replacing the first relative layout with scrollview. Or follow http://stackoverflow.com/questions/6674341/how-to-use-scrollview-in-android – Theyna Dec 18 '15 at 00:14
  • Well I got everything running with the scrollView, I will update the question with the code where I use this layout. If I would replace all my RelativeLayouts I would not be able to align the info correctly, or am I mistaken? How would this look? – Nik-Sch Dec 18 '15 at 00:23
  • Try it and find out! I can't tell you exactly what will happen as I can't run your project. Once you work out what you are doing you will find that changing layouts is pretty simple and relatively quick in a small project :) – Theyna Dec 18 '15 at 00:34
  • Keep trying things, once you get it working you will know what to do for next time and will have learnt plenty with your trying :) – Theyna Dec 18 '15 at 00:43