Search code examples
androiddrop-down-menuautocompletetextview

AutoCompleteTextView not showing drop down menu - Android


I've followed material.io to build an "Exposed Dropdown Menus" but the drop down menu is not showing.

I want to achieve this: Dropdown (screenshot from material.io)

However, I'm currently getting this: Failed dropdown

I'm not sure why I can even type into the box when I've added "android:inputType="none"" to the AutoCompleteTextView.

This is how I initialised the adapter inside the fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_add, container, false);

    // Initialise drop down list
    ArrayAdapter<String> adapter = new ArrayAdapter<>(
            getContext(),
            R.layout.dropdown_menu_popup_item,
            UNITS
    );

    AutoCompleteTextView unitsExposedDropdown = view.findViewById(R.id.unit_selection);
    unitsExposedDropdown.setAdapter(adapter);

    return view;
}

Thanks for your help!

Edit: This is the link I'm using to build the Exposed Dropdown Menu https://material.io/develop/android/components/menu


Solution

  • dont want to type in the box

    then why don't you use popup menu see this:

    PopupMenu popup = new PopupMenu(youractivity.this, yourbutton);  
                    //Inflate the pop up menu
                    popup.getMenuInflater().inflate(R.menu.popmenu, popup.getMenu());  
      
                    //do something on click 
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
                        public boolean onMenuItemClick(MenuItem item) {  
                          //do your work here
                        }  
                    });  
      
                    popup.show();//show it 
    

    To understand more see this https://developer.android.com/reference/android/widget/PopupMenu