简体   繁体   中英

Android. How to show button to top of keyboard when it's opened?

I need to show my button to top of soft keyboard (when it's opened). For this I implement this https://medium.com/@madalinnita/android-how-to-move-views-above-keyboard-when-its-opened-quick-secure-solution-90188c4d7b15 .

My layout structure:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical">
 <androidx.core.widget.NestedScrollView
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:isScrollContainer="true"
       app:layout_constraintBottom_toTopOf="@+id/bottomContainer"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

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

           <EditText..../>
           <EditText..../>

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

 <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:gravity="end"/>

</androidx.constraintlayout.widget.ConstraintLayout>

And everything working fine, but in small devices my button covers edit text. Is it possible to fix it? I don't have any ideas. I need to somehow make the button and keyboard appear under edit text even on small devices.

Please, help me.

You can wrap your whole view inside a ScrollView which would then allow you to scroll the view while the keyboard is open. In this example I am using a LinearLayout you can use other layouts as well.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
     ...
    </LinearLayout>
</ScrollView>

And add to your Manifest for the specific activity this line of code:

android:windowSoftInputMode="adjustResize"

Add this line -> android:windowSoftInputMode="adjustResize" , in your current activity tag in Manifest as follow

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        ...>
        
        <activity 
            android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize"
            ...
            />
        ...
    </application>
</manifest>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM