Skip to content

Commit

Permalink
Merge pull request #62 from rh-mitre-ten/android_fix_suggestions_not_…
Browse files Browse the repository at this point in the history
…showing_when_search_string_empty

Android - display suggestions empty search text
  • Loading branch information
dotMorten authored Mar 9, 2020
2 parents 484b779 + 9b616b6 commit 15365ad
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 4 deletions.
10 changes: 10 additions & 0 deletions AutoSuggestBox/Platform/NativeAutoSuggestBox.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public AndroidAutoSuggestBox(Context context) : base(context)
Adapter = adapter = new SuggestCompleteAdapter(Context, global::Android.Resource.Layout.SimpleDropDownItem1Line);
}

/// <inheritdoc />
public override bool EnoughToFilter()
{
// Setting Threshold = 0 in the constructor does not allow the control to display suggestions when the Text property is null or empty.
// This is by design by Android.
// See https://stackoverflow.com/questions/2126717/android-autocompletetextview-show-suggestions-when-no-text-entered for details
// Overriding this method to always returns true changes this behaviour.
return true;
}

/// <inheritdoc />
protected override void OnFocusChanged(bool gainFocus, [GeneratedEnum] FocusSearchDirection direction, Rect previouslyFocusedRect)
{
Expand Down
8 changes: 4 additions & 4 deletions SampleApp/SampleApp/SampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<PackageReference Include="Xamarin.Forms" Version="3.0.0.446417" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\AutoSuggestBox\dotMorten.Xamarin.Forms.AutoSuggestBox.csproj">
<Private>false</Private>
</ProjectReference>
<ItemGroup>
<ProjectReference Include="..\..\AutoSuggestBox\dotMorten.Xamarin.Forms.AutoSuggestBox.csproj">
<Private>false</Private>
</ProjectReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="SampleApp.Samples.AutoSuggestBoxSamples.PreviousAndSuggested"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:dm="clr-namespace:dotMorten.Xamarin.Forms;assembly=dotMorten.Xamarin.Forms.AutoSuggestBox"
Padding="20">
<ContentPage.Content>
<StackLayout>
<dm:AutoSuggestBox
x:Name="SuggestBox1"
Focused="SuggestBox1_OnFocused"
PlaceholderText="Scan or type"
QuerySubmitted="SuggestBox_QuerySubmitted"
TextChanged="SuggestBox_TextChanged" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using dotMorten.Xamarin.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace SampleApp.Samples.AutoSuggestBoxSamples
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Description("Previous search and suggested")]
[SamplePriority(4)]
public partial class PreviousAndSuggested : ContentPage
{
public PreviousAndSuggested()
{
InitializeComponent ();
Initialize();
}

private List<string> _suggestedSearches;
private List<string> _previousSearches;

private void Initialize()
{
// suggested searches based on what was typed - normally loaded from some api
_suggestedSearches = new List<string>
{
"suggestion 1",
"suggestion 2",
"suggestion 3"
};

// a history of searches the user has previous executed
_previousSearches = new List<string>
{
"previous search 1",
"previous search 2",
"previous search 3",
};
}

private void SuggestBox_TextChanged(object sender, AutoSuggestBoxTextChangedEventArgs e)
{
var box = (AutoSuggestBox)sender;
UpdateSuggestionItemSource(box);
}

private void SuggestBox1_OnFocused(object sender, FocusEventArgs e)
{
var box = (AutoSuggestBox)sender;
UpdateSuggestionItemSource(box);
}

private void UpdateSuggestionItemSource(AutoSuggestBox box)
{
if (box.Text.Length >= 3)
{
// show suggested searches, presumably loaded from an API
box.ItemsSource = _suggestedSearches;
box.IsSuggestionListOpen = true;
}
else if (box.Text.Length == 0)
{
// show the user's previous searches when they haven't typed anything yet
box.ItemsSource = _previousSearches;
box.IsSuggestionListOpen = true;
}
else
{
box.IsSuggestionListOpen = false;
box.ItemsSource = null;
}
}

private void SuggestBox_QuerySubmitted(object sender, AutoSuggestBoxQuerySubmittedEventArgs e)
{
SuggestBox1.Unfocus();
}
}
}

0 comments on commit 15365ad

Please sign in to comment.