import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
import android.widget.Toast;
/**
* Class which shows how to use context menus
*
* @author FaYnaSoft Labs
*/
public class Main extends Activity {
private static final int NEW_MENU_ITEM = Menu.FIRST;
private static final int SAVE_MENU_ITEM = NEW_MENU_ITEM + 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.textId);
tv.setText("TextView element");
registerForContextMenu(tv);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case NEW_MENU_ITEM:
showMsg("New");
break;
case SAVE_MENU_ITEM:
showMsg("Save");
break;
}
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("My Context Menu");
menu.add(0, NEW_MENU_ITEM, 0, "New");
menu.add(0, SAVE_MENU_ITEM, 1, "Save");
}
private void showMsg(String message) {
Toast msg = Toast.makeText(Main.this, message, Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
msg.getYOffset() / 2);
msg.show();
}
}