1

I have an adapter linked to RecyclerView. I'm trying to implement chat screen, I use sockets to fetch messages, and if message came from user's phone chat bubble displays in left part of screen, if message came from interlocutor's phone chat bubble displays in left part of screen. Let's see:

Normal view:

enter image description here

And after when I open keyboard to type something, something goes wrong:

Opened keyboard:

enter image description here

But when I scroll RecyclerView up and down everything becomes ok.

Here is my codes:

Chat screen layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#F3F7FD">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_marginBottom="47dp"
            android:paddingTop="6dp"
            android:paddingBottom="8dp"
            android:clipToPadding="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:id="@+id/element1"
            android:layout_width="match_parent"
            android:layout_height="47dp"
            android:background="#FFFFFF"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true">

            <ImageView
                android:id="@+id/sAttach"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_weight="0"
                android:layout_marginTop="9dp"
                android:layout_marginLeft="11dp"
                android:layout_marginRight="9dp"
                android:src="@drawable/attach"
                android:tint="#A7A7A7"
                android:background="?selectableItemBackgroundBorderless"
                android:clickable="true" />

            <ProgressBar
                android:id="@+id/updateBar"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_weight="0"
                android:layout_marginTop="9dp"
                android:layout_marginLeft="11dp"
                android:layout_marginRight="9dp"
                android:indeterminate="true"
                android:indeterminateTintMode="src_atop"
                android:visibility="gone" />

            <EditText
                android:id="@+id/msg"
                android:layout_width="match_parent"
                android:layout_height="47dp"
                android:layout_weight="1"
                android:paddingLeft="2dp"
                android:textSize="16sp"
                android:inputType="textCapSentences"
                android:maxLines="1"
                android:hint="@string/hint_message"
                android:background="#FFFFFF"/>

            <ImageView
                android:id="@+id/sBt"
                android:layout_width="33dp"
                android:layout_height="33dp"
                android:layout_weight="0"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                android:paddingLeft="9dp"
                android:paddingRight="7dp"
                android:layout_marginTop="7dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="10dp"
                android:src="@drawable/send"
                android:background="@drawable/circle_send_gray"
                android:clickable="true" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/element2"
            android:layout_width="match_parent"
            android:layout_height="47dp"
            android:background="#FFFFFF"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:visibility="gone">

            <TextView
                android:id="@+id/blockMessage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginBottom="47dp"
            android:layout_alignParentBottom="true"
            android:orientation="vertical"
            android:background="#20000000" />

    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

Here is Chat.class:

private JsonArrayRequest getDataFromServer(String user, String room) {

        //JsonArrayRequest of volley
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(HERE GOES MY APIS URL,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        //Calling method parseData to parse the json response
                        parseData(response);
                        //Hiding the progressbar

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        //If an error occurs that means end of the list has reached

                    }
                });

        //Returning the request
        return jsonArrayRequest;
    }

    private void parseData(JSONArray array) {
        for (int i = 0; i < array.length(); i++) {
            //Creating the superhero object
            ChatList superHero = new ChatList();
            JSONObject json = null;
            try {
                //Getting json
                json = array.getJSONObject(i);

                superHero.setMessage(json.getString("message"));
                superHero.setFromtype(json.getString("type"));
                superHero.setPhoto(json.getString("photo"));

            } catch (JSONException e) {
                e.printStackTrace();
            }
            //Adding the superhero object to the list
            listSuperHeroes.add(0, superHero);
        }

        recyclerView.scrollToPosition(listSuperHeroes.size() -1);

        //Notifying the adapter that data has been added or changed
        adapter.notifyDataSetChanged();
    }

Chat Adapter:

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {

    private Context context;

    private float scale;

    private int dpAsPixels;
    private int dpAsPixels2;
    private int dpAsPixels3;

    private LinearLayout.LayoutParams params;
    private LinearLayout.LayoutParams params2;

    //List to store all superheroes
    static List<ChatList> superHeroes;

    public ChatAdapter(List<ChatList> superHeroes, Context context){
        super();
        //Getting all superheroes
        this.superHeroes = superHeroes;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_list, parent, false);
        ChatAdapter.ViewHolder viewHolder = new ViewHolder(v);

        scale = context.getResources().getDisplayMetrics().density;
        dpAsPixels = (int) (3 * scale + 0.5f);
        dpAsPixels2 = (int) (12 * scale + 0.5f);
        dpAsPixels3 = (int) (12 * scale + 0.5f)+180;

        params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ChatAdapter.ViewHolder holder, int position) {

        //Getting the particular item from the list
        final ChatList superHero =  superHeroes.get(position);
        //Showing data on the views

        if(superHero.getMessage().equals("image")) {
            Picasso.with(context).load("http://mywebsite.com/uploads/media/"+superHero.getPhoto()).into(holder.photo);
            holder.photo.setVisibility(ImageView.VISIBLE);
            holder.message.setVisibility(TextView.GONE);
            params2.bottomMargin = dpAsPixels+5;
            params2.topMargin = dpAsPixels+5;
            if(superHero.getFromtype().equals("he")) {
                params.setMargins(dpAsPixels2, dpAsPixels, dpAsPixels3, dpAsPixels);
                holder.photo.setLayoutParams(params2);
                holder.photo.setBackgroundResource(R.drawable.chat_bubble_photo);
                holder.textCard.setGravity(Gravity.LEFT);
            }else{
                params.setMargins(dpAsPixels3, dpAsPixels, dpAsPixels2, dpAsPixels);
                holder.photo.setLayoutParams(params2);
                holder.photo.setBackgroundResource(R.drawable.chat_bubble_me_photo);
                holder.textCard.setGravity(Gravity.RIGHT);
            }

            holder.textCard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((Chat)context).openMedia(superHero.getPhoto());
                }
            });
        }else{
            holder.photo.setVisibility(ImageView.GONE);
            holder.message.setVisibility(TextView.VISIBLE);
            holder.message.setText(decodeMessage(superHero.getMessage()));
        }

        if(superHero.getFromtype().equals("he")) {
            params.setMargins(dpAsPixels2, dpAsPixels, dpAsPixels3, dpAsPixels);
            holder.message.setLayoutParams(params);
            holder.message.setBackgroundResource(R.drawable.chat_bubble);
            holder.message.setTextColor(context.getResources().getColor(R.color.darkgray));
            holder.textCard.setGravity(Gravity.LEFT);
        }else{
            params.setMargins(dpAsPixels3, dpAsPixels, dpAsPixels2, dpAsPixels);
            holder.message.setLayoutParams(params);
            holder.message.setBackgroundResource(R.drawable.chat_bubble_me);
            holder.message.setTextColor(context.getResources().getColor(R.color.colorPrimary));
            holder.textCard.setGravity(Gravity.RIGHT);
        }

    }

    public int getItemCount() {
        return superHeroes.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        //Views
        public TextView message;
        public LinearLayout textCard;
        public ImageView photo;

        //Initializing Views
        public ViewHolder(View itemView) {
            super(itemView);
            message = (TextView) itemView.findViewById(R.id.message);
            textCard = (LinearLayout) itemView.findViewById(R.id.textCard);
            photo = (ImageView) itemView.findViewById(R.id.photo);
        }
    }

    private String decodeMessage(String message) {
        String raz = URLDecoder.decode(message);
        return StringEscapeUtils.unescapeJava(raz);
    }

}

2 Answers 2

0

Sorry I cant able to comment thats why giving answer here

When you open keyboard at that time you have to scroll recyclerview at its last position.

mLinearLayoutManager.scrollToPosition(yourList.size()); 

Hope it works.

3
  • Oh, I've tried that right now, It still doesn't work :( Feb 11, 2017 at 18:46
  • please try this in your activity tag in manifest. android:windowSoftInputMode="adjustResize" Feb 14, 2017 at 9:16
  • stackoverflow.com/a/12956520/7320259 please try this android:isScrollContainer="false" in Scrollview Feb 16, 2017 at 4:21
0

I've solved this problem this way:

I've add 2 different chat bubbles for both incoming and outgoing messages, and in ChatAdapter adjusted them by setVisibility to set which bubble should be shown. Margins are setted right in the layout file, not from ChatAdapter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.