Android: auto scrolling down the EditTextView for chat apps

这一生的挚爱 提交于 2019-12-05 13:11:25

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

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