1

If I may to ask almost the same question here from this topic

I've added in my activity_main.xml file:

 android:focusable="true"
 android:focusableInTouchMode="true"

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context="com.example.stefancvetkovic.stefantest001.MainActivity">

<EditText
    android:id="@+id/txtScanedResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="16dp" />
</android.support.constraint.ConstraintLayout>

And it works fine, but when I want to hide my keyboard on finish event, the keyboard stays opened.

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ((EditText)findViewById(R.id.txtScanedResult)).setOnEditorActionListener(
            new EditText.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                            actionId == EditorInfo.IME_ACTION_DONE ||
                            event != null &&
                                    event.getAction() == KeyEvent.ACTION_DOWN &&
                                    event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                        if (event == null || !event.isShiftPressed()) {

                            // the user is done typing.
                                //HIDE KEYBOARD
                            EditText edtView=(EditText)findViewById(R.id.txtScanedResult);
                            edtView.setInputType((InputType.TYPE_NULL));
                                //
                            Toast.makeText(getApplicationContext(),"Voila!",Toast.LENGTH_SHORT)
                                    .show();
                            return true; // consume.
                        }
                    }
                    return false; // pass on to other listeners.
                }
            });
}

Toast works perfectly on finish event, but keyboard stays opened. Hoiw can I manage to be initialy closed keyboard on the load, and to be hidden on finishEvent? I am running in emulator on Android 5.1

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Stefan0309
  • 1,602
  • 5
  • 23
  • 61

3 Answers3

2

Try this one

/**
     * This function is used to hide soft keyboard
     *
     * @param context mContext
     * @param view    view for which keyboard is open
     */
    public static void hideSoftInput(Context context, View view) {
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (!inputMethodManager.isActive()) {
                return;
            }
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * This function is used to hide soft keyboard
     *
     * @param activity activity
     */
    public static void hideSoftInput(Activity activity) {
        try {
            if (activity != null) {
                View focusedView = activity.getCurrentFocus();
                hideSoftInput(activity, focusedView);
            }
        } catch (Throwable t) {
            CustomLogHandler.printErrorlog(t);
        }
    }

    /**
     * This function is used to show soft keyboard
     *
     * @param activity activity
     */
    public static void showSoftInput(Activity activity) {
        try {
            if (activity != null) {
                View focusedView = activity.getCurrentFocus();
                if (focusedView != null) {
                    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(focusedView, InputMethodManager.SHOW_IMPLICIT);
                }
            }
        } catch (Throwable t) {
            CustomLogHandler.printErrorlog(t);
        }
    }

    /**
     * This function is used to show soft keyboard
     *
     * @param view view for which soft keyboard need to be opened
     */
    public static void showSoftInput(final View view) {
        try {
            if (view == null) {
                return;
            }

                    view.requestFocus();
                    InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputManager.showSoftInput(view, 0);

        } catch (Exception e) {
            CustomLogHandler.printErrorlog(e);
        }
    }

or try

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  • last one didnt work. Please can you provide example for input argument? for example Activity? I am .NET dev sorry for bothering – Stefan0309 Feb 13 '18 at 13:30
  • Ok, this is working. I had to add one custom method to get activity from application, after that it went smooth. Just one more question before accepting your ansewr, how to hide same keyboard, on the same page when I click on settings button in Toolbar? Thanks – Stefan0309 Feb 13 '18 at 13:44
  • @Stefan0309 you can call hideSoftInput(this, btnView) like this and in that btnView will be your settings Button View –  Feb 17 '18 at 05:08
0

Call it before showing toast.

 public void hideKeyboard(Activity context) {
    // Check if no view has focus:
    View view = context.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
Mohit
  • 134
  • 7
  • How to get context? How to create input argument into this method? I am .NET dev :) Like this MyApplication.context = getApplicationContext(); ? – Stefan0309 Feb 13 '18 at 13:11
  • It's actually activity reference... If u r in activity then pass ACTIVITY_NAME.this , if u are calling it from fragment , then pass getActivity() – Mohit Feb 13 '18 at 16:32
  • Pass MainActivity.this here – Mohit Feb 13 '18 at 16:34
0

Try this code:

/**
 * Hides the soft keyboard
 */
public void hideSoftKeyboard() {
    if(getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

/**
 * Shows the soft keyboard
 */
public void showSoftKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    view.requestFocus();
    inputMethodManager.showSoftInput(view, 0);
}

or do this:

In the AndroidManifest.xml:

<activity android:name="com.your.package.ActivityName"
  android:windowSoftInputMode="stateHidden"  />

or try:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)‌​;
Masoud Mokhtari
  • 2,390
  • 1
  • 17
  • 45