83

I want to act my tabs to have different windowSoftInputMode properties for each tab. How to access this property from java class when all handling of your tab is done from one single activity?

Is there any way to access this manifest property from java code?

Prasham
  • 6,521
  • 8
  • 35
  • 54

5 Answers5

126

Use the following to change the softInputMode for an Activity.

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

Use the following to change the softInput type for an EditText.

mEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);

Thanks to @Eliezer for correction

AjOnFire
  • 2,818
  • 3
  • 23
  • 39
  • 20
    Thanks for this setSoftInputMode method... It really helped me... Additional info: To access properties like `adjustPan` and `adjustResize` you can go for `WindowManager.LayoutParams` class, you can find many useful constants that can be used in `setSoftInputMode` method – Prasham May 26 '11 at 13:33
  • @66CLSjY Thanks , However if i want to set it again to android:windowSoftInputMode="stateAlwaysVisible" then what will be the imeaction for it ? – Bora Jun 27 '13 at 10:54
  • 7
    @SureshBora i combined @66CLSjY answer and @aman and i finally got this: `getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);` I already tested it, and really works – Humberto Castañeda Jul 02 '13 at 15:43
  • 1
    How is this the correct answer? As per https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#softInputMode the only acceptable values to pass `Window#setSoftInputMode` are `WindowManager.LayoutParams.SOFT_INPUT_*` – Eliezer Aug 13 '15 at 20:20
  • 4
    @66CLSjY It doesn't work for me. Can you tell me what am I missing? I call this inside an activity: `getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);` But the keyboard still pushes up my FrameLayout, which is on top of other views. But just that, other views remain in their place. It works, when I add it in the Manifest, but I don't want to apply it for the whole Activity. – Tamás Kozmér Jan 19 '17 at 14:50
  • 1
    Did you call setSoftInputMode before setContentView in onCreate()? – AjOnFire Jan 02 '19 at 07:56
  • 1
    NOTE: `getWindow().setSoftInputMode(...);` only works in activity before you call `setContentView(R.layout...);` because it changes all the "controls"/"Views" on the layout when the content is loaded. – Pierre Oct 08 '19 at 05:00
36

According to Prasham's comment, I did this and it saved my life, thanks to him! The EditText and SoftWindowInput mode are quite buggy when you have a layout with ScrollView and you are filling it dynamically.

Since I had gone through this post but had continued to read other answers/comments (like Prashan's one), I decided to write it in a new post.

Below the code I used with my ScrollView:

Activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Community
  • 1
  • 1
aman
  • 418
  • 4
  • 7
  • 8
    hi aman, it's a little difficult to read your answer here. i can see you're very grateful to Prashan, but can you please clear out some of the personal comments and just explain what this code will help to solve? thanks. – Taryn East Jan 22 '13 at 07:18
  • 1
    Hm, doesn't work, at least to set to adjust pan. It works only if I put it in the manifest. – Ixx Dec 01 '13 at 15:00
  • This is crashing the application. – Farrakh Javed Aug 20 '14 at 09:46
10

I aim to use two different modes for tabs. The modes are SOFT_INPUT_ADJUST_RESIZE and SOFT_INPUT_ADJUST_NOTHING.

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

The code line above simply doesn't work by itself. App always behaves in a state ADJUST_NOTHING. However, if windowSoftInputMode="adjustResize" is inserted into <activity> tag in AndroidManifest.xml file, app window is resized as default. Additionally, when you call following line

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

it will behave as expected and nothing is resized.

P.S. improvement to the answer

Shnkc
  • 1,968
  • 1
  • 25
  • 31
  • 1
    I need to shuffle between RESIZE and NOTHING how can in do so once I go back to nothing programatically it does does not change to resize. – Taranmeet Singh Jul 11 '16 at 14:01
  • 1
    This is really nice observation. Basically setting `windowSoftInputMode="adjustResize"` in the manifest helps in handling this flag later. Specially in a scenario where, you have mostly Fragments in Single Activity. So, one can dynamically keep changing the Activity's input state by programatically switching from `SOFT_INPUT_ADJUST_RESIZE ` back to `SOFT_INPUT_ADJUST_NOTHING ` on demand. Nice one! – sud007 Mar 30 '17 at 13:05
1

In Xamarin Android You Can Do programatically Like This

 protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.DetailDesign);
             Window.SetSoftInputMode(SoftInput.AdjustPan);
        }
zubairz
  • 374
  • 1
  • 18
0

You can use the following code programmatically

android.view.inputmethod.InputMethodManager imm = (android.view.inputmethod.InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

Thanks Deepak

Sunil Kumar Sahoo
  • 49,865
  • 50
  • 172
  • 240