Move layouts when soft keyboard is displayed?

I have several elements in RelativeView with attribute of lower attribute align, when soft keyboard appears, elements are hidden using soft keyboard.

I would like them to move upward so that with sufficient screen space they are shown above the keyboard or so that a scroll scrolls over the section above the keyboard so that the user can still see the elements.

Any ideas on how to approach this?

+121
android android-layout user-interface android-keypad
Dec 27 '09 at 0:40
source share
16 answers

Yes, check out this article on the Android Developers website for a description of how the framework handles the appearance of a soft keyboard.

The android:windowSoftInputMode can be used to indicate what happens based on activity: whether the layout changes or it scrolls.

+92
Dec 27 '09 at 1:44
source share

You can also use this code in the onCreate() method:

 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 
+74
Feb 25 '13 at 9:13
source share

Add to AndroidManifest.xml for your activity:

 android:windowSoftInputMode="adjustPan|adjustResize" 
+30
Oct. 16 '13 at 15:00
source share

Make changes to the activity of your manifest file, for example

 windowSoftInputMode="adjustResize" 

OR

Make changes to your onCreate() method in the activity class, e.g.

 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 
+21
Apr 30 '14 at 6:57
source share

A similar problem I came across with a layout consisting of scrolling inside. Whenever I try to select text in EditText, the Copy / Cut / Paste action bar moves up using the code below, since it doesn't resize the layout

 android:windowSoftInputMode="adjustPan" 

Changing it as below

1) AndroidManifest.xml

 android:windowSoftInputMode="stateVisible|adjustResize" 

2) style.xml file, in action style

 <item name="android:windowActionBarOverlay">true</item> 

It worked. !!!!!!!!!!!!

+16
Jul 31 '15 at 9:00
source share

In AndroidManifest.xml, don't forget to set:

  android:windowSoftInputMode="adjustResize" 

and for RelativeLayout inside ScrollView set:

  android:layout_gravity="center" or android:layout_gravity="bottom" 

everything will be fine

+12
Jun 29. '16 at 7:27
source share

for activity in its built-in insert method below the line

 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 

and for fragments in the onCreate method

 getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 
+7
Mar 10 '16 at 10:58
source share

There are 3 steps to scroll up the screen.

  1. Write in the manifest file for your activity:

android: windowSoftInputMode = "adjustResize"

  1. Add the following line to the oncreate () method of your activity

GetWindow () setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE).

  1. Then put the whole view in a "Scrollview" in an XML file, for example:
 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:background="@drawable/bg" android:layout_height="match_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_gravity="center" > <TextView /> ......... <TextView /> <EditText > <requestFocus /> </EditText> <Button /> </RelativeLayout> </ScrollView> 

PS Remember to add android: layout_gravity = "center" to the parent relative layout.

+6
Oct 05 '16 at 7:04
source share

I tried the Diego Ramirez method, it works. In AndroidManifest:

  <activity android:name=".MainActivity" android:windowSoftInputMode="stateHidden|adjustResize"> ... </activity> 

In activity_main.xml:

 <LinearLayout ... android:orientation="vertical"> <Space android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" /> <EditText android:id="@+id/edName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="@string/first_name" android:inputType="textPersonName" /> <EditText ...> ... </LinearLayout> 
+5
Aug 13 '15 at 17:21
source share

I found a solution to my problems that it is very similar to yours.

First of all, I only have LinearLayout with two elements, ImageView and LinearLayout, which have 2 EditText and one button, so I had to keep them separate in full screen mode without a keyboard, and when the keyboard appears, they should look closer.

So, I added a view between them with the height attribute in 0dp and a weight of 1, which allowed me to keep separate views from each other. and when the keyboard appears, since they do not have a correction height, they simply resize and retain the aspect.

Voila! The layout size changes, and the distance between my views is the same when the keyboard is present or absent.

+4
Dec 10 '14 at 22:05
source share

Here is the full manifest code

 <activity android:name=".activities.MainActivity" android:windowSoftInputMode="adjustResize" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 
+1
Jul 31 '15 at 6:25
source share

if you are in fragments then you should add the code below to your onCreate in your activity, it solves the problem for me

 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 
+1
Jul 07 '17 at 11:15
source share

In your manifest file, add the line below. It will work

 <activity name="MainActivity" android:windowSoftInputMode="stateVisible|adjustResize"> ... </activity> 
+1
Feb 03 '19 at 18:43
source share

Well, this is very late, but I found that if you add a list view under your edit text, the keyboard will move all layouts under that edit text without moving the ListView.

 <EditText android:id="@+id/et" android:layout_height="match_parent" android:layout_width="match_parent" /> <ListView android:layout_width="match_parent" android:layout_height="match_parent"//can make as 1dp android:layout_below="@+id/et" // set to below editext android:id="@+id/view" > **<any layout** 

This is the only solution that worked for me.

0
Jul 01 '18 at 1:44
source share

enter image description here

 Here is the solution for this fix - android:windowSoftInputMode="adjustResize" in Manifest File - Make a class "CommentKeyBoardFix.Java" and copy and paste the below code. public class CommentKeyBoardFix { private View mChildOfContent; private int usableHeightPrevious; private FrameLayout.LayoutParams frameLayoutParams; private Rect contentAreaOfWindowBounds = new Rect(); public CommentKeyBoardFix(Activity activity) { FrameLayout content = activity.findViewById(android.R.id.content); mChildOfContent = content.getChildAt(0); mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent); frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams(); } private void possiblyResizeChildOfContent() { int usableHeightNow = computeUsableHeight(); if (usableHeightNow != usableHeightPrevious) { int heightDifference = 0; if (heightDifference > (usableHeightNow /4)) { // keyboard probably just became visible frameLayoutParams.height = usableHeightNow - heightDifference; } else { // keyboard probably just became hidden frameLayoutParams.height = usableHeightNow; } mChildOfContent.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom); mChildOfContent.requestLayout(); usableHeightPrevious = usableHeightNow; } } private int computeUsableHeight() { mChildOfContent.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds); return contentAreaOfWindowBounds.height(); } } And then call this class in your Activity or Fragment setContentView(R.layout.your_comments_layout) CommentKeyBoardFix(this) ---> For Kotlin or new CommentKeyBoardFix(this) ---> For Java 
0
Jan 21 '19 at 11:04
source share

I just added the following line to the top-level layout, and it worked:

 android:fitsSystemWindows="true" 
0
Jun 27 '19 at 8:54 on
source share



All Articles