1

I am creating autocomplete textview in xamarin forms by this using this sample. But in this example they are using AutoCompleteTextView in mainActivity.cs onCreate(). How could I use the same code in my xaml forms

CODE IS:

protected override void OnCreate (Bundle bundle)
{
  base.OnCreate (bundle);

  // Set our view from the "Main" layout resource
  SetContentView (Resource.Layout.Main);

  AutoCompleteTextView textView = FindViewById<AutoCompleteTextView> (Resource.Id.autocomplete_country);
  var adapter = new ArrayAdapter<String> (this, Resource.Layout.list_item, COUNTRIES);

  textView.Adapter = adapter;
}

Thank you.

Guillaume Raymond
  • 1,726
  • 1
  • 20
  • 33
Ir shan
  • 11
  • 3
  • https://stackoverflow.com/questions/54245357/xamarin-forms-entry-with-suggestion-items-in-a-listview-popup-when-typing-the-te/54275498#54275498 – sermet Jan 06 '20 at 08:06

2 Answers2

0

You can't use Android samples (including Xamarin.Android) directly in Xamarin.Forms. Those are different technologies.

Xamarin.Forms can also runs on iOS (even if you don't want it to) and iOS won't run Android specific code, that's the main reason why it is impossible.

Other than that you may build Xamarin.Forms controls from native controls using custom renderers. So basically you could make the above sample work on Xamarin.Forms, but with lot of knowledge and hard work, a lot more code is needed than the sample above.

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
  • Thank you Ivan for your suggestion, I dont have much knowledge in xamarin.Forms. So, I can't able to do this but i am using list view below the entry box and binding data on change of text. – Ir shan Jan 06 '20 at 11:45
0

I did something like this in my Xamarin.Forms project, which was 100% shared by Android and iOS - no custom renderers. This example uses the MVVM pattern with some use of the code-behind. My view model contains an IList of object which the ListView binds to. It is updated when the user presses three or more characters with a query expression.

private IList<myObject> results;
public IList<myObject> QueryResults
{
   get
   {
     return results;
   }
   set
   {
     SetProperty(ref results, value);
   }
}

Within the OnTextChanged handler I run the query expression and populate QueryResults on every key stroke. This might not be that efficient, but it performs well.

So the view mark up and code-behind is something like this:

    <StackLayout>
        <Label Style="{StaticResource formLabel}" Text="My Searchable List" />
        <!-- from poc -->
        <Grid WidthRequest="550">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="AUTO" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="350" />
            </Grid.ColumnDefinitions>
            <Frame Grid.Row="0" Style="{StaticResource InputFrame}" HeightRequest="49">
                <Entry x:Name="SearchText" Style="{StaticResource formLabel}" WidthRequest="550" HeightRequest="45" Text="{Binding SearchString, Mode=TwoWay}" TextChanged="Handle_TextChanged" />
            </Frame>
            <StackLayout Grid.Row="1" Orientation="Horizontal">
                <Frame x:Name="MyObjectFrame" Style="{StaticResource InputFrame}" HeightRequest="{Binding ListHeight}" HasShadow="true">

                    <ListView x:Name="MyObjectList" ItemsSource="{Binding ResultantMyObjects}" ItemSelected="OnItemSelected" HeightRequest="{Binding ListHeight}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <TextCell Text="{Binding Name}" />
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Frame>

            </StackLayout>

with the Handle_TextChanged in the code-behind:

void Handle_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
    {
                    if (e.NewTextValue.Length > 2)
        {
            (BindingContext as SampleDetailsViewModel).MySIteSearchResults(e.NewTextValue);
            this.ForceLayout();// force the change in heightrequest for ListView
            MyObjectList.IsVisible = true;
            MyObjectFrame.IsVisible = true;
        }
        else
        {
            MyObjectList.IsVisible = false;
            MyObjectFrame.IsVisible = false;
        }
    }

In the view model, I update the property above, note the return value really isn't used in this not-yet-perfect example:

public List<MyObjectDTO> MyObjectSearchResults(string keystrokes)
    {
        //TODO: encapsulate in a View
        List<MyObjectDTO> searchResults = null;
        IEnumerable<MyObjectDTO> queryResults;

            queryResults = from site in MyObjects
                           where site.Name.ToLower().Contains(keystrokes.ToLower())
                           select site;
            if (string.IsNullOrEmpty(SearchString))
            {
                QueryResults = MyObjects;
            }
            else
            {
                QueryResults = queryResults.ToList<MyObjectDTO>();
                ListHeight = QueryResults.Count * 45; //TODO: detect size. magic number of 45 limits the height. 
            }

        return searchResults;
    }

NOTE: There might be some errors, as I had to scrub some code.

Blanthor
  • 2,568
  • 8
  • 48
  • 66