Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: auto scrolling down the EditTextView for chat apps

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>
like image 470
Byte Avatar asked Feb 24 '11 07:02

Byte


People also ask

How do I auto scroll my screen?

Using Easy Scroll is, like the name suggests, quite easy. You simply grant it the accessibility permission as well as the permission to draw over other apps. And that's it. The app will now display a widget on your screen that allows you to scroll automatically.

Is there a scrolling app?

Automatic Scroll is an easy-to-use app that lets you automate taps and screen movements on your Android device. To use Automatic Scroll, all you have to do is activate the features that you want to use. Luckily, the app is easy to set up.

How do I scroll up layout when clicking on EditText?

Android EditText not editable but scrollableUse this porperties in your EdittText: android:enabled="true" android:focusableInTouchMode="false" android:scrollbars="vertical". Your EditText will be enable, but not focusable for edit. Then, you will able to scroll (in vertical on this case) and will not able to edit.


3 Answers

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());
                } 
            }); 
like image 121
senorcarbone Avatar answered Sep 27 '22 16:09

senorcarbone


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));
like image 45
Jignesh Dhua Avatar answered Sep 27 '22 17:09

Jignesh Dhua


Try this method: Programatically scrolling an EditText

like image 27
Michael Avatar answered Sep 27 '22 18:09

Michael