Skip to content

Instantly share code, notes, and snippets.

@bluemyria
Created March 19, 2018 10:58
Show Gist options
  • Save bluemyria/02d214e144f13430f482f2a59b00fe36 to your computer and use it in GitHub Desktop.
Save bluemyria/02d214e144f13430f482f2a59b00fe36 to your computer and use it in GitHub Desktop.
Android - 019 - ContextMenu
public class MainActivity extends AppCompatActivity {
private TextView tv;
private Button btn;
private CheckBox cb;
private MenuItem miBtnMenuitem1;
private MenuItem miBtnMenuitem2;
private MenuItem miTvMenuitem1;
private MenuItem miTvMenuitem2;
private MenuItem miTvMenuitem3;
private MenuItem miCbMenuitem1;
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if(v==btn) {
miBtnMenuitem1 = menu.add("Btn - Menuitem 1");
miBtnMenuitem2 = menu.add("Btn - Menuitem 2");
}
if(v==tv) {
miTvMenuitem1 = menu.add("TV-Menuitem 1");
miTvMenuitem2 = menu.add("TV-Menuitem 2");
miTvMenuitem3 = menu.add("TV-Menuitem 3");
}
if(v==cb) {
miCbMenuitem1 = menu.add("CB-Menuitem 1");
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
String text = "Context: " + item.getTitle().toString();
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
return super.onContextItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
btn = findViewById(R.id.btn);
cb = findViewById(R.id.cb);
registerForContextMenu(tv);
registerForContextMenu(btn);
registerForContextMenu(cb);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment