7

am new to this and quite new to Android programming.

I am trying to make a layout like in the WhatsApp chat activity. So what I want to achieve is to get the same layout and behavior as for the EditText and the Buttons next to it. This means a left Button for smileys and EditText in the middle and a Button to the right of it for sending the text.

In WhatsApp when the EditText expands its size (multiline) the buttons stay at the bottom. But it seems not to be the bottom of the parent view cause the Buttons have been centered to the EditText before.

I tried a lot like putting the three views in a TableLayout's row. Or just using a RelativeLayout. But nothing of it works properly.

Can anyone show me how to achieve this? I can provide my XML but....well...it's obviously pretty bad :D

Thanks in advance!

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">

<ListView
    android:id="@+id/messages"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/footer_section">
</ListView>

<LinearLayout
    android:id="@+id/footer_section"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:minHeight="48dp">

        <ImageView
            android:id="@+id/emoticons_button"
            android:layout_height="match_parent"
            android:layout_width="48dp"
            />

        <EditText
            android:id="@+id/message_text"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:inputType="textMultiLine"
            android:minLines="1"
            android:maxLines="4"/>

        <ImageView
            android:id="@+id/send_button"
            android:layout_height="match_parent"
            android:layout_width="48dp"
            android:layout_gravity="center_vertical"/>

</LinearLayout>

enter image description here

enter image description here

1
  • Please provide your xml Feb 18, 2015 at 3:43

1 Answer 1

18

Edit

<ListView
    android:id="@+id/messages"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/footer_section"
    android:layout_alignParentTop="true" >
</ListView>

<LinearLayout
    android:id="@+id/footer_section"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" 
    android:background="#eeeeee">

    <ImageView
        android:id="@+id/emoticons_button"
        android:layout_width="48dp"
        android:layout_height="match_parent" />

    <EditText
        android:id="@+id/message_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:fadeScrollbars="false"
        android:layout_weight="1"
        android:inputType="textMultiLine" 
        android:maxLines="4"
        android:minLines="1"
        android:textColor="@android:color/black" />

    <ImageView
        android:id="@+id/send_button"
        android:layout_width="48dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical" />
</LinearLayout>

As you said you are newbie in android, you should try using Linear Layout for layout you want to achieve.

Try below xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dip"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/btn_emoticon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_emoticon" />

    <EditText
        android:id="@+id/chat_text"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:paddingLeft="8dp"
        android:paddingRight="6dp" />

    <ImageButton
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_send_grey" />
</LinearLayout>
</LinearLayout>
9
  • Hi Dory, thanks a lot. Ok, I think I should deal a bit with weightSum and layout_weight attributes. I got it almost working yet in a RelativeLayout. So there is a ListView above this footer section described as above. But the problem now is...when the keyboard is shown on the screen the EditText does not really expand. If I hide the keyboard it does. Well....if you can help me any further it might be best to provide the xml file. I am just ashamed of it a bit cause obviously it contains some mistakes due to misunderstanding or non understanding :-D
    – marcus
    Feb 18, 2015 at 23:53
  • Hi @marcus, I didn't really understood your problem. could you elaborate me more exactly what is your requirement.
    – Dory
    Feb 19, 2015 at 5:32
  • Hi Dory, sorry. So I added two pictures. I want the EditText to show up max 4 lines. So when I enter text the parent LinearLayout should increase its height according to the EditText height or lines to be shown. But when the keyboard is shown it doesn't do this. If I close the keyboard the EditText shows max 4 lines and the parent LinearLayout also has a larger height. But I want to achieve this already when entering text and when the keyboard thus is shown. Did you get it?
    – marcus
    Feb 19, 2015 at 17:07
  • 1
    @marcus okie I got your issue. Please see my edit in xml. You need to remove the android:minHeight="48dp" from the inner Linear Layout. I will not hide the lines of you multiline edit text and increase the linear layout height likewise upto the device screen height.
    – Dory
    Feb 20, 2015 at 9:56
  • thank you so much. Exactly what I wanted. Finally I can rate up answers, too :-) Thanks again
    – marcus
    Feb 20, 2015 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.