Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to hide keyboard in DialogFragment when tapping anywhere?

Hello dudes

Small problem im sure some of you can help me with.

I got a edittext in a dialogfragment, and the keyboard pops up when i it comes in focus... now i want to remove the keyboard when hitting anywhere outside the edittext, but nomatter what i try i cant find the right eventhandler to do this.

In another activity i used this method:

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

works very nice, but it does not exist in the dialogfragment - anyone know what to do here?

Best Answer

Answers

  • TroelsThistedTroelsThisted DKMember ✭✭
    edited September 2016

    You sir jus saved my day...! If future people are looking at the thread, this is how i integrated the toch listener on the dialogfragment:

    In the OnCreateView method:

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            var view = inflater.Inflate(Resource.Layout.dialog_overlay_menu, container, false);
    
            EditText txtEditview = view.FindViewById<EditText>(Resource.Id.txtEditview );
    
            view.SetOnTouchListener(new ViewClickListener(mContext, txtEditview));
    }
    

    And the OnTouchListener:

    public class ViewClickListener : Java.Lang.Object, View.IOnTouchListener
        {
            private Context mContext;
            private EditText mEdittext;
    
            public ViewClickListener(Context context,EditText edittext)
            {
                mEdittext = edittext;
                mContext = context;
            }
    
            public bool OnTouch(View v, MotionEvent e)
            {
                InputMethodManager imm = (InputMethodManager)mContext.GetSystemService(Context.InputMethodService);
                imm.HideSoftInputFromWindow(mEdittext.WindowToken, 0);
                return true;
            }
        }
    
  • CarlosSantillanCarlosSantillan USMember ✭✭

    Alright bro, just implemented the class and added the method to view. I can guarantee that this perfoms as expected no more no less. You sir, have saved my life thank you very much

  • TroelsThistedTroelsThisted DKMember ✭✭

    @CarlosSantillan said:
    Alright bro, just implemented the class and added the method to view. I can guarantee that this perfoms as expected no more no less. You sir, have saved my life thank you very much

    glad i could help you :)

  • misko101misko101 HRMember ✭✭

    Do you know how to detect touch outside of fragment too?

  • TroelsThistedTroelsThisted DKMember ✭✭

    @misko101 i think you can implement the same touchlistener outside of fragments - If it is for hiding the keyboard that is!

  • KevinRobinKevinRobin AUMember ✭✭

    @TroelsThisted said:
    @misko101 i think you can implement the same touchlistener outside of fragments - If it is for hiding the keyboard that is!

    Is it work?

  • TroelsThistedTroelsThisted DKMember ✭✭

    @Jay2 i think it works - try it out!

  • KevinRobinKevinRobin AUMember ✭✭

    @TroelsThisted Yes it works for my website Vrinsoft PTY LTD, thanks for the information.

Sign In or Register to comment.