Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Created June 10, 2013 21:04
Show Gist options
  • Save Cheesebaron/5752281 to your computer and use it in GitHub Desktop.
Save Cheesebaron/5752281 to your computer and use it in GitHub Desktop.
Just a simple PopupMenu sample.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/MyButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Click Me!" />
<ImageView
android:layout_below="@+id/MyButton"
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MyImageView"
android:layout_alignParentRight="true" />
<TextView
android:id="@+id/MyTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me as well!"
android:layout_centerInParent="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/group_popupmenu">
<item android:id="@+id/menu1"
android:title="Popup menu item 1"/>
<item android:id="@+id/menu2"
android:title="Popup menu item 2"/>
<item android:id="@+id/menu3"
android:title="Popup menu item 3"/>
</group>
</menu>
using Android.App;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace PopUpMenuSample
{
[Activity(Label = "PopUpMenuSample", MainLauncher = true, Icon = "@drawable/icon")]
public class PopUpMenuSampleActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += (sender, args) => ShowPopupMenu((View)sender);
var imageView = FindViewById<ImageView>(Resource.Id.MyImageView);
imageView.Click += (sender, args) => ShowPopupMenu((View)sender);
var textView = FindViewById<TextView>(Resource.Id.MyTextView);
textView.Click += (sender, args) => ShowPopupMenu((View)sender);
}
private void ShowPopupMenu(View v)
{
var popupMenu = new PopupMenu(this, v);
popupMenu.MenuInflater.Inflate(Resource.Menu.popupmenu, popupMenu.Menu);
popupMenu.MenuItemClick += (sender, args) => Toast.MakeText(this, string.Format("Item {0} was clicked", args.Item.TitleFormatted), ToastLength.Long)
.Show();
popupMenu.Show();
}
}
}
@ranjeet18
Copy link

Nice example Cheesebaron....very helpfull

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment