Custom PopUp Menu
Asked Answered
H

3

5

Hello i have to create a PopUp Menu and i know how to do that.

Here is my code to create 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 customize it? I want to add custom fonts in PopUp menu with semi transparent background as shown in figure. Please help...!!!

enter image description here

Homoousian answered 4/12, 2014 at 10:23 Comment(3)
Create a custom dialog instead, with listview and custom list item.Cogitative
@MurtazaHussain : If Create a custom Dialog. i'll have to set its position according to the button click. because i don't to show it in the center. i want to show it near the button click. so i would rather prefer pop-up menu.Homoousian
Than go here androidhive.info/2013/11/android-working-with-action-bar. Point 18Cogitative
A
8

You can use ListPopupWindow. You can submit your custom Adapter to the ListPopupWindow's object, and customize its appearance into getView

Alexina answered 4/12, 2014 at 10:29 Comment(0)
I
2

You can use android:actionViewClass property in the menu xml to define you own custom class

Infighting answered 4/12, 2014 at 10:27 Comment(0)
R
1

Customize android:spinnerDropDownItemStyle for actionBarWidgetTheme changing it's text appearance. Also don't forget that dropdown list is managed by the adapter you use. Then if you used the standard one (simple_dropdown_item_1line) there's no problem. But if you used a custom one like me (to be able to add an icon) don't forget to apply style="?attr/spinnerDropDownItemStyle" in your layout TextView.

final custom style is:

<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>
Raving answered 4/12, 2014 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.