Android: How to push button above soft keyboard

后端 未结 6 2045
星月不相逢
星月不相逢 2020-12-04 19:04

I\'ve got a \"save\" button which I want to push up together with the soft keyboard. So when the user clicks an EditText in my layout, then the button has to stay above the

相关标签:
6条回答
  • 2020-12-04 19:31

    You need to set your keyboard's input mode to adjustResize. You can do this adding the following line to your activity's attributes in the manifest:

        android:windowSoftInputMode="adjustResize"
    

    Here's an example of the attribute added in the activity:

    <activity 
         android:name=".activity.MyActivity"
         android:windowSoftInputMode="adjustResize">
    </activity>
    
    0 讨论(0)
  • 2020-12-04 19:38

    So this is a pretty old post, but I struggled with the answers provided. Both oneavi and Intahep are correct, but let me show you EXACTLY where the android:windowSoftInputMode="adjustResize" goes.

    in Android Manifest

        <activity android:name=".DataScreen" />
        <activity android:name=".PauseScreen" />
        <activity android:name=".RouteInfo"
                   android:windowSoftInputMode="adjustResize"> <!--This goes in the specific activity with the button -->
        </activity>
    
    0 讨论(0)
  • 2020-12-04 19:44

    Additional point Any of the above including "adjustResize" will work except if your activity is full screen. That was my case. check your activity code to ensure its not in full screen.

    0 讨论(0)
  • 2020-12-04 19:45

    Order your layout like this and you will able to put button to above keyboard

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/button_next"
            android:background="#0ff"
            >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="250dp"
                    android:hint="Hint"
                    />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="ABC"
                    android:textSize="50sp"
                    />
            </LinearLayout>
        </ScrollView>
    
        <Button
            android:id="@+id/button_next"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:layout_margin="10dp"
            android:text="Button Next"
            />
    
    </RelativeLayout>
    

    In android manifest

    <application
            ...
            >
            <activity android:name=".YourActivity"
                android:windowSoftInputMode="adjustResize"
               >
            </activity>
    </application>
    

    Note that, instead of RelativeLayout, you can also use another ViewGroup like LinearLayout with weight, CordinatorLayout, ...

    0 讨论(0)
  • 2020-12-04 19:50

    Along with Inthathep's answer, you have to add an attribute in the parent viewgroup

    android:fitsSystemWindows="true"
    

    to work it as desired. i.e, in manifest file , for the activity add

    android:windowSoftInputMode="adjustResize"
    

    and eg.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:fitsSystemWindows="true" <!-- add this -->
        android:orientation="vertical"
        >
        <EditText
            android:id="@+id/et_assetview_comment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:minHeight="80dp"
            android:background="@color/white"
            android:hint="Enter comments"
            />
        <Button
            android:id="@+id/btn_assetview_postcomment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="POST"
            />
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-04 19:53

    The best way is to hide the keystroke and, if necessary, press the buttons above the keyboard

     android:windowSoftInputMode="adjustResize|stateHidden"
    
    0 讨论(0)
提交回复
热议问题