Android Studio Audio Player : Create a drop down menu

Leonard Loo
3 min readDec 9, 2020

Create a drop down menu

Create a button first, in this case, i’ll have an onClick to call loadAudioMenu, which will display the menu from my list of audios.

Then create an XML file in the res folder like this.

xml component

name it menu

It should look like this now, an xml directory with a menu.xml file.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/dropdown_menu1"
android:title="DropDown Menu 1"
android:visible="true"
app:showAsAction="ifRoom" />

</menu>

Use this format to populate the menu. In this case, the id is dropdown_menu1, and the text shown in the menu is DropDown Menu 1. Now its time to define the function which display this menu.

public void loadAudioMenu(View view){
LoadAudioButton = (Button) findViewById(R.id.loadaudiobutton);
PopupMenu AudioMenu = new PopupMenu(getApplicationContext(), LoadAudioButton);
AudioMenu.getMenuInflater().inflate(R.xml.menu, AudioMenu.getMenu());
AudioMenu.show();
}

This will open the menu that you defined in menu.xml, and opens the menu when u press the LoadAudio button.

This is an example of how the menu will look like when u press the button. You can add more items, and it will show more, and even enable scrolling if it exceeds the screen.

Now we want to be able to make use of the output of the selected menu

First, we define a TextView to show our output.

First we set the implements, this allows us to override the function.

Then we initialise and set the TextView.

Then we update the loadAudioMenu function to define what to do when we made our selection, which is to update the TextView.

The external Override on onMenuItemClick is just there because we implements it at the start.

Now, when we make the selection, it will update the TextView.

--

--