In my android application I have a listview with set of records. I have implemented the OnItemClickListener and set it to the listview. So when user click any listview Item it redirects to next activity and do the stuff I need.
The problem is, I have to implement the same functionality to be performed using android Menu.(When click menu button). When click menu button it displays a menu item, to perform Listviews focused item to call click event. Simply when click menu item it should perform the the focused listview item click, as when we normally click the listview item.
Have no idea of how to call click event
Can anyone guide me how to do this please....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Taken from http://developer.android.com/guide/topics/ui/menus.html.
Related
I want to show an Alert Dialog when the user clicks on the up button of ActionBar. I have implemented onBackPressed() already. What I want to do now is trigger same action as for pressing Back button when user clicks the Up button from ActionBar.
I tried using onNavigateUp(). However, it just returns me to the parent activity without showing the Alert Dialog.
from this answer you can override onOptionsItemSelected to detect action
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
//enter your code here
return true;
}
}
return super.onOptionsItemSelected(item);
}
try it
I have AlbumListActivity, I am fetching list of albums from internet. When album on the list is clicked, user is taken to AlbumDetailActivity. In AlbumDetailActivity when I press phone's back button, it takes me back to AlbumListActivity and when I press back button on ActionBar it again takes me back to AlbumlistActivity but reloading data in the list. I red this post and I understand that Back and Up are different things. But in this case it really does not matter because AlbumDetailActivity will not be open by another application.
on pressing the Up button, just call the
onBackPressed()
method like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
i want to add another one main item icon(the left one from title, hamburger icon) at Toolbar (SupportActionBar).
I want to hide it in one case and show in another (for back arrow in one case, and change it to hamburger icon in another). So is there a way to do so?
And i want it as menu item, not just change the icon at item. I want to use it this way
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.hamburger:
setActionBarMenu();
return true;
case android.R.id.back:
setActionBarMenu();
return true;}}
I wanted to create application where items in list ll be editting when you select item and press in menu "Edit", but items in list dont selecting...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
final long id = this.getSelectedItemId();
switch (item.getItemId()) {
case IDM_EDIT:
if (id > 0) {
CallEditContactDialog(id);
}
else {
Toast.makeText(this, R.string.toast_notify, Toast.LENGTH_SHORT)
.show();
}
break;
}
return(super.onOptionsItemSelected(item));
}
Maybe it is working for android 2nd, but i make it for third too and can test it just on tab 8.9 android 3.2...
Sorry for my english and knowledge in android.
Am i right that you have some items in the list in activity, you want to pres on one of it then press menu button and press Edit ?
I think you have to save id when press on the item in list and then perform change action with this id onOptionsItemSelected...
I have created a submenu using the manifest file. I have four text fields in my submenu. I want to know which submenu item the user clicked. I know about the switch case item.getItemId condition for the menu items. But i want to know how to check for which submenu the user clicked.
Thanks for any help
You could save the id of the submenu item to a variable. For example:
int item;
#Override
public void onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.something: {
this.item = something;
return true;
}
}
Now the variable item contains the id of the item selected.