[android] Move layouts up when soft keyboard is shown?

I have a few elements in a RelativeView with the align bottom attribute set, when the soft keyboard comes up the elements are hidden by the soft keyboard.

I would like them to move up so that if there is enough screen space they are shown above the keyboard, or to make the section above the keyboard scrollable so the user can still see the elements.

Any ideas on how to approach this?

This question is related to android user-interface android-layout android-keypad

The answer is


Alright this is very late however I've discovered that if you add a List View under your edit text then the keyboard will move all layouts under that edittext 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.


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" 

it will be okay


There are 3 steps to follow to scroll screen up.

  1. Write in manifest file for your activity:

android:windowSoftInputMode="adjustResize"

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

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

  1. Then place your whole view in "Scrollview" in XML file like :
<?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>

P.S. Don't forget to add android:layout_gravity="center" in parent Relative layout.


I have found a solution to my problems that it's pretty same as yours.

First of all, I only have a LinearLayout with 2 elements, an ImageView and a LinearLayout wich contains 2 EditText and one Button, so I needed to keep them separate in full screen without keyboard, and when the keyboard appears they should look closer.

So I added the view between them with the height attribute in 0dp and weight 1, that let me keep my views separate one form each other. and when the keyboard appers, as they don't have a fix height, they just resize and keep the aspect.

Voila! The layout resize and the distance between my views is the same when the keyboard is or not present.


similar issue i was facing with my layout which consist scroll view inside. Whenever i try to select text in EditText,Copy/Cut/Paste action bar gets moved up with below code as such it does not resize layout

android:windowSoftInputMode="adjustPan"

By modifying it to as below

1) AndroidManifest.xml

android:windowSoftInputMode="stateVisible|adjustResize"

2) style.xml file ,in activity style

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

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


I just added the following line to the top level layout and everything worked:

android:fitsSystemWindows="true"

if you are in fragments then you have to add the below code to your onCreate on your activity it solves issue for me

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

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

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

  1. If user want this task using manifest then add this line in manifest
  2. In manifest add this line: - android:windowSoftInputMode="adjustPan" e.g.

In Java File or Kotlin File (Programmatically):- If the user wants this using Activity then add the below line in onCreate Method

activity?.window()?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

Make changes in the activity of your Manifest file like

android:windowSoftInputMode="adjustResize"

OR

Make changes in your onCreate() method in the activity class like

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

It might be late but I want to add few things. In Android Studio Version 4.0.1, and Gradle Version 6.1.1, the windowSoftInputMode will not work as expected if you are adding the following flags in your Activity :

getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        );

After wasting lot of time, I have found that hack. In order for adjustResize or AdjustPanor any attribute of windowSoftInputMode to work properly, you need to remove that from your activity.


Use Scroll View as a Parent and use this Tag in Scroll View

android:fillViewport="true"


Add in AndroidManifest.xml for your activity:

android:windowSoftInputMode="adjustPan|adjustResize"

I tried a method of Diego Ramírez, 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>

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

Add NestedScrollView around layout which you want to scroll, this will perfectly work


for Activity in its oncreate methode insert below line

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

and for fragments in its onCreate method

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

Not a single advice solve my issue. Adjust Pan is almost as like work Adjust Resize. If i set Adjust Nothing than layout not move up but it hide my edittext of bottom that i am corrently writing. So after 24 hour of continuous searching I found this source and it solve my problem.


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

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

Here is 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>

This is what worked for me after looking at a lot of solutions:

  1. AndroidManifest.xml

    Add

     android:windowSoftInputMode="stateVisible|adjustResize" 
    

    to an activity

  2. style.xml file,in activity style

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

Props to this post: Scroll entire layout up when keyboard is shown


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to user-interface

Calling another method java GUI How do I center text vertically and horizontally in Flutter? Is it possible to put a ConstraintLayout inside a ScrollView? How to change color of the back arrow in the new material theme? How to create RecyclerView with multiple view type? Android RecyclerView addition & removal of items tkinter: how to use after method Presenting a UIAlertController properly on an iPad using iOS 8 Android ViewPager with bottom dots How do I get the height and width of the Android Navigation Bar programmatically?

Examples related to android-layout

How to check if a key exists in Json Object and get its value How to center the elements in ConstraintLayout Android - how to make a scrollable constraintlayout? Add ripple effect to my button with button background color? This view is not constrained vertically. At runtime it will jump to the left unless you add a vertical constraint Is it possible to put a ConstraintLayout inside a ScrollView? Differences between ConstraintLayout and RelativeLayout How to remove title bar from the android activity? How to have EditText with border in Android Lollipop Android: ScrollView vs NestedScrollView

Examples related to android-keypad

Page scroll when soft keyboard popped up Prevent the keyboard from displaying on activity start How to disable copy/paste from/to EditText Move layouts up when soft keyboard is shown?