How to Create Android Popup Menu Programmatically and Handle Menu Items Click

  Рет қаралды 11,727

Coding Demos

Coding Demos

Күн бұрын

Hi and welcome to another tutorial from CodingDemos :)
In this tutorial, you will learn how to create Android popup menu or floating menu programmatically and click on menu items using setOnMenuItemClickListener.
Here are the steps:
1- Open up Android Studio.
2- Using setOnClickListener to react to user taps on the button.
openMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
3- Inside the button onClick listener, declare the Popup menu.
PopupMenu popupMenu = new PopupMenu(MainActivity.this, v);
4- Create a new menu file under Menu folder and initialize the menu items that you want to show inside the floating menu.
popupMenu.getMenuInflater().inflate(R.menu.menu_popup, popupMenu.getMenu());
5- Next, you need call setOnMenuItemClickListener to react when the user taps on the menu items.
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
});
6- Inside onMenuItemClick, you need to check which menu item the user has taps based on the menu item ID using a Switch statement.
switch (item.getItemId()) {
case R.id.item_subscribe:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(" / codingdemos ")));
return true;
case R.id.item_website:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("www.codingdemos.com")));
return true;
default:
return false;
}
7- Finally, you need to show the popup menu like this.
popupMenu.show();
8- Now build and run the app to see the output.
#androiddevelopment #androidprogramming #androidtutorial #androidstudio #androidstudiotutorial #java
Links:
Android photo Intent: developer.android.com/trainin...
Website: www.codingdemos.com
FaceBook: / codingdemos
Introduction 00:00
Final output 00:30
Android Studio project introduction 01:47
Handle button click event 02:55
Android Popup Menu 03:25
Create menu items 04:35
Add items in Popup Menu 08:25
Clicking on Popup Menu items 09:35
Conclusion 15:30

Пікірлер: 12
@CodingDemos
@CodingDemos 4 жыл бұрын
Hey guys, I hope you liked this tutorial :) Comment below on topics you want me to cover next.
@leolima8585
@leolima8585 3 жыл бұрын
My first time watching your channel, I got another piece of writing, for the simple way of focusing on the words and expressions of JAVA. Congratulations! I use another language, I'm from South America, not too bad at understanding English, plus his calm and patient way between points and commas of thought, made the Google translator make my translations in my language more objective
@emilydruyan
@emilydruyan Жыл бұрын
Thanks, you help me a lot
@raphaelrossi5171
@raphaelrossi5171 3 ай бұрын
great stuff
@fernandoserrano3835
@fernandoserrano3835 3 жыл бұрын
So much helpful, thank you very much!
@shreyasdongre2537
@shreyasdongre2537 Жыл бұрын
you sir, are a fucking legend!! Thankyouuu
@webtknet
@webtknet 3 жыл бұрын
شكرا لك
@shahjahanmughal9558
@shahjahanmughal9558 3 жыл бұрын
Thank You man
@CodingDemos
@CodingDemos 3 жыл бұрын
You're welcome 😃
@jakubbober1459
@jakubbober1459 3 жыл бұрын
How to hide navigation bar when popMenu is active?
@user-cb9cp2fy4k
@user-cb9cp2fy4k Жыл бұрын
can you link your code source
@jakubbober1459
@jakubbober1459 3 жыл бұрын
How to hide navigaton bar when popMenu is showing?
Android Material Design Component - How to Use Sliders in Your App
34:44
Каха заблудился в горах
00:57
К-Media
Рет қаралды 10 МЛН
Spot The Fake Animal For $10,000
00:40
MrBeast
Рет қаралды 196 МЛН
Slow motion boy #shorts by Tsuriki Show
00:14
Tsuriki Show
Рет қаралды 10 МЛН
#3 Display Android notification at a particular time with Alarm Manager
5:59
RecyclerView OnClickListener (Best practice way)
11:48
CodingWithMitch
Рет қаралды 291 М.
How ChatGPT Built My App in Minutes 🤯
8:28
Website Learners
Рет қаралды 2,2 МЛН
How to Make a Floating Window Application in Android? | GeeksforGeeks
1:04:42
Каха заблудился в горах
00:57
К-Media
Рет қаралды 10 МЛН