popupactivity.java
public class MainActivity extends AppCompatActivity {
private final static int ONE = 1;
private final static int TWO = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myAnchor = findViewById(R.id.tv);
// Standard popup:
//final PopupMenu popupMenu = new PopupMenu(this, myAnchor);
// Changes position of the popup menu:
final PopupMenu popupMenu = new PopupMenu(this, myAnchor, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0);
popupMenu.getMenu().add(Menu.NONE, ONE, Menu.NONE, "Item 1");
popupMenu.getMenu().add(Menu.NONE, TWO, Menu.NONE, "Item 2");
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case ONE:
Toast.makeText(MainActivity.this, "One", Toast.LENGTH_SHORT).show();
break;
case TWO:
Toast.makeText(MainActivity.this, "Two ", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
myAnchor.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
popupMenu.show();
}
});