3

So I have a searchbar(My searchbar all have a custom renderer) on one page. When a user fills in their search term they get redirect to searchpage where their results get shown.

The problem I have here is that suddenly the searchbar on the new page gets focussed (because I pass the search term from the previous page into this searchbar) and the software keyboard shows.

I want to dismiss keyboard or prevent the keyboard from showing. But when the user clicks inside the searchbar than ofcourse the keyboard can appear.

Note this question has been asked before, the answers I followed were never successfull. Here you can find what I have tried

First try: Unfocus Just unfocussing my entry, did not work. Tried it both in my constructor and in my OnAppearing code of the page

Second try: Focus other element Tried focussing my listview, but keyboard stil showed up

Third try: Dependency service

https://forums.xamarin.com/discussion/comment/172077#Comment_172077

Interface:

public interface IKeyboardHelper
{
    void HideKeyboard();
}

iOS:

public class iOSKeyboardHelper : IKeyboardHelper
{
    public void HideKeyboard()
    {
        UIApplication.SharedApplication.KeyWindow.EndEditing(true);
    }
}

Droid:

public class DroidKeyboardHelper : IKeyboardHelper
{
    public void HideKeyboard()
    {
        var context = Forms.Context;
        var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
        if (inputMethodManager != null && context is Activity)
        {
            var activity = context as Activity;
            var token = activity.CurrentFocus?.WindowToken;
            inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);

            activity.Window.DecorView.ClearFocus();
        }
    }
}

Usage in Xamarin Forms:

DependencyService.Get<IKeyboardHelper>().HideKeyboard();

EDIT

My renderer's code for my search page

   protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                var searchView = Control;
                searchView.Iconified = true;
                searchView.SetIconifiedByDefault(false);
                // (Resource.Id.search_mag_icon); is wrong / Xammie bug
                int searchIconId = Context.Resources.GetIdentifier("android:id/search_mag_icon", null, null);
                var icon = searchView.FindViewById(searchIconId);

                (icon as ImageView).SetImageResource(Resource.Drawable.search_zwart);

                int cancelIconId = Context.Resources.GetIdentifier("android:id/search_close_btn", null, null);
                var eicon = searchView.FindViewById(cancelIconId);
                (eicon as ImageView).SetImageResource(Resource.Drawable.close_zwart);
            }
        }
Jordy Dieltjens
  • 1,528
  • 11
  • 29
  • What was wrong with the dependency service? – hvaughan3 Aug 25 '17 at 14:25
  • Dependency service could work if I use it correctly. If I put it at constructor or at on appearing it does nothing. But if I do it a searchbar_Focused it indeeds works but in the bizar way. So when I put it there, the first time I enter the page the keyboard is there but if I click away and than focus again on the searchbar the hidekeyboard thing works. So I probably just need to to find out when to use the HideKeyboard() function – Jordy Dieltjens Aug 25 '17 at 14:33
  • Normally controls do not receive focus when first opening the page unless you added special code to make that happen. So did you add code to focus on the search bar when first opening the page? – hvaughan3 Aug 25 '17 at 14:40
  • The only thing I have done in my constructor of that page is searchbar.text = searchterm . I've also edited my post and included my renderer, perhaps there is something there that gets focus? But I have also pasted my hide keyboard code behind my renderer ( to try it out) and still it shows the keyboard. – Jordy Dieltjens Aug 25 '17 at 14:44
  • Looks normal to me. Try replacing your search bar with a regular Xamarin Forms `Entry` just to test. You should see that the `Entry` does not get focused on when the page initially loads. – hvaughan3 Aug 25 '17 at 14:48
  • The problem I have is that my customer wants a search icon and cancel icon on the search entry, is there any other way? Or can I perhaps create a custom entry with a search icon inside? – Jordy Dieltjens Aug 25 '17 at 14:50
  • Found why my edittext gets focussed, if I delete my searchbar.text = searchterm it doesn't get focussed, what would be the best way to insert the string into the searchbar without getting the focus? – Jordy Dieltjens Aug 25 '17 at 14:51
  • 1
    Have you tried [this](https://stackoverflow.com/a/20994000/3850012)? Otherwise I am not sure. – hvaughan3 Aug 25 '17 at 15:01
  • Yup I tried this with the depency service, or is this for the pcl ? I've done this previous time in my renderer of android. – Jordy Dieltjens Aug 25 '17 at 15:07
  • That would be added to the annotation on your `MainActivity` – hvaughan3 Aug 25 '17 at 15:11
  • 1
    I will check it later because i just left. Thanks in advance though – Jordy Dieltjens Aug 25 '17 at 15:55

0 Answers0