2

I am using RecyclerView for chat purpose. And Kindly see the attached screenshots below to better understand the problem:

IMAGE-1

enter image description here

IMAGE-2

enter image description here

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.

Community
  • 1
  • 1
OBX
  • 6,044
  • 7
  • 33
  • 77
  • Possible duplicate of [Android: Resize only parts of view with soft keyboard on screen](http://stackoverflow.com/questions/21201569/android-resize-only-parts-of-view-with-soft-keyboard-on-screen) – Knossos Oct 14 '16 at 09:07
  • Since you say you added adjustResize, did you check if it actually works? you could set a background picture to the page and see if it's shrinked vertically when the keyboard pops. Also, you should add the smooth scroll in onConfigurationChanged. Sorry if the questions sound dumb, but it would seem what you tried should be the solution. It worked for me. – Scorpio Oct 14 '16 at 09:08
  • @Scorpio adjustResize is working picture perfect – OBX Oct 14 '16 at 09:10
  • rview.smoothScrollToPosition(adapter_data.size() - 1); ? And you could also try the answer from here: http://stackoverflow.com/questions/26580723/how-to-scroll-to-the-bottom-of-a-recyclerview-scrolltoposition-doesnt-work – Scorpio Oct 14 '16 at 09:15
  • @Scorpio `smoothScrollToPosition()` does work, here is the instance, consider a long conversation, so automatically, the RecyclerView is moved to the last messages as the conversation proceeds, the position get altered , only when the keyboard opens up , at all other times, smooth scroll is working perfectly alright – OBX Oct 14 '16 at 09:18

0 Answers0