Example usage for android.view.inputmethod InputMethodManager hideSoftInputFromWindow

List of usage examples for android.view.inputmethod InputMethodManager hideSoftInputFromWindow

Introduction

In this page you can find the example usage for android.view.inputmethod InputMethodManager hideSoftInputFromWindow.

Prototype

public boolean hideSoftInputFromWindow(IBinder windowToken, int flags) 

Source Link

Document

Synonym for #hideSoftInputFromWindow(IBinder,int,ResultReceiver) without a result: request to hide the soft input window from the context of the window that is currently accepting input.

Usage

From source file:app.sunstreak.yourpisd.LoginActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_new);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);//from w  w w .j  av  a 2 s. c  o m
    }
    final SharedPreferences sharedPrefs = getPreferences(Context.MODE_PRIVATE);
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    height = size.y;
    mLoginFormView = findViewById(R.id.login_form);
    mLoginStatusView = (LinearLayout) findViewById(R.id.login_status);
    mLoginStatusMessageView = (TextView) findViewById(R.id.login_status_message);

    if (DateHelper.isAprilFools()) {
        LinearLayout container = (LinearLayout) mLoginFormView.findViewById(R.id.container);
        ImageView logo = (ImageView) container.findViewById(R.id.logo);
        InputStream is;
        try {
            is = getAssets().open("doge.png");
            logo.setImageBitmap(BitmapFactory.decodeStream(is));
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    mAutoLogin = sharedPrefs.getBoolean("auto_login", false);
    System.out.println(mAutoLogin);

    session = ((YPApplication) getApplication()).session;

    try {
        boolean refresh = getIntent().getExtras().getBoolean("Refresh");

        if (refresh) {
            mEmail = session.getUsername();
            mPassword = session.getPassword();
            showProgress(true);
            mAuthTask = new UserLoginTask();
            mAuthTask.execute((Void) null);

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mPasswordView.getWindowToken(), 0);
        } else
            mLoginFormView.setVisibility(View.VISIBLE);
    } catch (NullPointerException e) {
        // Keep going.
    }

    if (sharedPrefs.getBoolean("patched", false)) {
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.remove("password");
        editor.putBoolean("patched", true);
        editor.commit();
    }

    if (!sharedPrefs.getBoolean("AcceptedUserAgreement", false)) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getResources().getString(R.string.user_agreement_title));
        builder.setMessage(getResources().getString(R.string.user_agreement));
        // Setting Positive "Yes" Button
        builder.setPositiveButton("Agree", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                sharedPrefs.edit().putBoolean("AcceptedUserAgreement", true).commit();
                dialog.cancel();
            }
        });

        // Setting Negative "NO" Button
        builder.setNegativeButton("Disagree", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to invoke NO event
                sharedPrefs.edit().putBoolean("AcceptedUserAgreement", false).commit();
                Toast.makeText(LoginActivity.this, "Quitting app", Toast.LENGTH_SHORT).show();
                finish();
            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    // Set up the remember_password CheckBox
    mRememberPasswordCheckBox = (CheckBox) findViewById(R.id.remember_password);
    mRememberPasswordCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mRememberPassword = isChecked;
        }
    });

    mRememberPassword = sharedPrefs.getBoolean("remember_password", false);
    mRememberPasswordCheckBox.setChecked(mRememberPassword);

    // Set up the auto_login CheckBox
    mAutoLoginCheckBox = (CheckBox) findViewById(R.id.auto_login);
    mAutoLoginCheckBox.setChecked(mAutoLogin);
    mAutoLoginCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton button, boolean isChecked) {
            mAutoLogin = isChecked;
            if (isChecked) {
                mRememberPasswordCheckBox.setChecked(true);
            }
        }

    });

    // Set up the login form.
    mEmailView = (EditText) findViewById(R.id.email);

    mPasswordView = (EditText) findViewById(R.id.password);
    mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
            if (id == R.id.login || id == EditorInfo.IME_NULL) {
                attemptLogin();
                return true;
            }
            return false;
        }
    });

    //Load stored username/password
    mEmailView.setText(sharedPrefs.getString("email", mEmail));
    mPasswordView.setText(new String(Base64.decode(sharedPrefs.getString("e_password", ""), Base64.DEFAULT)));
    // If the password was not saved, give focus to the password.
    if (mPasswordView.getText().equals(""))
        mPasswordView.requestFocus();

    mLoginFormView = findViewById(R.id.login_form);
    mLoginStatusView = (LinearLayout) findViewById(R.id.login_status);
    mLoginStatusMessageView = (TextView) findViewById(R.id.login_status_message);

    findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            attemptLogin();
        }
    });
    findViewById(R.id.sign_in_button).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mPasswordView.getWindowToken(), 0);
            return false;
        }
    });
    mLoginFormView.setVisibility(View.VISIBLE);
    // Login if auto-login is checked.
    if (mAutoLogin)
        attemptLogin();
}

From source file:com.oonhee.oojs.inappbrowser.InAppBrowser.java

/**
 * Navigate to the new page/*from  w  ww . j  a v a  2s. c  om*/
 *
 * @param url to load
 */
private void navigate(final String url) {
    InputMethodManager imm = (InputMethodManager) this.cordova.getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);

    this.cordova.getActivity().runOnUiThread(new Runnable() {
        public void run() {
            if (!url.startsWith("http") && !url.startsWith("file:")) {
                InAppBrowser.this.inAppWebView.loadUrl("http://" + url);
            } else {
                InAppBrowser.this.inAppWebView.loadUrl(url);
            }
            InAppBrowser.this.inAppWebView.requestFocus();
        }
    });
}

From source file:com.insthub.O2OMobile.Activity.C1_PublishOrderActivity.java

public void closeKeyBoard() {
    mPrice.clearFocus();//ww w .j a v  a 2  s  .  com
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mPrice.getWindowToken(), 0);
}

From source file:com.anjalimacwan.fragment.NoteEditFragment.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Hide soft keyboard
    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getActivity().findViewById(R.id.editText1).getWindowToken(), 0);

    switch (item.getItemId()) {
    case android.R.id.home:
        // Override default Android "up" behavior to instead mimic the back button
        getActivity().onBackPressed();//from w ww.jav a2s  .  co  m
        return true;

    // Save button
    case R.id.action_save:
        // Get current note contents from EditText
        noteContents = (EditText) getActivity().findViewById(R.id.editText1);
        contents = noteContents.getText().toString();

        // If EditText is empty, show toast informing user to enter some text
        if (contents.equals(""))
            showToast(R.string.empty_note);
        else if (directEdit)
            getActivity().onBackPressed();
        else {
            // If no changes were made since last save, switch back to NoteViewFragment
            if (contentsOnLoad.equals(noteContents.getText().toString())) {
                Bundle bundle = new Bundle();
                bundle.putString("filename", filename);

                Fragment fragment = new NoteViewFragment();
                fragment.setArguments(bundle);

                getFragmentManager().beginTransaction().replace(R.id.noteViewEdit, fragment, "NoteViewFragment")
                        .commit();
            } else {
                SharedPreferences pref = getActivity().getSharedPreferences(
                        getActivity().getPackageName() + "_preferences", Context.MODE_PRIVATE);
                if (pref.getBoolean("show_dialogs", true)) {
                    // Show save button dialog
                    listener.showSaveButtonDialog();
                } else {
                    try {
                        saveNote();

                        if (listener.isShareIntent())
                            getActivity().finish();
                        else {
                            Bundle bundle = new Bundle();
                            bundle.putString("filename", filename);

                            Fragment fragment = new NoteViewFragment();
                            fragment.setArguments(bundle);

                            getFragmentManager().beginTransaction()
                                    .replace(R.id.noteViewEdit, fragment, "NoteViewFragment").commit();
                        }
                    } catch (IOException e) {
                        // Show error message as toast if file fails to save
                        showToast(R.string.failed_to_save);
                    }
                }
            }
        }
        return true;

    // Delete button
    case R.id.action_delete:
        listener.showDeleteDialog();
        return true;

    // Share menu item
    /*case R.id.action_share:
        // Set current note contents to a String
        contents = noteContents.getText().toString();
            
        // If EditText is empty, show toast informing user to enter some text
        if(contents.equals(""))
            showToast(R.string.empty_note);
        else {
            // Send a share intent
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_TEXT, contents);
            intent.setType("text/plain");
            
            // Verify that the intent will resolve to an activity, and send
            if(intent.resolveActivity(getActivity().getPackageManager()) != null)
                startActivity(Intent.createChooser(intent, getResources().getText(R.string.send_to)));
        }
            
        return true;*/
    default:
        return super.onOptionsItemSelected(item);
    }
}

From source file:com.anyonavinfo.commonuserregister.MainActivity.java

/**
 * ?onTouchEvent??/*from   w w w.  j  a  v  a  2s . c  om*/
 */
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (null != this.getCurrentFocus()) {
        InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
    }
    return super.onTouchEvent(event);
}

From source file:com.bitants.wally.fragments.SearchFragment.java

public void dismissKeyboard() {
    if (getActivity() != null && getActivity().getCurrentFocus() != null) {
        InputMethodManager inputManager = (InputMethodManager) getActivity()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }/*from  www.ja v a  2  s. c  o  m*/
}

From source file:com.prad.yahooweather.YahooWeatherActivity.java

private void hideKeyboard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(searchText.getWindowToken(), 0);
}

From source file:com.ayuget.redface.ui.activity.ReplyActivity.java

/**
 * Hides the soft keyboard//from w w  w. ja v  a 2  s .  c  o  m
 */
public void hideSoftKeyboard() {
    if (getCurrentFocus() != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

From source file:com.dtworkshop.inappcrossbrowser.WebViewBrowser.java

/**
 * Navigate to the new page/*w  w  w.ja  v a 2s.  c  o m*/
 *
 * @param url to load
 */
protected void navigate(String url) {
    InputMethodManager imm = (InputMethodManager) this.cordova.getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);

    if (!url.startsWith("http") && !url.startsWith("file:")) {
        this.inAppWebView.loadUrl("http://" + url);
    } else {
        this.inAppWebView.loadUrl(url);
    }
    this.inAppWebView.requestFocus();
}