Created
March 19, 2018 10:58
-
-
Save bluemyria/02d214e144f13430f482f2a59b00fe36 to your computer and use it in GitHub Desktop.
Android - 019 - ContextMenu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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