First create a button on main activity
<Button android:layout_width="150dp" android:layout_height="wrap_content" android:text="View Popup Menu" android:textSize="20sp" android:textColor="#fff" android:textStyle="bold" android:background="@color/colorPrimaryDark" android:onClick="ShowPopup" android:textAllCaps="false" android:layout_gravity="center" />
Now in res directory create new directory for menu named as
menu
Then create popup_menu in menu directory and then implement
following code
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/vuimg" android:title="View Image"/> <item android:id="@+id/nuimg" android:title="New Image"/> </menu>
Now go to main activity and create method named as ShowPopup
for button
public void ShowPopup(View v){ PopupMenu popupMenu = new PopupMenu(this,v); popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu()); popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { Toast.makeText(MainActivity.this,""+item.getTitle(),Toast.LENGTH_SHORT).show(); switch (item.getItemId()){ case R.id.vuimg: getPicFromGallery(); return true; case R.id.nuimg: camerOpenForPic(); return true; default: return false;
}
});
popupMenu.show();
}
No comments:
Post a Comment