首页 > 解决方案 > Toolbar moves up when keyboard appears in fragment android

问题描述

I'm currently dealing with 1 problem in my Android App. I have a Fragment with a toolbar and with an EditText placed at the bottom. The first problem that I found was that when my keyboard appears the toolbar moves up, I found a solution for this adding this line of code in my Manifest.xml in the parent activity:

android:windowSoftInputMode="adjustPan|adjustResize"

it works well, with this code my toolbar doesn't move up, but my EditText stays hidden by the keyboard and I'm not able to see what I'm typing.

This is my chat Fragment view

enter image description here

When my keyboard appears, toolbar moves up and EditText is overlaping with the keyboard

enter image description here

After I added adjustResize and adjustPan, my toolbar doesn't move up when the keyboard appears, but my EditText stays hidden

enter image description here

I hope you can help me, thanks!

My layout:

<?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"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/materialToolbar"
    android:layout_width="0dp"
    android:layout_height="60dp"
    android:elevation="5dp"
    android:background="@android:color/white"
    app:layout_constraintBottom_toTopOf="@id/rclv_chats"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/btnback"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/icon_back"
            android:layout_marginLeft="15dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">
        </com.google.android.material.imageview.ShapeableImageView>

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/img"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/foto_guy"
            app:riv_corner_radius="15dp"
            android:layout_marginRight="80dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent">
        </de.hdodenhof.circleimageview.CircleImageView>

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/txt_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textColor="#000000"
            android:layout_marginLeft="15dp"
            android:fontFamily="@font/sf_regular"
            app:layout_constraintStart_toEndOf="@id/img"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">
        </com.google.android.material.textview.MaterialTextView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.appbar.MaterialToolbar>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rclv_chats"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/materialToolbar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toTopOf="@id/view">
</androidx.recyclerview.widget.RecyclerView>

<View
    android:id="@+id/view"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@id/rclv_chats"
    android:background="#D9D9D9">
</View>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="@android:color/white"
    android:layout_marginRight="15dp"
    android:src="@drawable/icon_send"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="@id/view"
    app:layout_constraintTop_toTopOf="@id/view">
</com.google.android.material.floatingactionbutton.FloatingActionButton>

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/edt_msj"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:background="@drawable/design_chat_inputmessage"
    android:hint="Escribe un mensaje"
    android:textColor="@android:color/black"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:textSize="14sp"
    android:textColorHint="@android:color/darker_gray"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="@id/view"
    app:layout_constraintTop_toTopOf="@id/view"
    app:layout_constraintEnd_toStartOf="@id/fab_send">
</com.google.android.material.textfield.TextInputEditText>

</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidandroid-fragmentsandroid-edittextandroid-toolbar

解决方案


推荐阅读