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); } } 
11
  • What was wrong with the dependency service? Commented Aug 25, 2017 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 Commented Aug 25, 2017 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? Commented Aug 25, 2017 at 14:40
  • 1
    Have you tried this? Otherwise I am not sure. Commented Aug 25, 2017 at 15:01
  • 1
    I will check it later because i just left. Thanks in advance though Commented Aug 25, 2017 at 15:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.