Reputation: 3535
I want to change the space between items in my popupmenu on android
I hope there is a straight foward solution, but as i know android it shall not be so simple
anyway i would like also to set the maxHeight
of my popUp...
In this case popup is achored by the TextView "Select the prize" so it fills the space above it, but not all the items fit in that space so android automatically creates an scrow view (on vertical)
the thing is, I know 100% of users wont use this scroll so the items in my menu bellow "Travel" will never be seen.
How can i set the height in order to make enough space to all items?
=============UPDATE================
Just to make it clear, this isn't a spinner it is a popupmenu
this.popupCategories = new PopupMenu(this, this.categoryLabel);
for (Giveaway.GiveawayCategoryGroup catGroup : categoryGroups) {
SubMenu submenu = this.popupCategories.getMenu().addSubMenu(catGroup.getDescription(lang));
for (Giveaway.GiveawayCategory cat : (Collection<? extends Giveaway.GiveawayCategory>) catGroup.getCategories()) {
if (cat.isActive())
submenu.add(Menu.NONE, cat.getValue().hashCode(), (int) cat.getOrder(), cat.getDescription(lang));
}
}
Upvotes: 2
Views: 3238
Reputation: 2165
You can create a layout for popup menu and initialize it in your activity as follows XML
> <?xml version="1.0" encoding="utf-8"?> <LinearLayout
> xmlns:android="http://schemas.android.com/apk/res/android"
> android:layout_width="match_parent"
> android:layout_height="match_parent"
> android:gravity="center"
> android:background="@android:color/transparent">
> <LinearLayout
> android:layout_width="wrap_content"
> android:layout_height="YOUR MAX HEIGHT"
> android:gravity="center"
> android:weightsum="YOUR NUMBER OF ITEMS"
> android:orientation="vertical"
> android:layout_gravity="center"
> >
> <TextView
> android:layout_width="wrap_content"
> android:layout_height="0dp"
> android:weight="1" /> ...no of views items
> </LinearLayout> </LinearLayout>
INITIALIZE IT AS
View parentv = findViewById(R.id.activityview); //id of parent view of activity in which popup will be shown
LayoutInflater layoutInflater
= (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.YOURPOPUPMENU, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT);
TextView tv = popupView.findViewById(R.id.textviewid); //to do something with textview
tv.setText("Fashion");
popupWindow.setOutsideTouchable(true); //if you want to dismiss popup if clicked outside
popupWindow.setFocusable(true);
// Removes default background.
popupWindow.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(android.graphics.Color.TRANSPARENT));
// popupWindow.setAnimationStyle(R.style.righttoleftstyle); can put animation to slide in popupmenu by defining any animation
popupWindow.showAtLocation(parentv, Gravity.START | Gravity.TOP,0,0);
//will show popup on top left corner of the screen of the activity view you can try some iterations for it to show where on screen according to your needs
I hope it will help!
Upvotes: 1
Reputation: 76699
PopupMenu
items are just regular MenuItem
; it's the same attribute, but another parent style:
<style name="customPopupMenuStyle" parent="@android:style/Widget.PopupMenu">
<item name="android:listPreferredItemHeightSmall">32dp</item>
</style>
this only affects the PopupMenu
to which the style had been assigned to.
also there one could set a custom ActionView
(per individual item, not as a template):
MenuItem item = menu.findItem(R.id.itemMenu);
MenuItemCompat.setActionView(item, R.layout.layout_menu);
Upvotes: 3
Reputation: 7979
You need to set your preferred item row height in your AppTheme inside styles.xml
:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:listPreferredItemHeightSmall">20dp</item>
</style>
You may need to modify this number a bit to get the correct value, but it determines the height of those rows. Make sure to replace Theme.AppCompat.Light.DarkActionBar
with whatever you're currently using.
As for setting a maximum height, I'm afraid you'll have to create your own extension class for that, as in this example.
Upvotes: 7