User menu PopUp

Hello, I need to create a PopUp menu, and I know how to do it.

Here is my code for creating the default PopUp menu.

popup_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/item"
    android:showAsAction="ifRoom|withText"
    android:title="item1"
    android:visible="true"/>
<item
    android:id="@+id/item2"
    android:showAsAction="ifRoom|withText"
    android:title="item2"
    android:visible="true"/>
<item
    android:id="@+id/item3"
    android:showAsAction="ifRoom|withText"
    android:title="item3"
    android:visible="true"/>

PopUpMenu_Activity.java

findViewById(R.id.btn_click).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            PopupMenu popupMenu = new PopupMenu(PopMenuActivity.this, view);
            popupMenu.setOnMenuItemClickListener(PopMenuActivity.this);
            popupMenu.inflate(R.menu.popup_menu);
            popupMenu.show();
        }
    });

and

public boolean onMenuItemClick(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.item1:
        Toast.makeText(this, "item1 clicked", Toast.LENGTH_SHORT).show();
        return true;
    case R.id.item2:
        Toast.makeText(this, "item2 clicked", Toast.LENGTH_SHORT).show();
        return true;
    case R.id.item3:
        Toast.makeText(this, "item3 clicked", Toast.LENGTH_SHORT).show();
        return true;
    default:
        return false;
    }

}

My question is: how can I configure it? I want to add custom fonts to the PopUp menu with a translucent background as shown. Please, help...!!!

enter image description here

+4
source share
3 answers

You can use ListPopupWindow . You can send your custom adapter to the ListPopupWindow object and configure its appearance togetView

+8
source

android:actionViewClass xml,

+2

Customize android:spinnerDropDownItemStylefor actionBarWidgetThemeby changing its appearance. Also do not forget that the drop-down list is controlled by the adapter you are using. Then, if you used the standard (simple_dropdown_item_1line), there is no problem. But if you used a custom one similar to me (to add an icon), be sure to apply style="?attr/spinnerDropDownItemStyle"in your layout TextView.

End user style:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="Theme.myapp" parent="@style/Theme.Light.DarkActionBar">
    <item name="android:actionDropDownStyle">@style/myapp_DropDownNav</item>        
    <item name="android:actionBarWidgetTheme">@style/myapp.actionBarWidgetTheme</item>
</style>

<style name="myapp.actionBarWidgetTheme" parent="@style/Theme.">
     <item name="android:spinnerDropDownItemStyle">@style/myapp.Widget.DropDownItem.Spinner</item>
</style>

<style name="myapp_DropDownNav" parent="@style/Widget.Spinner.DropDown.ActionBar">
    <item name="background">@drawable/spinner_background_ab_myapp</item>
    <item name="android:background">@drawable/spinner_background_ab_myapp</item>
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_myapp</item>
    <item name="android:dropDownSelector">@drawable/selectable_background_myapp</item>
</style>

<style name="myapp.Widget.DropDownItem.Spinner" parent="Widget.DropDownItem.Spinner">
    <item name="android:textAppearance">@style/myapp.TextAppearance.Widget.DropDownItem</item>
</style>

<style name="myapp.TextAppearance.Widget.DropDownItem" parent="TextAppearance.Widget.DropDownItem">
    <item name="android:textColor">@color/black</item>
</style>
+1
source

All Articles