'How to display a custom clickable menu from XML under clicked button in Android?
I have a custom XML file for the options menu which I want to display underneath an ImageButton when it is clicked. I want to be able to get the id (or a name, index...) of the item from the menu when the item is selected.
Here's the XML file of recycler_view_item.xml which is the layout with the ImageButton that's supposed to show the menu on click:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="12dp"
android:layout_marginBottom="55dp"
android:gravity="center_horizontal"
android:orientation="vertical"
app:layout_anchorGravity="center_horizontal">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="0dp"
app:cardBackgroundColor="#182A3E"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="382dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="0dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="43dp"
android:background="@drawable/recycler_item_background"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_item_title"
android:layout_width="200dp"
android:layout_height="43dp"
android:layout_gravity="center_horizontal"
android:layout_marginStart="90dp"
android:layout_marginEnd="0dp"
android:gravity="center|center_horizontal"
android:text="Placeholder"
android:textColor="#F9F9F9"
android:textSize="20sp" />
<ImageButton
android:id="@+id/optionsMenuButton"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginStart="20dp"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:background="@android:color/transparent"
android:paddingLeft="20dp"
android:paddingTop="3dp"
android:paddingRight="20dp"
app:srcCompat="@drawable/ic_options_menu_icon" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/subRecyclerView"
android:layout_width="356dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Here's the XML file of options_menu_view.xml, which is the menu I want to display on click of the optionsMenuButton from the code above:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/optionsMenu"
android:layout_width="190dp"
android:layout_height="118dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/ic_options_menu_background"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/addSubitem"
android:layout_width="match_parent"
android:layout_height="39dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/ic_add_subitem_icon" />
<TextView
android:id="@+id/addItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="46dp"
android:layout_marginTop="11dp"
android:fontFamily="@font/segoe_regular"
android:text="Add Item"
android:textColor="#EBEBEB"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:layout_width="172dp"
android:layout_height="1dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/options_menu_divider" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/editGroup"
android:layout_width="match_parent"
android:layout_height="39dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/ic_edit_group_icon" />
<TextView
android:id="@+id/editGroupText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="46dp"
android:layout_marginTop="11dp"
android:fontFamily="@font/segoe_regular"
android:text="Edit Group"
android:textColor="#EBEBEB"
android:textSize="17sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:layout_width="172dp"
android:layout_height="1dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/options_menu_divider" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/deleteGroup"
android:layout_width="match_parent"
android:layout_height="39dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_delete_group_icon" />
<TextView
android:id="@+id/deleteGroupText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="46dp"
android:layout_marginTop="9dp"
android:fontFamily="@font/segoe_regular"
android:text="Delete Group"
android:textColor="#C93434"
android:textSize="17sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
What I would like it to look like when displayed: Option Menu Example
I've been trying to find the solution for quite some time, but I'm quite new and to be honest, I don't even know what exactly I'm supposed to be searching for. Your help is greatly appreciated and sorry if this was posted in the past.
Thank you in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
