Add search icon on a PopupMenu to Search the Country in Android Studio Project

I want to add a search bar menu on my country selection PopupMenu in Android Studio to search the country from a ISO Countries….

     SearchManager searchManager = (SearchManager)
            getSystemService(Context.SEARCH_SERVICE);

  SearchView searchView = (SearchView) popup.getMenu();

    searchView.setSearchableInfo(searchManager.
            getSearchableInfo(getComponentName()));
    searchView.setSubmitButtonEnabled(true);

public void showPopup() {

    popup = new PopupMenu(ActivityRegisterScreen.this, tablayout);





    String[] isoCountryCodes = Locale.getISOCountries();
    final Map<String, String> countryMap = new HashMap<>();
    int i = 1;
    String conCode = null;
    for (String countryCode : isoCountryCodes) {
        Log.i("test1234", "countryCode" + i + ":" + countryCode);
        Locale locale = new Locale("", countryCode);
        String countryName = locale.getDisplayCountry();
       // Toast.makeText(this,countryName,Toast.LENGTH_SHORT).show();
        countryMap.put(countryName, countryCode);
        Log.i("test1234", "countryName" + i + ":" + countryName);
        popup.getMenu().add(countryName);
        i++;

    }
    //popup.getMenuInflater().inflate(R.menu.menu_profile, popup.getMenu());
    //popup.setGravity(Gravity.START);
    popup.show();



    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {

            FragmentRegisterPersonal.updateCountry(countryMap.get(item.getTitle().toString()));
            String Cname=countryMap.get(item.getTitle().toString());
            Toast.makeText(getApplicationContext(),Cname,Toast.LENGTH_SHORT).show();
            return false;
        }
    });
    popup.setOnDismissListener(new PopupMenu.OnDismissListener() {
        @Override
        public void onDismiss(PopupMenu menu) {
            //flag_show_popup_menu = true;
        }
    });

}

i want to saw a search bar on th top of the PopupMenu for search the country from the getisocountries list

Leave a Reply

avatar
  Subscribe  
Notify of