Rutuja Shinde
Rutuja Shinde

Reputation: 1

Display Country List dropdown with Flag and Country name C# Xamarin android

I am trying to create dropdown with country flag and country name. I have done the following code changes 1.In Sign Up page i have added AutoCompleteTextView

 <com.google.android.material.textfield.TextInputLayout
                         style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
                         android:layout_width="0dp"
                         android:layout_height="wrap_content"
                         android:layout_weight="1"
                         app:endIconDrawable="@drawable/icon_dropdown"
                         app:boxCornerRadiusBottomEnd="7dp"
                         app:boxCornerRadiusTopEnd="7dp"
                         app:boxCornerRadiusBottomStart="7dp"
                         app:boxCornerRadiusTopStart="7dp"
                         android:layout_marginStart="20dp"
                         android:layout_marginTop="20dp"
                         android:layout_marginEnd="20dp"
                         app:errorEnabled="false"
                         android:id="@+id/CountryError"
                         app:errorTextAppearance="@style/ErrorText">


                    <AutoCompleteTextView
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:fontFamily="@Assets/segoeui"
                        android:textSize="16sp"
                        android:hint="country"
                        android:paddingStart="10dp"
                        android:paddingEnd="15dp"
                        android:background="@null"
                        android:inputType="text"
                        android:singleLine="true"
                        android:id="@+id/s_DropCountry"
                        android:clickable="true"/>
           </com.google.android.material.textfield.TextInputLayout>

Created an view

<?xml version="1.0" encoding="utf-8" ?>
<!-- dropdown_item.xml -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:paddingStart="10dp"
        android:layout_height="50dp"
        android:paddingEnd="15dp">

    <ImageView
        android:id="@+id/flag_image"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:contentDescription="Country flag"
        android:padding="5dp"/>

    <com.google.android.material.textfield.TextInputEditText

       android:id="@+id/country_name"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"

       android:fontFamily="@Assets/segoeui"
       android:textSize="16sp"
       android:hint="country"
       android:background="@null"
       android:inputType="text"
       android:singleLine="true" ></com.google.android.material.textfield.TextInputEditText>
</LinearLayout>

Created an Country adapter

  public class CountryAdapter : BaseAdapter<Country>, IFilterable
    {
        private Context context;
        private List<Country> countries;

        public CountryAdapter(Context context, List<Country> countries)
        {
            this.context = context;
            this.countries = countries;
        }
        public Filter Filter => new CountryFilter(this);
        public override int Count => countries.Count;

        public override long GetItemId(int position) => position;

        public override Country this[int position] => countries[position];

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var context = parent.Context;
            var view = convertView ?? LayoutInflater.FromContext(context).Inflate(Resource.Layout.CountryDropdown, parent, false);

            var flagImageView = view.FindViewById<ImageView>(Resource.Id.flag_image);
            var countryNameTextView = view.FindViewById<TextView>(Resource.Id.country_name);

            flagImageView.SetImageResource(countries[position].Id); // Set flag image
            countryNameTextView.Text = countries[position].Name; // Set country name

            return view;
        }
    }

In Activity binding adapter to AutoCompleteTextView

  var countryAdapter = new CountryAdapter(this, countries);
            DropCountry.Adapter = countryAdapter;
            DropCountry.InputType = InputTypes.Null;
            DropCountry.ItemClick -= DropCountry_ItemClick;
            DropCountry.ItemClick += DropCountry_ItemClick;
 private void DropCountry_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        { var selectedCountry = countryList[e.Position];
            // Set the text of the AutoCompleteTextView to the selected country
            DropCountry.Text = selectedCountry.Name;

            // Set the threshold to 1 to show suggestions immediately
            DropCountry.Threshold = 1;

            
        }

getting Error [InputMethodManager] showSoftInput fail, mServedView:android.widget.AutoCompleteTextView{845aa72 VFED..CL. .F...... 0,0-443,138 #7f09029c app:id/s_DropCountry aid=1073741827}

I just display selected country in AutoCompleteTextView on selection of country

Upvotes: 0

Views: 50

Answers (0)

Related Questions