No video

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

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

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?
Introduction to Git Recap | Learn with Dr G
18:42
Microsoft Developer
Рет қаралды 95 М.
How to Add Contacts Programmatically in Android
19:33
Coding Demos
Рет қаралды 13 М.
小蚂蚁被感动了!火影忍者 #佐助 #家庭
00:54
火影忍者一家
Рет қаралды 39 МЛН
Java 8 to 18: Most important changes in the Java Platform
31:24
PianoSens vs PitchGrabber with High Noise
1:49
PianoSens
Рет қаралды 16
JPEG is Dying - And that's a bad thing
8:09
2kliksphilip
Рет қаралды 85 М.
BEST Ways to Handle and Validate React Forms without a Library
42:38
Top 30 Flutter Tips and Tricks
6:50
Flutter Mapp
Рет қаралды 545 М.
Every Android Setting in the Developer Options Explained!
29:41
HowToMen
Рет қаралды 2,6 МЛН
How to publish app in Google Play Store | 2024 update
30:05
iRekha Tech Solutions
Рет қаралды 112 М.