179

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?

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
Dinedal
  • 2,646
  • 2
  • 19
  • 18
  • Updated reference from [android developers blog](http://android-developers.blogspot.in/2009/04/updating-applications-for-on-screen.html) – Jayabal Oct 22 '12 at 11:50
  • @Dinedal How do u set align bottom attribute to ur RelativeView??? – KJEjava48 Sep 22 '16 at 05:17
  • I posted a simple solution that does not involve adding anything to the manifest. Just a simple layout and android handles the rest. See https://stackoverflow.com/questions/15323758/android-how-to-push-button-above-soft-keyboard/67798240#67798240 – Jeffrey Jun 02 '21 at 03:05

24 Answers24

125

Yes, check out this article on the Android developers' site which describes how the framework handles the soft keyboard appearing.

The android:windowSoftInputMode attribute can be used to specify what happens on a per-activity basis: whether the layout is resized or whether it scrolls etc.

joe
  • 51
  • 6
Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
111

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

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Bob
  • 22,810
  • 38
  • 143
  • 225
  • 20
    For me even if I set adjustPan screnn move only up to the edit text I've clicked but not the whole layout – murt Jul 08 '16 at 08:59
41

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_RESIZE);
Mahmoud Mabrok
  • 1,362
  • 16
  • 24
Jose Kurian
  • 703
  • 7
  • 10
37

Add in AndroidManifest.xml for your activity:

android:windowSoftInputMode="adjustPan|adjustResize"
Serhii Bohutskyi
  • 2,261
  • 2
  • 28
  • 28
  • 28
    According to docs the behaviour is unspecified if you use two "adjust..." flags here. See http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft – joe1806772 Apr 15 '15 at 12:35
24

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

    true

It worked for me.

Ido Barnea
  • 142
  • 2
  • 13
Ronak Poriya
  • 2,369
  • 3
  • 18
  • 20
20

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

Zahra.HY
  • 1,684
  • 1
  • 15
  • 25
15

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);
Rahul_Pawar
  • 572
  • 1
  • 8
  • 16
11

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.

Vlad
  • 110
  • 1
  • 5
Heena Arora
  • 739
  • 8
  • 18
9
<activity name="ActivityName"
    android:windowSoftInputMode="adjustResize">
    ...
</activity>

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

Prakash Reddy
  • 944
  • 10
  • 20
6

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>
CoolMind
  • 26,736
  • 15
  • 188
  • 224
6

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

In AndroidManifest.xml add the following line to an activity declaration in the manifest file of your project.

android:windowSoftInputMode="stateHidden|adjustPan" 

The code should look something like this:

    <activity
        android:name=".MainActivity"
        android:screenOrientation="landscape"
        android:windowSoftInputMode="stateHidden|adjustPan">
    </activity>
Martin Vichev
  • 69
  • 1
  • 4
4

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

android:fitsSystemWindows="true"
Darush
  • 11,403
  • 9
  • 62
  • 60
  • 1
    @yash786 is possibly show button below edittext that receive a focus ? Second question can you share you layout file or answer me how you add edittext a top of keybord ? – Heroes84 Oct 11 '19 at 07:01
  • it works for me event without android:windowSoftInputMode="stateVisible|adjustResize" – zoom Dec 08 '19 at 05:12
3

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.

  • 8
    Why don't you post the code, for better comprehension? – johnkarka May 20 '15 at 00:33
  • I posted a sample code above: https://stackoverflow.com/a/31994697/2914140. – CoolMind Oct 17 '18 at 09:11
  • you can achieve this in a less hacky way by using a `ConstraintLayout` and pinning a top section to the top and a bottom section to the bottom. When the keyboard opens it will naturally resize by squeezing the space in the middle (if you use `adjustResize`) – hmac Jan 14 '20 at 10:17
3
  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)
OhhhThatVarun
  • 3,981
  • 2
  • 26
  • 49
safal bhatia
  • 195
  • 1
  • 5
  • Hi and welcome to stackoverflow, and thank you for your answer. Rather than just posting a block of code, can you give a short explanation to what the issue is you solved and how you solved it? This will help people who find this question in the future to better understand the issue and how to deal with it. – Plutian Dec 03 '19 at 09:33
2

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>
Dantalian
  • 561
  • 6
  • 15
2

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

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

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);
duggu
  • 37,851
  • 12
  • 116
  • 113
Pratik Gondil
  • 689
  • 1
  • 6
  • 17
1

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

android:fillViewport="true"

Tippu Fisal Sheriff
  • 2,177
  • 11
  • 19
1

I tried a lot of methods but at the I added this line in the AndroidManifest.xml file and it worked perfectly

<activity
      ...
      android:windowSoftInputMode="adjustPan|adjustResize"
0

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.

Omar Boshra
  • 447
  • 7
  • 21
0

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.

0

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.

Sharryy
  • 224
  • 4
  • 11
0

Add this line to the view:

view.requestFocus();
Antoine
  • 1,393
  • 4
  • 20
  • 26
0

In my case, I wanted the screen contents to be pushed up with the soft keyboard only in one fragment but not in all the activity fragments.

So I dropped the solution of window params.

My solution is,

  1. wrap the contents to be pushed up inside the Scrollview.

  2. in the fragment onResume() add the below code,

    val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    
     // listen for the keyboard popping up and going down..
     requireView().viewTreeObserver.addOnGlobalLayoutListener {
         // Check if the ScrollView is scrolled to the bottom
         val isScrolledToBottom =
             binding.scrollView.scrollY >= binding.scrollView.getChildAt(0).height - binding.scrollView.height
    
         if (imm.isActive && !isScrolledToBottom) {
             binding.scrollView.smoothScrollTo(
                 0,
                 binding.scrollView.bottom
             )
         }
     }
    

This will scroll the contents up when the keyboard is shown very similar to the Activity window_adjust_pan flag.

Willey Hute
  • 939
  • 13
  • 18