1

I wanted to create a popup menu like following,

enter image description here

I used actionLayout attribute like this,

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_1"
        android:title="@string/title"
        app:showAsAction="always"
        android:actionLayout="@layout/item_1" />
    <item
        android:id="@+id/action_2"
        android:title="@string/title"
        app:showAsAction="always"
        android:actionLayout="@layout/item_2" />
    <item
        android:id="@+id/action_3"
        android:title="@string/title"
        app:showAsAction="always"
        android:actionLayout="@layout/item_3" />
</menu>

where my item_1 layout will be like,

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minWidth="100dp"
    android:padding="5dp">

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#fff9c4"
        app:layout_constraintBottom_toBottomOf="@+id/imageTemp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imageTemp" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/image"
        android:padding="@dimen/dp8"
        android:src="@drawable/ic_baseline_remove_red_eye_24px"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

And I have used this code to create PopupMenu,

PopupMenu popupMenu = new PopupMenu(Objects.requireNonNull(fragment.getActivity()), v);
popupMenu.getMenuInflater().inflate(R.menu.menu_attachment, popupMenu.getMenu());
popupMenu.show();

But unfortunately I couldn't achieve what I want. The current output is,

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • Not exactly the same, but you can get hints of how to do it [here](https://stackoverflow.com/questions/15454995/popupmenu-with-icons) – Kilarn123 Apr 25 '19 at 09:00
  • Also refer [this](https://stackoverflow.com/questions/40671629/change-popup-menu-background-color/45779607) one – Jimale Abdi Apr 25 '19 at 09:02
  • I think You need create a Custom [`PopupWindow`](https://stackoverflow.com/a/15455081/4168607) for this – ADM Apr 25 '19 at 09:24
  • @ADM PopupWindow to view Anchor always bottom to the view... but I want it to top of the view.. is it possible? – Gunaseelan Apr 25 '19 at 10:33
  • I'm not sure about this part . Try dig into `showAtLocation` . – ADM Apr 25 '19 at 10:44

0 Answers0