Skip to content

Instantly share code, notes, and snippets.

@felipeslongo
Last active April 11, 2019 16:37
Show Gist options
  • Save felipeslongo/a333878f0a589a9baf5fda061f9f0dba to your computer and use it in GitHub Desktop.
Save felipeslongo/a333878f0a589a9baf5fda061f9f0dba to your computer and use it in GitHub Desktop.
Extension method ShowSuggestions to AutoCompleteTextView
using Android.Widget;
namespace App.Droid.Extensions
{
/// <summary>
/// Extension method ShowSuggestions to AutoCompleteTextView
/// </summary>
/// <seealso cref="https://stackoverflow.com/questions/2126717/android-autocompletetextview-show-suggestions-when-no-text-entered"/>
public static class AutoCompleteTextView_ShowSuggestions
{
public static void ShowSuggestionsOnInteraction(this AutoCompleteTextView @this)
{
@this.ShowSuggestionsOnFocus();
@this.ShowSuggestionsOnClick();
@this.ShowSuggestionsOnEmptyText();
}
/// <summary>
/// Configure the FocusChange EventHandler to show the drop down suggestions on focus.
/// </summary>
/// <param name="this"></param>
public static void ShowSuggestionsOnFocus(this AutoCompleteTextView @this)
{
@this.FocusChange += (sender, args) =>
{
if(args.HasFocus)
@this.ShowDropDown();
};
}
public static void ShowSuggestionsOnClick(this AutoCompleteTextView @this)
=> @this.Click += (sender, args) => @this.ShowDropDown();
public static void ShowSuggestionsOnEmptyText(this AutoCompleteTextView @this)
{
@this.TextChanged += async (sender, args) =>
{
if (!string.IsNullOrEmpty(@this.Text))
return;
await Task.Delay(TimeSpan.FromMilliseconds(300));
@this.ShowDropDown();
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment