Monday 29 November 2021

How do you close/hide the Android soft keyboard programmatically?

 I created one function hideKeyboard  to hide keyboard on application class,


public class AppController extends Application {
    private static AppController appController;

    public static synchronized AppController getInstance() {
        return appController;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        appController = this;
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        MultiDex.install(newBase);
    }
    public void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        View view = activity.getCurrentFocus();
        if (view == null)
            view = new View(activity);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    } 
}


 I an calling hideKeyboard function on my Activity and Fragment pages like,


    AppController.getInstance().hideKeyboard(getActivity());

No comments:

Post a Comment