Add ContextMenu to a customized View in Android
In Android, we may sometimes need to add ContextMenu to a View, it's not so easy to add ContextMenu to a customized View. Here we explain how to add ContextMenu to a customized View.
First, we may need to create View.OnCreateContextMenuListener so that the customized view can register for it.
Here is the class definition:
public class GraphView extends View {
private View.OnCreateContextMenuListener vC = new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(Menu.NONE,R.id.graph_view_refresh,Menu.NONE,"Refresh");
menu.add(Menu.NONE,R.id.save,Menu.NONE,"Save");
}
};
public GraphView(Context context,AttributeSet attrs) {
super(context,attrs);
this.setOnCreateContextMenuListener(vC);
}
}
That's all. Then when you add the View into your Activity. To show the ContextMenu, you only need to call showContextMenu(); then the ContextMenu will show up.
Also, in the Activity class, you may need to listen to the ContextMenu item selection event. In the Activity class, you only need to override the below method :
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.graph_view_refresh:
return true;
case R.id.save:
if(currentView!=null){
currentView.save();
}
return true;
default:
return super.onContextItemSelected(item);
}
}
Hope this will help those who are struggling with making ContextMenu work in the customized view.
By clicking the "Mark as important" button, this article will be put to your important article list which you can find in "Amin->Article important list". Later when you want reread this article, it's easier for you to find it by checking the "Article important list".
Tags:Android, ContextMenu,Customized view Read(3026) Comment(0)
Previous : Behind Start Screen of Windows Phone 8 Next : Click and Drag on xkcd
::Related Articles
::Comment List
No comment for this article.
::Comment
:: Users edited this page
No users edited this page yet.
:: Recent articles
- Content based HTTP Cache
- Select top 3 values from each group in a table with SQL
- Meta tag in HTML header
- Text editor vs IDE
- Sorry, I don't want to download your fucking app
- Display GIF animation while submitting the web form
- PHP to get access token for Sina Weibo app
- Tencent released Q1 earning report of 2013
- How to be jQuery-free?
- Programmer's Mother's Day
- more>>
:: Most read
- TIOBE : C overtakes Java as the No.1 programming language
- Which programming language should I learn first?
- Sony is to release PlayStation4 in 2015
- Disposable Email address
- 5 Free Open Source Chat Applications For Developers
- Never ever touch a programmer
- Hacking Vs. Programming
- TIOBE : Where is that next big programming language?
- Multitasking vs multiprogramming
- Google is developing advanced programming technology to simplify Web application development
- more>>
:: Most commented
- Hacking Vs. Programming
- Which programming language should I learn first?
- Sony is to release PlayStation4 in 2015
- Error handling style in C
- 10 controversial programming opinions?
- C vs Java Complete Comparison
- Google+ is sick
- Disposable Email address
- Unix Philosophy
- A C++ program puzzle
- more>>
:: Find us
