Android: auto-scroll EditTextView apps for chats

Thanks for watching, I am creating a chat application. It works for the most part. The only thing I came across is scrolling. I am using EditText to post a new message from the server.

according to the method

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

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

Therefore, I think the best way is to automatically scroll down to the lowest level as soon as the view is updated.

Is there an easy way to do this? for example, in the layout ?

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> 
+4
source share
3 answers

The solution is to send a separate user interface message through the message. It will definitely work. After you added the TextText / append text to the TextView inside the ScrollView, update the ScrollView using the post (Runnable) method, for example, the code below:

 messageView.append(blabla); scroller.post(new Runnable() { public void run() { scroller.smoothScrollTo(0, messageView.getBottom()); } }); 
+15
source

I think the best way to do this is with a TextView using a scroller rather than with an EditText, because when a user who prints a message cannot edit it. Try something like this. This is a great way to print a 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 automatically scroll down, call this method after clicking a message to view

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

Here scroller is ScrollView and messageView is TextView

You can also print a message with differnt color using HTML, for example

 messageView.append(Html.fromHtml("<font color='green'><b>("+date+") "+ username +":</b></font><br/>"+ message+"<br/>", null, null)); 
+8
source

Try this method: EditText soft scrolling

+1
source

Source: https://habr.com/ru/post/1341092/


All Articles