androidandroid-listviewandroid-contextmenu

How to open context Menu at specific position in Android and not as menu dialog


How to have context menu open at specify place like the below image. The application is doubleTwistPlayer - See on Play Store

double twist list ellipsis

On Clicking it open context menu there itself, instead of a context menu dialog

on clicking the ellipsis, menu open here


Already Tried

menu_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/itemApplyNew"
        android:title="@string/applyNew"/>

    <item android:id="@+id/itemAppliedLeaves"
        android:title="@string/appliedLeaves"/>    


</menu>

list_item.xml(since I want to implement this in list)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<!-- <RelativeLayout  
    android:id="@+id/relUpperBody"
    android:background="@color/listViewTopContentBackground"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"> -->
<TextView
    android:id="@+id/txvLeaveName"
    style="@style/ListViewItemHeaderText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"        
    android:text="sdfsdfsd" />

<TextView
    android:id="@+id/txvBalanceLeave"
    style="@style/ListViewItemHeaderText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/imgLeaveAction"
    android:text="dsfgd" />
<!-- </RelativeLayout> -->

<TextView
    android:id="@+id/txvAllottedLbl"
    style="@style/ListViewItemTextSize"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txvLeaveName"
    android:layout_marginTop="15dp"
    android:text="@string/allotted" />

<TextView
    android:id="@+id/txvAllottedLeave"
    style="@style/ListViewItemTextSize"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txvBalanceLeave"
    android:layout_alignBaseline="@+id/txvAllottedLbl"
    android:text="sgsdg" />

<ImageView
    android:id="@+id/imgLeaveAction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ellipsis_menu_overflow" />


Question

  1. How to implement it?
  2. Is it context menu or something else?

Solution

  • You need to use a PopupMenu

    Popup menu
    A popup menu displays a list of items in a vertical list that's anchored to the view that invoked the menu. It's good for providing an overflow of actions that relate to specific content or to provide options for a second part of a command. Actions in a popup menu should not directly affect the corresponding content—that's what contextual actions are for. Rather, the popup menu is for extended actions that relate to regions of content in your activity.

    More about implementing it here.