Membuat Popup Menu di Android Studio

 



menu_popup.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_1"
android:title="menu 1"/>
<item
android:id="@+id/action_2"
android:title="menu 2"/>
<item
android:id="@+id/action_3"
android:title="menu 3"/>
</menu>
MainActivity.java
public class MainActivity extends AppCompatActivity {

private ImageView imgMore;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imgMore = findViewById(R.id.imgMore);
imgMore.setOnClickListener(v -> {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, imgMore);
popupMenu.inflate(R.menu.menu_popup);
popupMenu.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.action_1:
Toast.makeText(this, "Menu 1", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_2:
Toast.makeText(this, "Menu 2", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_3:
Toast.makeText(this, "Menu 3", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
});
popupMenu.show();
});
}
}

Video tutorial



Comments

Popular Posts