Custom PopupMenu (layout)

I am trying to update my PopupMenu, so it will contain icons and custom styles.
I created a new layout for him

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
        android:id="@+id/layout_sharea"
        android:background="@drawable/share"
        android:paddingLeft="10.0dip"
        android:paddingRight="10.0dip"
        android:layout_width="wrap_content"
        android:layout_height="50.0dip"
        android:onClick="share">
        <TextView
            android:id="@+id/sharetexta"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/share"
            android:drawableLeft="@drawable/share_button"
            android:drawablePadding="10.0dip"
            android:layout_centerVertical="true" />
     </RelativeLayout>
     <RelativeLayout
        android:id="@+id/layout_shareb"
        android:background="@drawable/share"
        android:paddingLeft="10.0dip"
        android:paddingRight="10.0dip"
        android:layout_width="wrap_content"
        android:layout_height="50.0dip"
        android:layout_below="@+id/layout_sharea"
        android:onClick="share">
        <TextView
            android:id="@+id/sharetextb"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/share"
            android:drawableLeft="@drawable/share_button"
            android:drawablePadding="10.0dip"
            android:layout_centerVertical="true" />
     </RelativeLayout>
</RelativeLayout>

I want PopupMenu to be configured (like this layout), as in this picture Popupmenu

+3
source share
2 answers

A PopupMenu is designed to display menus , and this is actually not a good way to customize the appearance of menu items. If you want something more flexible, your answer is ListPopupWindow .

private static final String TITLE = "title";
private static final String ICON = "icon";

private List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

// Use this to add items to the list that the ListPopupWindow will use
private void addItem(String title, int iconResourceId) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put(TITLE, title);
    map.put(ICON, iconResourceId);
    data.add(map);
}

// Call this when you want to show the ListPopupWindow
private void showListMenu(View anchor) {
    ListPopupWindow popupWindow = new ListPopupWindow(this);

    ListAdapter adapter = new SimpleAdapter(
            this,
            data,
            android.R.layout.activity_list_item, // You may want to use your own cool layout
            new String[] {TITLE, ICON}, // These are just the keys that the data uses
            new int[] {android.R.id.text1, android.R.id.icon}); // The view ids to map the data to


    popupWindow.setAnchorView(anchor);
    popupWindow.setAdapter(adapter);
    popupWindow.setWidth(400); // note: don't use pixels, use a dimen resource
    popupWindow.setOnItemClickListener(myListener); // the callback for when a list item is selected
    popupWindow.show();
}
+21
source

ListPopupWindow

Model

public class Item {
    private String title;
    private int imageRes;

    public Item(String title, int imageRes) {
        this.title = title;
        this.imageRes = imageRes;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getImageRes() {
        return imageRes;
    }

    public void setImageRes(int imageRes) {
        this.imageRes = imageRes;
    }
}

ListAdapter

public class ListPopupWindowAdapter extends BaseAdapter {
    LayoutInflater mLayoutInflater;
    List<Item> mItemList;

    public ListPopupWindowAdapter(Context context, List<Item> itemList) {
        mLayoutInflater = LayoutInflater.from(context);
        mItemList = itemList;
    }

    @Override
    public int getCount() {
        return mItemList.size();
    }

    @Override
    public Item getItem(int i) {
        return mItemList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvTitle.setText(getItem(position).getTitle());
        holder.ivImage.setImageResource(getItem(position).getImageRes());

        return convertView;
    }

    static class ViewHolder {
        TextView tvTitle;
        ImageView ivImage;

        ViewHolder(View view) {
            tvTitle = view.findViewById(R.id.text);
            ivImage = view.findViewById(R.id.image);
        }
    }
}

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

, ListPopupWindow

private void showListPopupWindow(View anchor) {
    final ListPopupWindow popupWindow = new ListPopupWindow(this);

    List<Item> itemList = new ArrayList<>();
    itemList.add(new Item("A", R.mipmap.ic_launcher));
    itemList.add(new Item("B", R.mipmap.ic_launcher));
    itemList.add(new Item("C", R.mipmap.ic_launcher));

    ListAdapter adapter = new ListPopupWindowAdapter(this, itemList);
    popupWindow.setAnchorView(anchor);
    popupWindow.setAdapter(adapter);
    popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            popupWindow.dismiss();
        }
    });
    popupWindow.show();
}
+3

All Articles