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...
Related
Iet's say I have 3 tabs, i use slidingTabLayout
so 3 tabs are 3 fragments.
i use toolbar, so each fragment has toolbar with textview and icons.
for fragment 1 and 2, icon is the same "search icon", if click it, will go to another activity for searching something.
if user at "search activity" there is a "back arrow" on tool bar, click it, user should go back to previous fragment.
if user in fragment 1, and then he click "search" he go to "search activity", and after searching, he click "back arrow", he should go back to fragment 1.
but if user in fragment 2, he click "search" he go to the same "search activity", and after searching, he click "back arrow", he should go back to fragment 2.
so i googled "click go back arrow , from same activity back to different fragment"
i did not find clue ...i think it might be something with "fragment Manager", "backPressed"....i lost myself now
I guess i might not use the correct words for google.....
I think this could work.
Have a searchable activity, a link for that is here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
onSearchRequested();
return true;
}
return super.onOptionsItemSelected(item);
}
In the Search activity, have something like this in the OnCreate method:
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
And then also in the search activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
Hope it works!
I think your problem can be solved in onBackPressed() function.
It works like a back button.
You can try to add your arrow icon like this.
public void onClick() {
onBackPressed();
}
Hope it helps.
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;}}
How to create overflow menu in application. but I don't want the overflow menu in action bar. But i want it to be attached any where in the application. I have seen one music app in which something similar is done. I want ot include it in my application. I am attaching image fro the same.
Clicking on the image.. on drop down menu will come similar to overflow menu in action bar. Can anybody tell me how to do it. Any help will be appreciated.
User117 comment was helpful and worked for me.
You can use this class and create overflow menu fron any widget.
Refer Pop up menu link.
private void setOverflowMenu(View view)
{
popup = new PopupMenu(getActivity(), view.findViewById(R.id.image_overflow));
popup.getMenu().add(Menu.NONE, OPTION1, Menu.NONE, "My option 1");
popup.getMenu().add(Menu.NONE, OPTION2, Menu.NONE, "My option 2");
popup.getMenu().add(Menu.NONE, OPTION3, Menu.NONE, "My option 3");
popup.setOnMenuItemClickListener(this);
}
Called from fragment thus passed view as parameter. If called from activity you can directly call this method. view.findViewById(R.id.image_overflow) is the image from which I need to attach the menu. then Implemented PopupMenu.OnMenuItemClickListener in my class which force me to implement below method
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch (item.getItemId())
{
case OPTION1:
Toast.makeText(getActivity(), "OPTION1 clicked", Toast.LENGTH_LONG).show();
break;
case OPTION2:
Toast.makeText(getActivity(), "OPTION2 clicked", Toast.LENGTH_LONG).show();
break;
case OPTION3:
Toast.makeText(getActivity(), "OPTION3 clicked", Toast.LENGTH_LONG).show();
break;
}
return false;
}
That it. every thing works. AND don't forget to add click listener for widget(imageview in my case).
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.
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.