Is there any way to change android:windowSoftInputMode value from java class?
Asked Answered
U

5

86

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?

Unchristian answered 26/5, 2011 at 12:17 Comment(0)
H
129

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

Homothermal answered 26/5, 2011 at 12:32 Comment(7)
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 methodUnchristian
@66CLSjY Thanks , However if i want to set it again to android:windowSoftInputMode="stateAlwaysVisible" then what will be the imeaction for it ?Larceny
@SureshBora i combined @66CLSjY answer and @Lightner and i finally got this: getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); I already tested it, and really worksJerricajerrie
How is this the correct answer? As per developer.android.com/reference/android/view/… the only acceptable values to pass Window#setSoftInputMode are WindowManager.LayoutParams.SOFT_INPUT_*Firmament
@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.Zales
Did you call setSoftInputMode before setContentView in onCreate()?Homothermal
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.Pungy
L
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);
Lightner answered 22/1, 2013 at 6:56 Comment(3)
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.Polygenesis
Hm, doesn't work, at least to set to adjust pan. It works only if I put it in the manifest.Ricketts
This is crashing the application.Receptacle
S
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

Sporty answered 23/12, 2014 at 14:47 Comment(3)
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.Gascony
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!Agitato
SOFT_INPUT_ADJUST_RESIZE is deprecatedOldtime
P
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);
        }
Premonish answered 30/4, 2019 at 7:32 Comment(0)
K
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

Kuhlmann answered 26/5, 2011 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.