/res/menu/popup_login.xml
<?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/itemLoginAdmin" android:title="@string/login_admin" /> <item android:id="@+id/itemLoginUser" android:title="@string/login_user" /> </menu>
MainActivity.java
public class MainActivity extends AppCompatActivity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button= (Button) findViewById(R.id.buttonLogin); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PopupMenu popupMenu=new PopupMenu(getBaseContext(),v); popupMenu.getMenuInflater().inflate(R.menu.popup_login,popupMenu.getMenu()); popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { if (item.getItemId()==R.id.itemLoginAdmin) { Toast.makeText(getBaseContext(),"Admin",Toast.LENGTH_LONG).show(); return true; } else if (item.getItemId()==R.id.itemLoginUser) { Toast.makeText(getBaseContext(),"User",Toast.LENGTH_LONG).show(); return true; } return false; } }); popupMenu.show(); } }); } }
References
https://github.com/mhdr/AndroidSamples/tree/master/026