I,m assuming you're using a custom adapter with some kind of RecyclerView or so.
Simply create static boolean variable that helps hold true when the bottom is reached in your adapter like below, i'm assuming recyclerView in this case.
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
public static boolean bottomReached = false;
@Override //Make sure it happens on bindViewHolder or related...
public void onBindViewHolder(final ViewHolder holder, int position) {
if (position == data.size() - 1)
bottomReached = true;
else
bottomReached = false;
}
}
So in your activity, for example chatActivity, we do like below.
public class ChatActivity extends AppCompatActivity{
ChatAdapter chatAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
chatAdapter = new ChatAdapter(this, messagesDataSample);
}
private void gotNewMessage(){
if(chatAdapter.bottomReached)
recyclerView.scrollToPosition(adapter.getItemCount() - 1);
else
// else is not necessary as you don't want to do anything.
}
}
Hopefully this helps, else pls let me know what goes wrong.