I have a PopupMenu that appears when I click on an action button in a actionbar. I would like the MenuItem, in my PopupMenu, with a custom layout like this:
layout/menu_item_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/menuItemLayout"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageViewMenuItem"
android:layout_width="20dip"
android:layout_height="20dip"
android:src="@drawable/abc_list_focused_holo" />
<TextView
android:id="@+id/textViewMenuItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextViewMenuItem" />
</LinearLayout>
This is the xml of PopUpMenu:
menu/pop_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="apparound.actiobarpopupstylefacebook.Main" >
<item
android:id="@+id/popupItem"
android:showAsAction="ifRoom"/>
</menu>
In my activity code is following:
public void showPopup(int idR){
View menuItemView = findViewById(idR);
PopupMenu popup = new PopupMenu(this, menuItemView);
MenuInflater inflate = popup.getMenuInflater();
inflate.inflate(R.menu.pop_menu, popup.getMenu());
MenuItem menuItem= popup.getMenu().findItem(R.id.popupItem);
menuItem.setActionView(R.layout.menu_item_layout);
popup.show();
}
But when appear popupmenu, item is empty. I was wrong to use setActionview() method? Thanks.
You can use 9patch to get the shadow and rounded corners for the background. You can either add some styles to the popupMenu and achieve your UI or create popup Window. My conclusion is; if we define a style with parent="@android:style/Widget. PopupMenu" as parent, we override behaviour of our PopupMenu.
Way to create menu directory and menu resource file: To create the menu directory just right-click on res folder and navigate to res->New->Android Resource Directory. Give resource directory name as menu and resource type also menu. one directory will be created under res folder.
For custom layouts you can't use a menu, one alternate option is a PopupWindow
PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(clickbtn, -40, 18); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);
public PopupWindow popupDisplay()
{
final PopupWindow popupWindow = new PopupWindow(this);
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylayout, null);
Button item = (Button) view.findViewById(R.id.button1);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}
Create this XML file in the res/layout folder named my layout.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With