Android: auto scrolling down the EditTextView for chat apps

醉酒当歌 提交于 2020-01-13 11:45:52

问题


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>

回答1:


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());
                } 
            }); 



回答2:


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));



回答3:


Try this method: Programatically scrolling an EditText



来源:https://stackoverflow.com/questions/5101448/android-auto-scrolling-down-the-edittextview-for-chat-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!