18

I'm trying to close the softkeyboard that is opened in another app. I tried every solution from here: Programmatically Hide/Show Android Soft Keyboard or here: Close/hide the Android Soft Keyboard

As you can see in the pictures i have to close the keyboard opened from another app, adding to manifest to don't make the keyboard visible didn't make the trick.

To note that this is a locker app, i start an activity when the phone goes to sleep mode.

Am i missing something ? Testing other locker apps from store and didn't encountered this problem

But here is the result:

App with opened keyboard My app

Edit: More info

This is how i start the locker:

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
    //Toast.makeText(context, "" + "screeen off", Toast.LENGTH_SHORT).show();

    wasScreenOn = false;
    Intent intent = new Intent(context, LockScreenActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    context.startActivity(intent);

    // do whatever you need to do here
    //wasScreenOn = false;
} 

This is the manifest code:

<activity
    android:name=".ui.activities.LockScreenActivity"
    android:excludeFromRecents="true"
    android:noHistory="true"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
Community
  • 1
  • 1
Tazz
  • 781
  • 1
  • 8
  • 23

5 Answers5

4

Try replacing android:windowSoftInputMode="stateAlwaysHidden|adjustNothing" with android:windowSoftInputMode="stateHidden" line in AndroidManifest.xml like this

<activity
        android:name=".ui.activities.LockScreenActivity"
        android:excludeFromRecents="true"
        android:noHistory="true"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

For reference, you can refer http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

"stateHidden" The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.

"stateAlwaysHidden" The soft keyboard is always hidden when the activity's main window has input focus.

Harsh Dattani
  • 2,109
  • 1
  • 17
  • 27
  • same issue :( ... the keyboard doesn't hide if i open the keyboard from sms composer or google search. Tried in apps like whatsapp that works, but it worked with my solution too :( – Tazz Apr 28 '15 at 09:18
2

It can be achieved overriding onPause() of this activity and use following piece of code as

@Override
public void onPause() {
    super.onPause();
    if (null != getWindow()){
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
Narendra
  • 609
  • 1
  • 12
  • 28
1

Try this in your Activity:

private void hideKeyboard() {   
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
Hitesh Singh
  • 1,951
  • 1
  • 10
  • 15
  • http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard/7696791#7696791 – bardi May 04 '15 at 10:40
  • Doesn't work because the view that has the focus is not the view that has opened the softkeyboard. The softkeyboard is opened in another app. – Tazz May 05 '15 at 08:56
1

Try this way

  InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

Check this link

Community
  • 1
  • 1
Jithu P.S
  • 1,843
  • 1
  • 19
  • 32
0

I finally resolved the issue. This is how my manifest code for the activity looks like:

<activity
        android:name=".ui.activities.LockScreenActivity"
        android:excludeFromRecents="true"
        android:noHistory="true"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden"
        android:configChanges="keyboardHidden"
        android:launchMode="singleInstance"
        android:multiprocess="false"
        android:stateNotNeeded="true"
        android:taskAffinity=""
        android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
Tazz
  • 781
  • 1
  • 8
  • 23
  • I tried this one but no effect. Please let me know if need to do something else excepty this – Akarsh M Apr 04 '16 at 09:44
  • @AkarshM I didn't worked for me too, it only worked on some devices, sadly, the project was put on hold so i didn't make any more research – Tazz Apr 07 '16 at 08:06