I created a new menu, named drmenu.xml. It works correctly when i press menu button, but I need to open a context menu when the user press the button. The below code the button just show a toast.
this is my xml layout:
<LinearLayout
android:id="@+id/MenuCall"
android:layout_width="90dip"
android:layout_height="match_parent"
android:gravity="right|center_vertical" >
<ImageView
android:id="@+id/MenuCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/imageiew6" />
</LinearLayout>
and this is my java code:
final LinearLayout callback_var = (LinearLayout) findViewById(R.id.MenuCall);
registerForContextMenu(callback_var);
callback_var.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "this is repeated", Toast.LENGTH_LONG).show();
openContextMenu(callback_var);
}
The Android context menu is alike to the right-click menu in Windows or Linux. In the Android system, the context menu provides actions that change a specific element or context frame in the user interface and one can provide a context menu for any view.
A context menu is a pop-up menu that provides shortcuts for actions the software developer anticipates the user might want to take. In a Windows environment, the context menu is accessed with a right mouse click.
Android context menu appears when user press long click on the element. It is also known as floating menu. It affects the selected content while doing action on it. It doesn't support item shortcuts and icons.
If you want create a context Menu, you have to call the method registerForContextMenu()
passing it the View that should be associated with the context menu.
For example, supposing to associate the context menu with a Button:
Button button = (Button) findViewById(R.id.my_button);
registerForContextMenu(button);
which can be called in the onCreate() of your Activity. Then, inside the same activity, you need to override the onCreateContextMenu()
method.
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_context_menu, menu);
}
Then you have to implement onContextItemSelected()
, for triggering the proper action when a item inside the context menu is pressed:
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.first_action:
// your first action code
return true;
case R.id.second_action:
// your second action code
return true;
default:
return super.onContextItemSelected(item);
}
}
Now the long press click on the button opens the context menu you defined in the your_context_menu.xml
file.
Consider that opening the context menu with a long press is compliant with the Android standard UI, however If you want your context menu to appear on a simple tap you can look here the answer
NOTE: As said here
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With