Akhil Jain
Akhil Jain

Reputation: 14203

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?

Upvotes: 1

Views: 6049

Answers (3)

Androider
Androider

Reputation: 3873

Use the pop-up menu. Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu.

check it here.

Upvotes: 0

LordRaydenMK
LordRaydenMK

Reputation: 13321

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.

Upvotes: 2

Arun Mehra
Arun Mehra

Reputation: 559

I think that is a ListPopupWindow, all you need to do is create a callback from your list adapter to your fragment / activity and pass the clicked view in the callback as a parameter and using the ListPopupWindow set the anchor point to the view you have passed, you can create your own custom adapter and set it to the ListPopupWindow and pass the list of items that you want to show in the pop up, hope this helps.

Upvotes: 1

Related Questions