484

I have a vertical sliding drawer at the bottom of my app. When the soft keyboard opens, it pushes the tab for the drawer up, so it sits atop the keyboard. I actually want it to remain at the bottom of the screen, becoming hidden when the keyboard is shown.

Anyone else run into this issue? Know how to fix it?

Christopher Perry
  • 38,891
  • 43
  • 145
  • 187
  • 1
    hi, I am having same problem which flag you have used in manifest file, can you plz help me with that – Nixit Patel Jun 11 '12 at 05:05
  • 1
    This question is duplicated here: http://stackoverflow.com/questions/5516216/how-to-avoid-soft-keyboard-pushing-up-my-layout – WonderWorker Apr 17 '13 at 07:30

29 Answers29

781

You can simply switch your Activity's windowSoftInputModeflag to adjustPan in your AndroidMainfest.xml file inside your activity tag.

Check the official documentation for more info.

<activity
   ...
   android:windowSoftInputMode="adjustPan"> 
</activity>

If your container is not changing size, then you likely have the height set to "match parent". If possible, set the parent to "Wrap Content", or a constraint layout with constraingts to top and bottom of parent.

The parent container will shrink to fit the available space, so it is likely that your content should be inside of a scolling view to prevent (depending on the phone manufacturer and the layout choosen...)

  1. Content being smashed together
  2. Content hanging off the screen
  3. Content being inacccessable due to it being underneath the keyboard

even if the layout it is in is a relative or constraint layout, the content could exhibit problems 1-3.

Watachiaieto
  • 417
  • 3
  • 10
Alexander Oleynikov
  • 19,190
  • 11
  • 37
  • 51
  • 61
    I don't want the view to pan, I want it to remain static where it is, and just have the keyboard cover up whatever it's going to cover up. – Christopher Perry Nov 17 '10 at 23:41
  • 5
    By panning it's meant that view will be scrolled to make visible that view which you're typing to. In other words if just appeared keyboard didn't cover up focused view it will remain static. – Alexander Oleynikov Nov 18 '10 at 20:42
  • 19
    I am facing the same problem. made the windowSoftInputMode flag to "adjustPan" in manifeast file.but not working . how to solve it – Subrat May 31 '11 at 10:23
  • 24
    Doesn't help in my case of having a row of buttons on the bottom of the screen and a ScrollView above them. The buttons get pushed up no matter what value is in windowSoftInputMode. – Artem Russakovskii Oct 17 '11 at 21:44
  • @ArtemRussakovskii have you found a solution to this? I still have the problem. – Gibberish Sep 03 '14 at 20:49
  • Working great, but you view layout must be != LinearLayout, I didn't check other layouts, I wrap my view into RelativeLayout and now works – buxik Nov 17 '14 at 11:14
  • 3
    I found that this general method works as long as your activity is not full screen – kip2 Jan 15 '15 at 22:58
  • 1
    This actually didn't work for mi adjustResize ended up working after taking a look at the layout and seeing that there was a useless relative layout in there. – Doug Ray Feb 12 '16 at 03:26
  • 4
    not working in a fragment. How to do it in fragment? – Prashanth Debbadwar Apr 19 '16 at 07:26
  • @AlexanderOleynikov i am facing problem similar this. help me to solve it. – Vishal Thakkar Sep 17 '16 at 13:12
  • I've used softkeyboard in my fragment but doing this in parent activity solved the problem. – Mahdi Jul 28 '17 at 05:49
  • Please help to avoid scroll up of `Toolbar` when keyboard is shown. Just using `adjustPan` doesn't work for me. – Konstantin Konopko Mar 04 '18 at 19:03
  • I am writing android:windowSoftInputMode="adjustPan" inside my application tag to prevent layout changes when keyboard opens up, but it didnot work. can you tell me if there is a way to prevent keyboard from changing UI for entire app? – Jay Dangar Aug 28 '18 at 12:08
  • Praise the lord I'm so happy. I've been searching up and reading up on this and it seemed like I had to make a whole new activity and then I'd need a landscape version too and need to copy over all my xml, or I'd have to hide my buttons and there's no setting a class so I'd have to hide all 18 of them in a function or just put them in a container but it's a constraint layout so they're all relative to each other and I can't easily do that without messing everything up, and either way I'd have to figure out the event for setting them back up like on search or keyboard close... PRAISE – Justin Nov 25 '19 at 07:58
238

None of the answers worked for me, but this did the trick, add this attribute to the activity tag in your AndroidManifest.xml:

<activity
     ...
   android:windowSoftInputMode="adjustNothing"> 
</activity>
lasec0203
  • 2,422
  • 1
  • 21
  • 36
Daniel Douglas
  • 3,283
  • 1
  • 16
  • 17
  • 7
    This makes keyboard appear overlaying content, without making layout to recalculate it's height. – Reinherd Jun 04 '14 at 09:40
  • 27
    This solves the issue of the keyboard pushing the content up though which is what I and the OP wanted to achieve. – Daniel Douglas Jul 01 '14 at 15:32
  • 8
    borrowing from @Dom you can use `getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);` to set this value programmatically – Bron Davies Sep 18 '15 at 05:32
  • 1
    This one does work for me, unlike other answers (jelly bean 4.1) – Virus721 Dec 10 '15 at 09:52
  • 1
    Perfect! It is also working in my case. I am showing a dialog that has a edittext inside. As soon as dialog opens, keyboard also was appearing. I have tried "adjustPan", but it blocked keyboard appearing feature. So, "adjustNothing" worked better in my case. – sembozdemir Jan 28 '16 at 14:03
  • not working in a fragment. How to do it in fragment? I tried getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHI‌​NG); this also not working. – Prashanth Debbadwar Apr 19 '16 at 09:17
  • @PrashanthDebbadwar if you'd like to use it in `fragment`, use it like this `getActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT‌​_INPUT_ADJUST_NOTHI‌‌​​NG);` `getActivity` will associate that particular `fragment` with its `activity` – jlively May 16 '17 at 18:50
  • This one worked for me. If you have a dialog at the front of the activity. – André Luiz Reis Apr 05 '18 at 21:58
  • This worked for me rather than `adjustResize`. My Activity has a child viewpager which has 3 child fragments. I needed to combine `statehidden` to prevent keyboard from popping up too. Worked like a charm – grantespo Jun 20 '19 at 03:25
  • It's working, but how i can scroll view when kayboard is attached? This is cover my buttons on bottom side of layout – AlexS Jul 12 '19 at 12:56
96

In my case, the reason the buttons got pushed up was because the view above them was a ScrollView, and it got collapsed with the buttons pushed up above the keyboard no matter what value of android:windowSoftInputMode I was setting.

I was able to avoid my bottom row of buttons getting pushed up by the soft keyboard by setting android:isScrollContainer="false" on the ScrollView that sits above the buttons.

duggu
  • 37,851
  • 12
  • 116
  • 113
Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115
  • 4
    But in this case the soft keyboard covers part of the screen and there's no way to scroll down. Looks like there is only one way to prevent bottom panel to show up at the top of the keyboard - hiding it when keyboard is open... – Dmitry Jul 16 '12 at 21:22
  • 1
    this is it! This setting is in my opinion very interesting to remark, as is the only way I have found to achieve the effects of "adjustNothing" missing property! – rupps Mar 03 '13 at 23:52
  • FYI to everyone, this also applies to elements that contain scrolling behavior, such as `listview` – Ruchir Baronia Jul 09 '19 at 16:00
  • Just to add a note, for `WebViews` you'll also need to add `android:windowSoftInputMode="adjustPan"> ` besides `android:isScrollContainer="false"` – GuilhE Nov 26 '19 at 18:19
91

You can try to add this attribute dynamically, by putting the following code in the onCreate method of your activity:

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

This worked for me, but that:

android:windowSoftInputMode="adjustPan"

didnt.

Mr. Ace
  • 335
  • 3
  • 14
Dom
  • 1,427
  • 1
  • 12
  • 16
  • Did you added the `android:windowSoftInputMode="adjustPan"` in AndroidManifest.xml? – Mohammed H Apr 29 '14 at 11:11
  • No, as it didnt really make any difference for me. The first line of code did the job. :) @HabeebPerwad – Dom Jun 08 '14 at 21:59
  • I added the seconds line in AndoidManifest.xml file. I think after that only it worked. so just asked. – Mohammed H Jun 09 '14 at 04:50
  • 2
    @Dom, how to use "`getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);`" in a fragment? – Tushar Gogna Sep 12 '14 at 05:05
  • @TusharGogna I only used in classes where there were both Activity and Fragment class, so I used this method in onCreate() method of the Activity class. If I find out how to do that in fragment, I´ll let you know. – Dom Oct 08 '14 at 12:00
  • 1
    Finally! This works! I had a controller (extends from `FragmentActivity`) and some views attached to it (the views extend from `RelativeLayout`). So in the troublesome view i did `controller.getWindow()...` like you said. Works – Alaa M. Jul 20 '15 at 21:25
  • Boom, this did it for me. I also tried the windowSoftInputmode but it wasnt available an available autocomplete option activity file, so I added it into onCreate and success. Thanks :) – JazzyJ Sep 11 '20 at 19:48
47

These answers here didn't help me. So I tried this:

android:windowSoftInputMode="adjustResize"

This worked like a charm, Now the header of my app doesn't disappear. Its smoother.

amalBit
  • 12,041
  • 6
  • 77
  • 94
  • This nearly works perfectly for me. The only problem is that the keyboard's suggestion bar/row still hides my layout. Did you face such an issue? – Sufian Aug 29 '18 at 07:35
  • 1
    For those who may face similar issue, it probably was due to `TabLayout`. The problem was gone once I set its visibility to `View.GONE`. It might be time to redesign my layout. – Sufian Sep 08 '18 at 18:02
  • This one works without overlap the button at the bottom of my nested scroll view. The "adjustPan" however overlap the bottom button – Hoang Thinh Jun 11 '22 at 18:41
14

To do this programatically in a fragment you can use following code

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

Place this in onResume()

ChaturaM
  • 1,507
  • 18
  • 32
13

This one worked for me

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
Suresh Pattu
  • 6,083
  • 16
  • 59
  • 91
11

Just a single line to be added...

Add android:windowSoftInputMode="stateHidden|adjustPan" in required activity of your manifest file.

I just got solved :) :)

duggu
  • 37,851
  • 12
  • 116
  • 113
Sravani
  • 528
  • 1
  • 8
  • 13
11

For future readers.

I wanted specific control over this issue, so this is what I did:

From a fragment or activity, hide your other views (that aren't needed while the keyboard is up), then restore them to solve this problem:

            rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    rootView.getWindowVisibleDisplayFrame(r);
                    int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top);

                    if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                    //ok now we know the keyboard is up...
                        view_one.setVisibility(View.GONE);
                        view_two.setVisibility(View.GONE);

                    }else{
                    //ok now we know the keyboard is down...
                        view_one.setVisibility(View.VISIBLE);
                        view_two.setVisibility(View.VISIBLE);

                    }
                }
            });
Petro
  • 3,484
  • 3
  • 32
  • 59
  • 1
    @Skynet You're absolutely right! This was a custom solution that I used for my own problem. – Petro Jan 18 '16 at 18:57
  • 8
    Hey, this was brilliant. However, the instance the keyboard is closed, there is a short period (milliseconds) where the view is switched to visible. So instead of setting the visibility directly, I used `view.post(Runnable runnable)` and set the visibility from there. It turned out great for me – daisura99 Sep 21 '16 at 09:35
  • 1
    @Andrew I switched to Flutter and couldn't be happier, the code is much cleaner too – Petro Sep 21 '20 at 12:51
  • 1
    Yeah, this will be my last android project for ever. After that I will switch to iOS or just stop programming apps at all. This is hideous. – Andrew Sep 21 '20 at 13:04
  • 1
    @Andrew Adobe XD has an export to Flutter function, check it out... might make your life easier – Petro Sep 23 '20 at 13:51
9

So far the answers didn't help me as I have a button and a textInput field (side by side) below the textView which kept getting hidden by the keyboard, but this has solved my issue:

android:windowSoftInputMode="adjustResize"
9

For xamarin users add this code to Activity attribute of the MainActivity class,

WindowSoftInputMode =Android.Views.SoftInput.AdjustNothing

or you can add this code Window.SetSoftInputMode(Android.Views.SoftInput.AdjustNothing) to the OnCreate method of MainActivity class.

Tushar patel
  • 3,279
  • 3
  • 27
  • 30
8

This was the best which worked for me

android:windowSoftInputMode="adjustNothing"

Try it!

Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67
rajlaxmi_jagdale
  • 1,370
  • 15
  • 16
7

Add following code to the 'activity' of Manifest file.

android:windowSoftInputMode="adjustResize"
Jose Kurian
  • 703
  • 7
  • 10
  • This works well. However, just to advice, it doesn´t work well when you use percent. Because it readjust some elements. So, if you have Constraints elements with percent you will have problem. Use wrap_content instead and it will works very well. – Thiago Silva Jun 01 '20 at 14:16
7
android:windowSoftInputMode="stateHidden|adjustNothing"

This code works.

JoeG
  • 7,191
  • 10
  • 60
  • 105
amyShamna
  • 962
  • 8
  • 8
6

Well i have watched these answers but in my case i fell into the same issue and got refuge through a very handy and easiest solution that involves putting a very small innocent attribute in your Scrollview tag residing in your xml file. That is

android:isScrollContainer="false"

Good luck!

Ali Nawaz
  • 2,016
  • 20
  • 30
4
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

This one is working for me.

Zoe
  • 27,060
  • 21
  • 118
  • 148
3

I have solved my issue by adding

 android:windowSoftInputMode="adjustNothing" 

In manifest file add.

and making the Recyclerviews constraint isScrollContainer to false .

android:isScrollContainer="false"
2

For Scroll View:

if after adding android:windowSoftInputMode="stateHidden|adjustPan" in your Android Manifest and still does not work.

It may be affected because when the keyboard appears, it will be into a scroll view and if your button/any objects is not in your scroll view then the objects will follow the keyboard and move its position.

Check out your xml where your button is and make sure it is under your scroll View bracket and not out of it.

Hope this helps out. :D

duggu
  • 37,851
  • 12
  • 116
  • 113
Yils
  • 21
  • 1
2

Try to use this:

android:windowSoftInputMode="stateHidden|adjustPan"
arghtype
  • 4,376
  • 11
  • 45
  • 60
2

In my case I needed the keyboard to stay hidden and just after the click of the button my layout needs to be adjusted, so I just added this command in the manifest and it got super right.

android:windowSoftInputMode="stateHidden|adjustResize"
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
2

When you want to hide view when open keyboard.

Add this into your Activity in manifest file

android:windowSoftInputMode="stateHidden|adjustPan"
Petro
  • 3,484
  • 3
  • 32
  • 59
Hakim Khan
  • 317
  • 5
  • 9
2

None of them worked for me, try this one

 private void scrollingWhileKeyboard() {
    drawerlayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            Rect r = new Rect();
            try {
                drawerlayout.getWindowVisibleDisplayFrame(r);
                int screenHeight = drawerlayout.getRootView().getHeight();
                int keypadHeight = screenHeight - r.bottom;
                if (keypadHeight > screenHeight * 0.15) {
                    tabLayout.setVisibility(View.GONE);
                } else {
                    tabLayout.setVisibility(View.VISIBLE);
                }
            } catch (NullPointerException e) {

            }
        }
    });

}
Saljith Kj
  • 125
  • 2
  • 4
2

I was struggling for a while with this problem. Some of the solutions worked however some of my views where still being pushed up while others weren't... So it didn't completely solve my problem. In the end, what did the job was adding the following line of code to my manifest in the activity tag...

android:windowSoftInputMode="stateHidden|adjustPan|adjustResize"

Good luck

Johan Fick
  • 375
  • 2
  • 14
1

The activity's main window will not resize to make room for the soft keyboard. Rather, the contents of the window will be automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing.

android:windowSoftInputMode="adjustPan"

This might be a better solution for what you desired.

1

This code may help you. Use it in your oncreate method.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Radouane ROUFID
  • 10,595
  • 9
  • 42
  • 80
  • I tried this but I am having issues where the view cuts off a bit of the bottom part of the view and pushes the view to overlay the time above? Any ideas on what layout changes or what is needed? http://stackoverflow.com/questions/39909897/how-to-push-up-the-whole-view-when-a-keyboard-comes-up-what-layout-changes-need – Lion789 Oct 07 '16 at 06:47
1

Include in your manifest file under activity which you want to display .But make sure not using Full screen Activity

android:windowSoftInputMode="adjustPan"
Manohar
  • 22,116
  • 9
  • 108
  • 144
1

@manifest in your activity:

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
Juan Mendez
  • 2,658
  • 1
  • 27
  • 23
1

There are two ways of solving this problem. Go to the AndroidManifist.xml, and in the name of your activity, add this line

   android:windowSoftInputMode="adjustPan"

As in the below code, I have added to the Register Activity.

 <activity android:name=".Register"
            android:windowSoftInputMode="adjustPan">

In the second way, go to your activity, and in your onCreate method, add this code.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Ramzan
  • 191
  • 2
  • 11
0

I was using transparent status bar so nothing worked for me. Instead of transparent statusbar hiding the status bar worked for me

window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

and added this for activity in manifest

android:windowSoftInputMode="adjustResize"

Worked for me