How to hide keyboard

I have an EditText and I would like that when I focus in it the virtual keyboard does not appear.
I have try to put this code in activity's oncrate method

 InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
 imm.HideSoftInputFromWindow(edtPassword_Parametri.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.ImplicitOnly);

but doesn't work.
Can any one guide me how to hide the keyboard and have the cursor flashing in the EditText?

Thanks in advance

Posts

  • JoeJoe AUMember
    edited October 2012

    I'm not sure exactly why, but I found that a short delay is required. This is the method that works for me.

    private void ShowSoftKeyboard(View input, bool selectText)
    {
        if (selectText) ((EditText)input).SelectAll();
        ThreadPool.QueueUserWorkItem(s =>
        {
            Thread.Sleep(100);
            RunOnUiThread(() => ((InputMethodManager)GetSystemService(InputMethodService))
                .ShowSoftInput(input, ShowFlags.Implicit));
        });
    }
    

    Edit: Sorry; jumped the gun. Just realised you do not want to show the keyboard.

  • LeeLee GBMember

    May I ask why you would want to show an editable box but try to prevent the user from using the keyboard? If you wish the box to be read only, perhaps you should use a TextView? You could always style it to look a bit like a textbox if that's really necessary but I would imagine such a thing would be confusing for the users? I'd think I could edit a box and keep pressing on it...?

  • EmilianaPavaniEmilianaPavani ITMember

    It doesn't work again

  • TeHaTeHa ✭✭✭ DEMember ✭✭✭

    Try to set the InputType of the EditText to 0

    EditText myEditText = FindViewById(Resource.Id.myEditText);

    myEditText.InputType = 0;

    Later you can show the keyboard by set any InputType.

  • appapp INMember
    edited July 2016

    It's working for me

      InputMethodManager inputManager = (InputMethodManager)Activity.GetSystemService(Context.InputMethodService);
             var currentFocus = Activity.CurrentFocus;
             if (currentFocus != null)
             {
                inputManager.HideSoftInputFromWindow(currentFocus.WindowToken, HideSoftInputFlags.none);
            }
    
  • TonyCeliaTonyCelia ✭✭ USMember ✭✭

    app - thanks for pointing me in the right direction. This worked for me (Visual Studio 2015/Xamarin):

    InputMethodManager inputManager = (InputMethodManager)GetSystemService(Context.InputMethodService);
    var currentFocus = Window.CurrentFocus;

    if (currentFocus != null)
    {
    inputManager.HideSoftInputFromWindow(currentFocus.WindowToken, HideSoftInputFlags.None);
    }

  • iPaul1746iPaul1746 USMember

    I know this thread was last posted to nearly a year and a half ago but I am having some huge issues with this. I have been trying to hide the keyboard for hours. Nothing works. No matter how I obtain an android View, the WindowToken is null so HideSoftInputFromWindow will fail. How was this working for anyone else? Even the View returned by Window.CurrentFocus has null for its WindowToken. I even went as far as to try passing in a Xamarin Forms view from my OnAppearing and then getting the renderer from that and then the Android view. Again, the token is always null.
    I've tried a non-custom-android-code approach which was to call Unfocus() in an Entry but that also failed. I absolutely have no way to fix this.

    The whole reason for me wasting half my day trying to do this is, sometimes the keyboard is displayed when my app is first launched. I know the user can tap off the keyboard and close it but that's annoying.

  • KayKuoKayKuo ✭✭✭ Member ✭✭✭

    @iPaul1746 said:
    I know this thread was last posted to nearly a year and a half ago but I am having some huge issues with this. I have been trying to hide the keyboard for hours. Nothing works. No matter how I obtain an android View, the WindowToken is null so HideSoftInputFromWindow will fail. How was this working for anyone else? Even the View returned by Window.CurrentFocus has null for its WindowToken. I even went as far as to try passing in a Xamarin Forms view from my OnAppearing and then getting the renderer from that and then the Android view. Again, the token is always null.
    I've tried a non-custom-android-code approach which was to call Unfocus() in an Entry but that also failed. I absolutely have no way to fix this.

    The whole reason for me wasting half my day trying to do this is, sometimes the keyboard is displayed when my app is first launched. I know the user can tap off the keyboard and close it but that's annoying.

    I have the same problem,have you solved it?

  • HowdyHoHowdyHo ✭✭ USMember ✭✭
    edited September 2018

    Try inserting this in your LinearLayout -

    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"

    Your issue could be related to this - https://forums.xamarin.com/discussion/1856/how-to-disable-auto-focus-on-edit-text

    Look at this thread on stackoverflow - https://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup/8639921#8639921

  • vinothswamivinothswami ✭✭✭ Member ✭✭✭

    Yes,
    just like @HowdyHo said create a dummy view with size (0,0) and set the focus that view its a simple way.

    and also... this one is worked for me.

    InputMethodManager inputManager = (InputMethodManager)GetSystemService(Context.InputMethodService);
    inputManager.HideSoftInputFromWindow(txt_addInspectors.WindowToken, 0);

Sign In or Register to comment.