Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Hide keyboard in Xamarin Android after clicking outside Edittext

I am working on Xamarin(Android) .Now i want to hide keyboard after clicking outside Edit Text.

Thanks in Advance.

public class MainActivity : Activity
{


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

        // Set our view from the "main" layout resource
        RequestWindowFeature(WindowFeatures.NoTitle);


        SetContentView(Resource.Layout.Main);

        EditText Etusername= FindViewById<EditText>(Resource.Id.EtUname);
        Etusername.SetHintTextColor(Color.Gray);

        InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
        imm.HideSoftInputFromWindow(Etusername.WindowToken, 0);
    }
like image 265
raji Avatar asked Sep 22 '16 10:09

raji


1 Answers

Use this code to Hide Keyboard.

public override bool OnTouchEvent(MotionEvent e)
    {
         InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
         imm.HideSoftInputFromWindow(Etusername.WindowToken, 0);
         return base.OnTouchEvent(e);
    }

and make sure you have to add this library :

using Android.Views.InputMethods;
like image 116
Harshad Pansuriya Avatar answered Nov 07 '22 05:11

Harshad Pansuriya