1

I'm making a chat activity for a school project and I want the newest messages at the bottom of the listView. Adding the code below does not work. I simply want the newest message to appear at the bottom and make the listView automatically scroll to the last list item. Thanks in advance!

android:transcriptMode="alwaysScroll"
android:stackFromBottom="true"

Link to image of current situation

XML:

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#1a1a1a"
        android:id="@+id/relativeLayout_Messages"
        android:layout_below="@id/my_toolbar"
        android:layout_alignBottom="@+id/imageView_Separator">
        <ListView
            android:id="@+id/listView_Messages"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:stackFromBottom="true"

            />

    </RelativeLayout>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_Separator"
        android:background="#1a1a1a"
        android:src="@drawable/seperator"
        android:layout_above="@+id/relativeLayout_ChatInput"
        />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:id="@+id/relativeLayout_ChatInput"
        android:background="#1a1a1a">


        <EditText
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:id="@+id/editText_ChatInput"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_marginStart="5dp"
            android:background="@drawable/search_field"
            android:layout_toStartOf="@+id/imageButton_Send"
            android:layout_alignBottom="@+id/imageButton_Send"
            android:inputType="textPersonName"
            android:hint="@string/chat_field_hint"
            android:textColor="#f4f5f6"
            android:textColorHint="#f4f5f6"/>


        <ImageButton
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:id="@+id/imageButton_Send"
            android:background="@android:color/transparent"
            android:src="@drawable/send_button"
            android:scaleType="fitCenter"
            android:layout_marginTop="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="6dp"
            android:layout_marginBottom="8dp"
            android:layout_alignParentEnd="true"
            android:onClick="sendButtonClicked"
          />
    </RelativeLayout>


</RelativeLayout>

JAVA

package com.example.muhryn.resonatem;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import java.util.*;

public class ChatActivity extends AppCompatActivity{

    public void hideKeyboard() {
        try {
            InputMethodManager imm=(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        } catch (Exception ex) { }
    }

    private ListView chatListView;
    private Button submitButton;
    private EditText chatEditText;
    private Chat chat;
    private ArrayList receivedList=new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        chatListView=(ListView)findViewById(R.id.listView_Messages);
        chatEditText=(EditText)findViewById(R.id.editText_ChatInput);
        chat=new Chat("A","Resonate");
        chat.setListener(new Chat.Listener(){
            public void received(final String received) {
                System.out.println("Received: "+received);
                runOnUiThread(new Runnable() {
                    public void run() {
                        hideKeyboard();
                        receivedList.add(0, received);
                        chatListView.setAdapter(new ChatList(ChatActivity.this, receivedList));
                    }
                });
                //////((ArrayAdapter)chatListView.getAdapter()).insert(received,0);
            }
        });
    }

    public void sendButtonClicked(View view){
        String textToSubmit=chatEditText.getText().toString().trim();
        if(textToSubmit.isEmpty())
            return;
        if(chat.submitted(textToSubmit)) {
            receivedList.add(0,"Sent: "+textToSubmit);
            chatListView.setAdapter(new ChatList(ChatActivity.this, receivedList));
        } else
            Toast.makeText(this,"Failed to submit "+textToSubmit+".",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStop () {
        super.onStop();
        chat.stop();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.chat_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.view_profile:
                finish();
                return true;
            case R.id.report_match:
                finish();
                return true;
            case R.id.add_match:
                finish();
                return true;
            case R.id.unmatch:
                finish();
                return true;


            default:
                return super.onOptionsItemSelected(item);
        }
    }






}
Muhryn
  • 53
  • 5
  • I'm not sure about `stackFromBottom` but you can always add items in reverse order and scroll to bottom manually – Sourabh Mar 30 '16 at 15:18
  • How do you add items in reverse order? – Muhryn Mar 30 '16 at 15:18
  • 1
    `Collections.reverse(receivedList)`? – Sourabh Mar 30 '16 at 15:25
  • Possible duplicate of [Is it possible to make a ListView populate from the bottom?](http://stackoverflow.com/questions/9987402/is-it-possible-to-make-a-listview-populate-from-the-bottom) – Petro Mar 30 '16 at 17:36
  • Cant figure out how to do it with "Collections.reverse(receivedList)" because android switches it multiple times and messes up the entire order. Also, not a duplicate of the linked post. I already tried the solutions there. – Muhryn Apr 01 '16 at 13:18

1 Answers1

2

Try the below code.

chatListView.smoothScrollByOffset(receivedList.size()-1);

It will scroll down to latest chat.

sanjay
  • 626
  • 1
  • 7
  • 12
  • Could you help me where exactly I have to put in this piece of code? – Muhryn Apr 01 '16 at 14:14
  • First add new item in the list then call 'notifyDatasetChanged()' inside your adapter.And then call above line from your **Activity** or **Fragment**. – sanjay Apr 02 '16 at 16:23