Hiding the soft keyboard in MonoDroid

Go To StackoverFlow.com

2

I've got an Activity with a bunch of EditTexts and a button. When the user clicks the button, an answer based on the input from the EditTexts appears in the caption of the button. In the buttonclick handler I change the focus to the button and the cursor disappears from whatever EditText has the focus, but the soft keyboard remains on the screen. How can I force the soft keyboard to vanish?

EditText1.ClearFocus();    
EditText2.ClearFocus();    
EditText3.ClearFocus();
calc_btn.Focusable  =   true;
calc_btn.RequestFocus();

I've seen several answers as to how to do this in Java, but I haven't been able to figure out how to translate them to C#.

Thanks for any advice.

2012-04-03 20:23
by The Mighty Skunk


3

You can do something like this:

var inputManager = (InputMethodManager)GetSystemService(InputMethodService);
inputManager.HideSoftInputFromWindow(editText.WindowToken, HideSoftInputFlags.None);
2012-04-03 20:44
by Greg Shackles
Works perfectly, Greg. Many thanks - The Mighty Skunk 2012-04-03 22:48
Don't know why this did not work for me : - PCoder 2013-01-03 09:14


2

Jon O's answer above is perfect. This is how you use it.

Window.SetSoftInputMode(SoftInput.StateHidden);
2013-01-03 09:12
by PCoder


2

protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView (Resource.Layout.Main);

        Button button = FindViewById<Button> (Resource.Id.button1);
        button.Click+= onClick;
        EditText es=FindViewById<EditText>(Resource.Id.editText1);
        es.EditorAction += (object sender, TextView.EditorActionEventArgs e) => 
        {

            if ( e.ActionId == Android.Views.InputMethods.ImeAction.Done )
            {
                var editText = sender as EditText;
                var inputManager = GetSystemService(InputMethodService) as InputMethodManager;
                inputManager.HideSoftInputFromWindow(editText.WindowToken, 0);
            }

        };
        EditText es1=FindViewById<EditText>(Resource.Id.editText2);
        es1.EditorAction += (object sender, TextView.EditorActionEventArgs e) => 
        {
            if ( e.ActionId == Android.Views.InputMethods.ImeAction.Done )
            {
                var editText = sender as EditText;
                var inputManager = GetSystemService(InputMethodService) as InputMethodManager;
                inputManager.HideSoftInputFromWindow(editText.WindowToken, 0);
            }
        };



    }

I have used the given methods in above code.I dont want to show the soft keypad for both the textboxs.Its not working,have i written it incorrectly?

2013-02-06 12:30
by Prerana


1

Is this useful to you?

There seems to be a method here:

public override void HideSoftInput (int flags, Android.OS.ResultReceiver resultReceiver)

2012-04-03 20:31
by Jon O
I'm sure that method would be useful if I could figure out how to call it. This does not yield the method, so it appears not to be implemented directly: Android.InputMethodServices.InputMethodService.InputMethodImpl - The Mighty Skunk 2012-04-03 22:46
Don't forget to accept the right answer! : - Jon O 2012-04-04 00:21


0

Here is a completely working solution:

using Android.Views.InputMethods;

yourEditTextObject.EditorAction += (object sender, TextView.EditorActionEventArgs e) => 
        {
            if ( e.ActionId == Android.Views.InputMethods.ImeAction.Done )
            {
                var editText = sender as EditText;
                var inputManager = GetSystemService(InputMethodService) as InputMethodManager;
                inputManager.HideSoftInputFromWindow(editText.WindowToken, 0);
            }
        };
2013-02-06 10:32
by Roosevelt
Ads