6

Thank you for looking, I am creating a chat application. It works for the most part. The only thing I have a problem with is the scrolling. I use EditText to publish the new message from the server.

by method

msg = msg + "\n" + newMsg
EditText.setText(msg)

I need to make the new text that is under the old text visible as soon as it comes.

So I think best way is to auto scroll down to the bottom as soon as the view is updated.

Is there an easy way to do that? like in layout maybe?

Thanks again!

code for layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="send"
/>
    <EditText
android:id="@+id/msgBox"
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:layout_alignParentBottom="true" 
android:layout_toLeftOf="@+id/sendButton"
android:gravity="left"
android:longClickable="false"
/>
<EditText  
android:id="@+id/chatBox"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:editable="false"
android:layout_above="@+id/msgBox"
android:scrollbars="vertical" 
android:gravity="left|top" 
android:isScrollContainer="true" 
android:cursorVisible="false" 
android:longClickable="false" 
android:clickable="false"
android:autoLink="all"
/>
</RelativeLayout>
Byte
  • 2,920
  • 3
  • 33
  • 55

3 Answers3

16

A solution is to send a seperate UI message via post. That will definitely work. After you setText/append text to your TextView inside the ScrollView update the ScrollView via the post(Runnable) method like the code below:

messageView.append(blabla);      
scroller.post(new Runnable() { 
                public void run() { 
                    scroller.smoothScrollTo(0, messageView.getBottom());
                } 
            }); 
senorcarbone
  • 242
  • 2
  • 8
  • 1
    This works to scroll ALL the way to the bottom ! Use Jignesh's XML layout http://stackoverflow.com/questions/5101448/android-auto-scrolling-down-the-edittextview-for-chat-apps/5101616#5101616 and then SenorCarbone's code above. – Someone Somewhere Nov 18 '11 at 04:41
11

I think best way to do this use TextView with scroller rather than EditText because once message print user can't edit it. Try something like this, This is beautiful way to print message

<ScrollView android:id="@+id/scroller"
        android:layout_width="fill_parent" android:layout_height="fill_parent"

        android:background="#FFFFFF">
        <TextView android:id="@+id/messageView"
            android:layout_height="fill_parent" android:layout_width="fill_parent"
            android:paddingBottom="8dip" android:background="#ffffff"
            android:textColor="#000000" />
    </ScrollView>

And to scroll automatically down call this method after pushing message to view

private void scrollDown() {
        scroller.smoothScrollTo(0, messageView.getBottom());  
}

Here scroller is ScrollView and messageView is TextView

You can also print message with differnt color using HTML like

messageView.append(Html.fromHtml("<font color='green'><b>("+date+") "+ username +":</b></font><br/>"+ message+"<br/>", null, null));
Jignesh Dhua
  • 1,471
  • 1
  • 14
  • 31
  • it is very nice to do that but i use EditText because i like the box look. I also made it not editable nor clickable. I have been trying to get this to work with your solution but it seems my layout is a relative layout and that does not go well with the rest. or maybe i was not doing it right. please take a look at my layout. thank you! – Byte Feb 24 '11 at 08:01
  • 1
    I was stupid! I should have looked closely, it was the append instead of setText that makes it work!! Now it wont go down to the last one but the second last.. any comment? – Byte Feb 28 '11 at 08:17
  • Thanks for accept ans, you can also append smiles using HTML.formHTML() – Jignesh Dhua Feb 28 '11 at 11:55
  • Try. scroller.smoothScrollTo(0, messageView.getBottom()+5000); – Jignesh Dhua Feb 28 '11 at 11:56
1

Try this method: Programatically scrolling an EditText

Community
  • 1
  • 1
Michael
  • 53,859
  • 22
  • 133
  • 139