Get Context of Popupmenu Like Contextmenu

Get context of PopupMenu like ContextMenu

So I figured out that in order to have some context of which menu icon was clicked, I utilized the setTag() and getTag() methods of the View class and just applied these methods to the ImageView (menu icon).

Create on Android a Context Menu when selecting an Item from a Popup Menu

I have found an alternative to Context Menus by using List Dialog. This way is much easier that the one I considered.

How to create Popup Menu in Context Menu?

In Java when i long press the button it shows context menu, then i select context menu it shows popup menu.

Button button = findViewById(R.id.button);
registerForContextMenu(button);

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Upload");
menu.add(0, v.getId(), 0, "Search");
menu.add(0, v.getId(), 0, "Share");
menu.add(0, v.getId(), 0, "Bookmark");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
try {
showPopup(findViewById(item.getItemId()));
} catch (Exception e) {
e.printStackTrace();
}
return true;
}

private void showPopup(View anchorView) {
PopupMenu popup = new PopupMenu(this, anchorView);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MapsActivity.this, "Selected Item: "
+ item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.inflate(R.menu.menu_example);
popup.show();
}

Open dialog exactly above clicked item like context menu

   btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

// infalte menu

PopupMenu popup = new PopupMenu(activity, v);
popup.getMenuInflater().inflate(R.menu.clipboard_popup,
popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {

switch (item.getItemId()) {
case R.id.install:

// Or Some other code you want to put
// here.. This is just an example.
Toast.makeText(
activity,
" Install Clicked at position " + " : "
+ position, Toast.LENGTH_LONG)
.show();

break;
case R.id.addtowishlist:

Toast.makeText(
activity,
"Add to Wish List Clicked at position "
+ " : " + position,
Toast.LENGTH_LONG).show();

break;

default:
break;
}

return true;
}
});

}
});

Updated answer with custom layout

 PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(clickbtn, -40, 18);

// where u want show on
//view click event popupwindow.showAsDropDown(view, x, y);

public PopupWindow popupDisplay()
{

final PopupWindow popupWindow = new PopupWindow(this);

// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view = inflater.inflate(R.layout.mylayout, null);

Button item = (LinearLayout) view.findViewById(R.id.button1);

popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);

return popupWindow;
}

mylayout.xml

    <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>

PopupMenu in wxPython with key press activated context menu items

Tried it out and learned something (tested on Win8/wxPython 2.9.5.1):

Example popup:

Mind the two different underline styles here. Both work with both the lowercase and uppercase letter.

  • Applying the Accelerator by accelerator entry:

    menu = wx.Menu()
    item = wx.MenuItem(menu, self.popupID1, "One\tO")
    acc = wx.AcceleratorEntry()
    acc.Set(wx.ACCEL_NORMAL, ord('O'), self.popupID1)
    item.SetAccel(acc)
    menu.AppendItem(item)

Allows to get rid of the underscore.

  • Applying the accelerator by modifying the menu item string with &:

    menu.Append(self.popupID2, "Two\t&T")

Context menu shows changes on next click

I guess that's happening because you're actually modifying the state of the item once it's already being shown.

Use the PopupMenuShowing event instead. Here is an example on how to modify the PopUpMenu using a GridView.

private void Whatever_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
{
var menu = e.Menu;
var hi = e.HitInfo;
if (!(sender is GridView view))
return;

var inDetails = (hi.HitTest == GridHitTest.EmptyRow);
if (menu == null && inDetails)
{
menu = new DevExpress.XtraGrid.Menu.GridViewMenu(view);
e.Menu = menu;
}
if (menu == null)
return;

//If there are any entries, show "Duplicate" button
var rowHandle = hi.RowHandle;
if (!view.IsDataRow(rowHandle)) return;
var mnuDuplicate = new DXMenuItem("Duplicate",
async delegate { await ClickDuplicate(); },
Properties.Resources.copy_16x16)
{
BeginGroup = true
};
menu.Items.Add(mnuDuplicate);
}


Related Topics



Leave a reply



Submit