|
|
Short press the button to run the method onClick(..) in setOnClickListener(..). Long press the button to execute callback code in the onContextItemSelected(..) method. See a toast popup for both and a TextView message from the last action remains. The registerForContextMenu(button1) produces a call to onCreateContextMenu(..) to define the Context Menu. The Context Menu is created from the MenuInflater method inflate on file context_menu.xml.
package com.brainyideas.dev.poc.cmsxbtpu;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ContextMenuApp extends Activity {
String msg_start = "start";
String msg = "Selected Context Menu is ... ";
TextView textView1;
Button button1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(msg_start);
button1 = (Button) findViewById(R.id.button1);
button1.setText("long press me (keep finger down)");
button1.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
seeToastAndMsg("regular button press");
}
});
registerForContextMenu(button1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
seeToastAndMsg(msg + "Menu1");
return true;
case R.id.menu2:
seeToastAndMsg(msg + "Menu2");
return true;
default:
return super.onContextItemSelected(item);
}
}
void seeToastAndMsg(String toast) {
textView1.setText(toast);
Toast.makeText(
getApplicationContext(),
toast,
Toast.LENGTH_SHORT).show();
}
}<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu1" android:title="@string/menu1" /> <item android:id="@+id/menu2" android:title="@string/menu2" /> </menu>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ContextMenuSelect_XMLBased-ToastPopUp</string>
<string name="msg_start">Start</string>
<string name="menu1">Menu1</string>
<string name="menu2">Menu2</string>
</resources>