I am using RecyclerView
for chat purpose. And Kindly see the attached screenshots below to better understand the problem:
IMAGE-1
IMAGE-2
Now, the problem here is when the keyboard opens up, the user must be able to see the last sent/received message, however in image-2
, as you can see, the position of scroll of RecyclerView
is at an earlier message.
I've tried few changes:
changed
android:windowSoftInputMode="adjustResize"
Changed the smoothScroll of RecyclerView ,
rview.smoothScrollToPosition(adapter_data.size());
What can I do to fix this , so that I receive the last message when the keyboard opens up?
EDIT-1
This is the code to my adapter:
CustomAdapter.class
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.FeedsViewHolder>
{
DataHolder d1 = new DataHolder();
public class FeedsViewHolder extends RecyclerView.ViewHolder
{
private TextView chat;
private Typeface face;
FeedsViewHolder(View itemView)
{
super(itemView);
chat = (TextView)itemView.findViewById(R.id.chatMessage);
face = Typeface.createFromAsset(itemView.getContext().getAssets(), "Fonts/Roboto-Regular.ttf");
chat.setTypeface(face);
}
}
private static class DataHolder
{
List<Text> feeds;
}
CustomAdapter(List<Text> feeds)
{
this.d1.feeds = feeds;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public FeedsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_message, viewGroup, false);
FeedsViewHolder pvh = new FeedsViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(FeedsViewHolder feedViewHolder, int i)
{
RelativeLayout.LayoutParams params =
(RelativeLayout.LayoutParams)feedViewHolder.chat.getLayoutParams();
if(d1.feeds.get(i).isMachineOrHuman())
{
params.addRule(RelativeLayout.ALIGN_PARENT_START);
feedViewHolder.chat.setLayoutParams(params);
feedViewHolder.chat.setBackgroundResource(R.drawable.user);
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
else
{
params.addRule(RelativeLayout.ALIGN_PARENT_END);
feedViewHolder.chat.setLayoutParams(params);
feedViewHolder.chat.setBackgroundResource(R.drawable.ais);
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
}
@Override
public int getItemCount()
{
if(d1.feeds!=null)
{
return d1.feeds.size();
}
else
{
return 0;
}
}
}
chat_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_rel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/chatMessage"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:gravity="start"
android:textSize="15sp"
android:textColor="#ffffff"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
android:windowSoftInputMode="adjustResize"
is working perfect here, but the problem is associated to the RecyclerView
, I am seeking a programmatic way to scroll to the end of RecyclerView
when keyboard pops up.