129

I have a set of navigation buttons sitting at the bottom of each Activity. In some activities, I need a search textview at the top of it. However, whenever the user types something in the textview, the soft keyboard always pushes up my navigation buttons, even though it doesn't actually block the textview. It makes my UI looks funny. How can I force my navigation buttons to stay static where they are without ever being pushed by the soft keyboard? I have tried to set the Activity's windowSoftInputMode, but none of the configurations help.

Any suggestions? Thanks

Jay
  • 1,624
  • 2
  • 14
  • 11

7 Answers7

160

I did have the same problem and at first I added:

<activity
    android:name="com.companyname.applicationname"
    android:windowSoftInputMode="adjustPan">

to my manifest file. But this alone did not solve the issue. Then as mentioned by Artem Russakovskii, I added:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:isScrollContainer="false">
</ScrollView>

in the scrollview.

This is what worked for me.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
droid kid
  • 7,569
  • 2
  • 32
  • 37
  • 4
    Adding the code to the manifest file worked for me. My particular issue was not with a ScrollView, but rather with a LinearLayout. I added this to my manifest: `(android:windowSoftInputMode="adjustPan")` – Jason May 14 '14 at 19:53
  • 1
    in fact adding `isScrollContainer="false"` is a great tip, a lot of people digging in layout or bg itself when facing this issue without having an idea that the ScrollView may be the original producer of it. – Muhammed Refaat Oct 21 '15 at 11:57
  • In case of mine using android:windowSoftInputMode="adjustPan" dither screen. But using android:windowSoftInputMode="adjustNothing" works fine. – Shahidul Nov 16 '15 at 17:35
  • 3
    A not-scrolling scroll view would not be not a proper choice – user3290180 Feb 06 '16 at 19:01
  • only adding isScrollContainer="false" in scroll view solved the problem. No need to add android:windowSoftInputMode in manifest. – Amna Mirza Aug 11 '16 at 11:04
  • much better answer, android:windowSoftInputMode="stateHidden|adjustPan" – Shahab Rauf Aug 19 '16 at 10:37
  • it not working for me – Ram Dec 14 '17 at 07:37
  • this line worked for me `android:windowSoftInputMode="adjustPan"`, Thank You so much! – iamkdblue Nov 16 '19 at 11:39
72

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.

Deepak Swami
  • 3,838
  • 1
  • 31
  • 46
Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115
  • 14
    The name of this property is misleading but eclipse IDE provided a short description: `Set this if the view will serve as a scrolling container, meaing that it can be resized to shrink its overall window so that there will be space for an input method. [boolean]`. Meaning it doesn't do what the name says it does, scrolling behaviour will be preserved – SparK Oct 09 '13 at 19:46
  • This doesn't work with the ViewPager, since the scroll view is inside the pager. – Davideas May 25 '15 at 10:04
  • it also needs `android:windowSoftInputMode="adjustResize"` for your `Activity` in `Manifest` – Logic Dec 22 '15 at 07:30
67

To solve this simply add android:windowSoftInputMode="stateVisible|adjustPan to that activity in android manifest file. for example

<activity 
    android:name="com.comapny.applicationname.activityname"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateVisible|adjustPan"/>
tir38
  • 9,810
  • 10
  • 64
  • 107
Usman Nisar
  • 3,031
  • 33
  • 41
14

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);       
        }
     }
});
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Petro
  • 3,484
  • 3
  • 32
  • 59
  • 1
    This seems to work great! Of course is a trick... IMO. But 1 thing is necessary: listener must be removed in onDestroy: `@SuppressWarnings("deprecation") @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override protected void onDestroy() { if (!Utils.hasJellyBean()) { mRootView.getViewTreeObserver().removeGlobalOnLayoutListener(mGlobalLayoutListener); } else { mRootView.getViewTreeObserver().removeOnGlobalLayoutListener(mGlobalLayoutListener); } super.onDestroy(); }` – Davideas May 25 '15 at 10:36
  • Glad i could help! I'm not understanding this addon though. When the activity gets destroyed so does all the views under it. Unless I'm mistaken, this should destroy the listener because the view it's referencing is null. – Petro May 25 '15 at 14:05
  • I'm not sure, but maybe you could be right http://stackoverflow.com/a/5690468/3397345 – Davideas May 26 '15 at 12:05
  • 2
    Basically the adjustPan works, but ScrollView doesn't work anymore. So to have the ScrollView scrollable *with keyboard*, adjustResize is necessary. I used your solution in a ViewPager (hiding lower buttons) and it seems the only one that works to me. ScrollViews are in each fragment not in the activity. If I put it in the activity ViewPager starts to get problems... – Davideas May 26 '15 at 12:12
  • @Davidea I had a similar issue, which lead me to create this solution. Glad I could help! – Petro Mar 05 '16 at 17:59
  • I see, using above code works fine with android:windowSoftInputMode="adjustPan" in manifest file, hope it saves somebody's time – praveenb May 26 '16 at 11:07
  • Temporay solution not working all deveice – Nitesh Khosla Sep 01 '17 at 11:54
  • @NiteshKhosla what was the issue that you see – sunil Oct 27 '17 at 14:15
  • In my case i needed adjustResize since the view inside need to be scrolled properly and then required a way to hide the footer section. this tweak really helped. – sunil Oct 27 '17 at 14:16
6

windowSoftInputMode will either pan or resize your activity layout. One thing that you can do is to attach an onFocusChanged listener to your EditText and when the user selects/taps the EditText then you hide or move your navigation buttons out of the screen. When the EditText loses focus then you can put the navigation buttons back at the bottom of the activity.

Abhinav
  • 38,516
  • 9
  • 41
  • 49
  • Thank you. This sounds doable. However, my textview is on focus by default when the Activity starts or after the search is performed, so the soft keyboard is not necessary shown whenever the textview is on focus. – Jay Apr 02 '11 at 03:16
  • When the Activity starts you can set android:windowSoftInputMode="stateHidden" in your manifest. After the search is performed, you can move your navigation buttons back. – Abhinav Apr 02 '11 at 10:59
  • 3
    That shouldn't be necessary- you sure you added windowSoftInput to the activity in the *manifest*? http://stackoverflow.com/questions/4301422/android-showing-keyboard-moves-my-components-up-i-want-to-hide-them-instead – Nathan Fig Apr 14 '11 at 13:11
0

I had the same problem, but setting windowSoftInputMode did not help, and I did not want to change the upper view to have isScrollContainer="false" because I wanted it to scroll.

My solution was to define the top location of the navigation tools instead of the bottom. I'm using Titanium, so I'm not sure exactly how this would translate to android. Defining the top location of the navigation tools view prevented the soft keyboard from pushing it up, and instead covered the nav controls like I wanted.

Scott Driscoll
  • 2,869
  • 2
  • 23
  • 22
-4

Solved it by setting the naughty EditText:

etSearch = (EditText) view.findViewById(R.id.etSearch);

etSearch.setInputType(InputType.TYPE_NULL);

etSearch.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        etSearch.setInputType(InputType.TYPE_CLASS_TEXT);
        return false;
    }
});
Exikle
  • 1,155
  • 2
  • 18
  • 42
Golan Shay
  • 1,250
  • 18
  • 15