80

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 keyboard. Now the button becomes hidden underneath the keyboard. How do you do this?

Thanks in advance!

5
  • 1
    android-developers.blogspot.com/2009/04/… Commented Mar 10, 2013 at 15:17
  • @CommonsWare actually, the referenced article is no help here. OP has a button below his EditText field (a common layout). There is actually no way to push up the EditText and the Button using the standard input method settings. Unfortunate. Commented Mar 10, 2013 at 15:28
  • 1
    @DavidWasser: Then make the IME's action button do the same thing as the hidden button. Commented Mar 10, 2013 at 15:54
  • @CommonsWare what if there's a checkbox also? Then the IME's action is of no help, actually Commented Jul 6, 2017 at 8:00
  • The answers for this question need updating to work with ConstraintLayout. Commented Sep 19, 2021 at 18:47

9 Answers 9

134

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> 
Sign up to request clarification or add additional context in comments.

2 Comments

It worked! But not completely as expected. See this new issue: stackoverflow.com/questions/15343355/…
I am using ConstraintLayout, it does not work for me.
51

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> 

2 Comments

Only this helped.
dealing with xml has always been a pain in the ass.
27

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> 

enter image description here

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

3 Comments

You should test it with a huge text, say 15 lines. And you'll see that when write it and then click on top line, part of edittext will hide behind the keyboard
Incase of everything is correct and if you gave windowFullscreen "true" it will not work, so just remove one line from your theme : <item name="android:windowFullscreen">true</item>
@varotariyavajsi thanks brother you saved my time I was messed with this issue thank you very much
11

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> 

1 Comment

You should test it with a huge text, say 15 lines. And you'll see that when write it and then click on top line, part of edittext will hide behind the keyboard
4

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

 android:windowSoftInputMode="adjustResize|stateHidden" 

Comments

2

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.

Comments

1

This simple setup scrolls the entire layout up with the keyboard.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/recycler_view" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:src="@drawable/image" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/button" android:layout_centerHorizontal="true" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="Button" /> </RelativeLayout> 

Key points:

  • Nothing added to the manifest.
  • Image must be "wrap_content" (not fixed size) to allow android to resize it as needed.
  • All views must be linked as shown above. The bottom view layout_alignParentBottom="true", the top view layout_alignParentTop="true", and all intermediate views layout_above="@id/view_below".
  • The layout_below="@id/view_above" attribute does not work for some reason.

Comments

0

In AndroidX:

Use CoordinatorLayout for main parent layout and add a NestedScrollView for your content and add your layout or button in child of CoordinatorLayout to push button above soft keyboard

<androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent" > <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:isScrollContainer="true" > ....... </androidx.core.widget.NestedScrollView> <com.google.android.material.button.MaterialButton android:id="@+id/send_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="@string/login" /> 

photo:

https://snipboard.io/n45tbx.jpg

Comments

0
private fun viewTopKeyboard() { viewBinding.content.viewTreeObserver.addOnGlobalLayoutListener { val rect = Rect() viewBinding.content.rootView.getWindowVisibleDisplayFrame(rect) val screenHeight = viewBinding.content.rootView.height val keypadHeight = screenHeight - rect.bottom val params = viewBinding.keyboardSpacer.layoutParams if (keypadHeight > screenHeight * 0.15) { params.height = keypadHeight } else { params.height = 0 } viewBinding.keyboardSpacer.layoutParams = params } } <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" android:fitsSystemWindows="false" android:id="@+id/content" tools:context=".component.weblink.WebLinkActivity"> <!-- Header --> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/headerLayout" android:layout_width="0dp" android:layout_height="@dimen/size100" android:background="@drawable/bg_orange_radius_button_40dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/imgBack" android:layout_width="@dimen/size36" android:layout_height="@dimen/size36" android:layout_marginStart="@dimen/size16" android:layout_marginBottom="@dimen/size10" android:src="@drawable/ic_back_start" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/txtTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/size10" android:text="@string/txt_web_link" android:textColor="@color/white" android:textSize="@dimen/size20" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> <!-- EditText --> <EditText android:id="@+id/edtWebLink" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/size16" android:layout_marginStart="@dimen/size16" android:layout_marginEnd="@dimen/size16" android:background="@drawable/bg_white_line_radius_60" android:hint="@string/txt_hint_link" android:inputType="textUri" android:paddingHorizontal="@dimen/size16" android:paddingVertical="@dimen/size12" android:textSize="@dimen/size16" app:layout_constraintTop_toBottomOf="@+id/headerLayout" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" tools:ignore="Autofill" /> <!-- Button Continue --> <TextView android:id="@+id/btnContinue" android:layout_width="0dp" android:layout_height="50dp" android:layout_margin="@dimen/size16" android:background="@drawable/bg_gray_radius_60" android:enabled="false" android:gravity="center" android:text="Continue" android:textColor="#808080" android:textSize="@dimen/size16" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@id/keyboardSpacer" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <View android:id="@+id/keyboardSpacer" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 

1 Comment

While this code may answer the question, might you please edit your post to add an explanation as to why/how it works as suggested by How do I write a good answer? Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.