Skip to content
Advertisement

recycler view hides message upward when keyboard is open / how to keep recycler View from scrolling when keyboard is on

I am making chat app but when I send message recycler view does not show first 2 messages because it is up I want something like whatsapp if I open keyboard recycler view is shown from start I tried following but it sticks chats to end even after I close keyboard:

            LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
            layoutManager.setStackFromEnd(true);
            recyclerView.setLayoutManager(layoutManager);

. following is my code:

public class UserChat extends AppCompatActivity {

private RecyclerView recyclerView;
private Button btnSend;
private EditText messageBox;
private TextView name_txt;
final ArrayList<ChatModel> chatModels = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_chat);

    recyclerView = findViewById(R.id.chat_list);
    messageBox = findViewById(R.id.et_chat_box);
    btnSend = findViewById(R.id.btn_chat_send);
    name_txt = findViewById(R.id.name_txt);
    String username = "username not set";
    Bundle extras = getIntent().getExtras();
    if(extras!=null){
        username = extras.getString("username");
    }
    name_txt.setText(username);

    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            int i = 1;
            i++;
            ChatModel chatModel = new ChatModel();
            chatModel.setId(i);
            chatModel.setMe(true);
            chatModel.setMessage(messageBox.getText().toString().trim());

            chatModels.add(chatModel);

            ChatAdapter chatAdapter = new ChatAdapter(chatModels, getApplicationContext());
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            recyclerView.setAdapter(chatAdapter);
            messageBox.setText("");
        }
    });
}

}

following is my XML :

   <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UserChat">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/chat_list"
    android:layout_width="406dp"
    android:layout_height="611dp"
    android:paddingStart="15dp"
    android:paddingTop="15dp"
    app:layout_constrainedHeight="true"
    app:layout_constraintBottom_toTopOf="@+id/view"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar"
    app:layout_constraintVertical_bias="0.974" />

<View
    android:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#e0e0e0"
    app:layout_constraintBottom_toTopOf="@+id/layout_gchat_chatbox" />

<RelativeLayout
    android:id="@+id/layout_gchat_chatbox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent">


    <EditText
        android:id="@+id/et_chat_box"
        android:layout_width="333dp"
        android:layout_height="48dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="0dp"
        android:layout_toStartOf="@+id/btn_chat_send"
        android:background="@android:color/transparent"
        android:hint="Enter Message"
        android:inputType="text"
        android:maxLines="6"
        tools:ignore="Autofill" />

    <Button
        android:id="@+id/btn_chat_send"
        android:layout_width="78dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="0dp"
        android:background="?attr/selectableItemBackground"
        android:text="Send"
        android:textColor="@color/teal_700" />

</RelativeLayout>

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="408dp"
    android:layout_height="64dp"
    android:background="#2E2A2A"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.333"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/name_txt"
    android:layout_width="wrap_content"
    android:layout_height="39dp"
    android:layout_marginTop="16dp"
    android:text="TextView"
    android:textColor="#FAF8F8"
    android:textSize="21sp"
    app:layout_constraintEnd_toEndOf="@+id/toolbar"
    app:layout_constraintHorizontal_bias="0.368"
    app:layout_constraintStart_toStartOf="@+id/toolbar"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="67dp"
    android:layout_height="53dp"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toStartOf="@+id/name_txt"
    app:layout_constraintHorizontal_bias="0.673"
    app:layout_constraintStart_toStartOf="@+id/toolbar"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/female" />

</androidx.constraintlayout.widget.ConstraintLayout>

Advertisement

Answer

There was a problem in height of the recycler view. I saw it here: https://github.com/stfalcon-studio/ChatKit/issues/103. @andriizhumela said that you need to put match parent in width and height of recycler view . so now it is solved

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement