I originally had a menu that looked like:
Line1
Line2
Line3
The app was published. I then added in another Item, so it now looks like this:
Line1
Line2
Line4 << newly added item
Line3
I updated all my code and it works, but with some added side effects. The original "Line3" was pulling up a app list to share 'something'. Line 4 was added in to open another specific app. What happens now is if I click on Line4, the app opens and all is well until I hit the back button to go back to the original app, I see that the app list for sharing 'Something' opened up as well, meaning I have to click the back button twice.
This is my code (representing the second example above)
/**creates action bar**/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/**handles which action item is selected**/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.stuff:
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
return true;
case R.id.MoreStuff:
//things
return true;
case R.id.TheProblemStuff:
Intent intent1 = getPackageManager().getLaunchIntentForPackage("anApp");
if (intent1 != null)
{
/* we found the activity now start the activity */
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
}
else
{
/* bring user to the market or let them choose an app? */
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("a website .com"));
startActivity(intent);
}
case R.id.TheOtherProblemStuff:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Check out this app!");
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,("myapp"));
startActivity(Intent.createChooser(sharingIntent,"Share using"));//this is the menu that still comes up when "TheProblemStuff" is clicked. This use to be in the spot that "TheProblemStuff" was
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and here is my xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.gpstest1.MainActivity" >
<item
android:id="#+id/Stuff"
android:title="#string/action_settings"
android:orderInCategory="1"
app:showAsAction="never"/>
<item
android:id="#+id/MoreStuff"
android:title="#string/action_map"
android:orderInCategory="2"
app:showAsAction="never"/>
<item
android:id="#+id/TheProblemStuff"
android:title="#string/speedometer"
android:orderInCategory="3"
app:showAsAction="never"/>
<item
android:id="#+id/TheOtherProblemStuff"
android:title="#string/action_share_app"
android:orderInCategory="4"
app:showAsAction="never"/>
</menu>
you forgot the return true; line inside the R.id.TheProblemStuff: case, so it continues to the R.id.TheOtherProblemStuff case and only then stops.
Related
I define a SettingsActivity on my app and i want to show this activity when pressing on the 3 dot on the right side of the screen.
How to do it ?
I can't find any manual/demo that help me to do it.
First of all, create a menu resource file and add the following code.
<group android:checkableBehavior="single">
<item
android:id="#+id/settings"
android:title="Settings"/>
</group>
then, in your MainActivity (Activity where you want to show the three dots in toolbar) create a method onCreateOptionsMenu and onOptionItemSelected,
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater.inflate(R.id.menu_settings, menu);
MenuItem menuItem = menu.findItem(R.id.settings);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getId();
if (id == R.id.settings){
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
return super.onOptionsItemSelected(item);
}
I have an android app that has a gridview in it. The gridview items contain among other things, a button to show context sensitive menus. So, I implemented a popup menu that comes up when they touch the button in the gridview item.
This menu contains 3 items:
Edit Item
Delete Item
Share Item
I have successfully implemented the edit and delete menu items. The problem is with the "Share Item" menu item. It is a ShareActionProvider. I previously implemented these menu choices as an ActionMode (menu items across the top). But now that the menu is a popup, I'm not sure how to implement the "Share Item" menu option.
Here is my popup_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/group_edit_mode">
<item
android:id="#+id/MenuItemEdit"
android:title="#string/item_option_edit"
app:showAsAction="withText|ifRoom" />
<item
android:id="#+id/MenuItemDelete"
android:title="#string/delete"
app:showAsAction="withText|ifRoom" />
<item
android:id="#+id/MenuItemShare"
android:title="#string/share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
</group>
</menu>
Here is the popup menu code:
PopupMenu popupMenu = new PopupMenu(MINMainActivity.getSharedInstance(), optionButton);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.gridview_edit_menu_single_item, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
boolean choiceHandled = false;
int itemID = item.getItemId();
switch (itemID)
{
case R.id.MenuItemEdit:
MINPageTypeGridFragment.launchAlbumItemDetails(mFragment, albumItem, mPageItem.pageConfigFileName);
mFragment.currentMode = MINPageTypeGridFragment.MODE_STANDARD;
choiceHandled = true;
break;
case R.id.MenuItemDelete:
MINPageTypeGridFragment.deleteItem(mFragment, mAlbum, albumItem);
mFragment.currentMode = MINPageTypeGridFragment.MODE_STANDARD;
choiceHandled = true;
break;
case R.id.MenuItemShare:
choiceHandled = true;
mFragment.currentMode = MINPageTypeGridFragment.MODE_STANDARD;
break;
}
return choiceHandled;
}
});
popupMenu.show();
This was WAY overthought. I just kept it as a button and created a chooser.
public void onShareClick(MINAlbumItem albumItem)
{
List<MINAlbumItem> albumItemsArray = new ArrayList<MINAlbumItem>();
albumItemsArray.add(albumItem);
// Creates intent and loads data from items array
Intent intent = mFragment.Share(albumItemsArray);
MINMainActivity.getSharedInstance().startActivity(Intent.createChooser(intent, MINMainActivity.getSharedInstance().getResources().getString(R.string.send_to)));
}
I have an action bar in my application with one button that should be options button.
When you click it, it should open few options.
I made a String-Array in the Strings.xml file but I can't get it to work.
Any code samples?
I searched the internet but couldn't find anything.
Thanks!
Vogella provides good tutorials. Here is the ActionBar tutorial which shows how to do that: http://www.vogella.com/tutorials/AndroidActionBar/article.html
Make sure you have an xml document in your menu folder in resources called something like mainmenu.xml which should look like the following:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:title="Settings">
</item>
</menu>
Then make sure you have the following functions in your Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_refresh:
Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT)
.show();
break;
// action with ID action_settings was selected
case R.id.action_settings:
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}
I have a menu with a submenu. The parent item is "Options" and its children are "Call", "Navigate", and "Edit". Here is my activity code that handles the menu click. The problem is that the first submenu always gets selected by the event. With this code if i click on "Call"(the first child item) the onOptionsItemSelected() method registers the R.id.record_edit item being selected. I don't know why this is happening since the "Call" menu item has the id of "record_call". I've tried switching the submenu items around in the xml but the first submenu item is the only one that registers and it always registers as R.id.record_edit. Not sure what's wrong with my code.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.record_edit:
Intent intent = new Intent(this, EditRecordActivity.class);
myApp.setRecord(record);
Bundle b = new Bundle();
b.putBoolean("new", false);
b.putString("pageTitle", pageTitle);
b.putString("meta_universalid", meta_universalid);
intent.putExtras(b);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
xml menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/record_options"
android:showAsAction="ifRoom|withText"
android:title="..."
android:titleCondensed="Options"
android:enabled="true">
<menu>
<item
android:id="#+id/record_call"
android:showAsAction="ifRoom|withText"
android:title="#string/record_call"/>
<item
android:id="#+id/record_navigate"
android:showAsAction="ifRoom|withText"
android:title="#string/record_navigate"/>
<item
android:id="#+id/record_edit"
android:showAsAction="ifRoom|withText"
android:title="#string/record_edit"/>
</menu>
</item>
</menu>
just checked with your menu to be sure. It is working fine with me. Check whether your overall format is like the following code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.record_call:
Toast.makeText(this, "call", 1000).show();
return true;
case R.id.record_edit:
Toast.makeText(this, "edit", 1000).show();
return true;
case R.id.record_navigate:
Toast.makeText(this, "navigate", 1000).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I use an xml to for my ContextMenu, which is like :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/Ordermenu" android:title="Order">
<menu android:id="#+id/OrderBySubMenu">
<item android:id="#+id/OrderByASC" android:title="Order ASC" />
<item android:id="#+id/OrderByDESC" android:title="Order DESC" />
<item android:id="#+id/Cancel" android:title="Cancel" />
</menu>
</item>
<item android:id="#+id/ActionAmenu" android:title="Action A"/>
</menu>
I use following code to display the menu, in my onCreateContextMenu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.my menu, menu);
I manage option click with following code :
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Displaymenu:
//do stuff
return true;
case R.id.OrderByASC:
//do stuff
return true;
case R.id.OrderByDESC:
//do stuff
return true;
default :
return(super.onOptionsItemSelected(item));
}
Starting the Context Menu it display Two options:
Order
Action A
Clicking on Order show a submenu :
Order ASC
Order DESC
Cancel
Now, If the user click on cancel (or click on the hardware back button), no action is specified, so it call super.onOptionsItemSelected(item) which go back to my main activity.
How can I manage to go back to the main menu in such case? i.e. diplay the initial :
Order
Action A
I tried this long ago but i think you will have to override onPrepareOptionsMenu as well to get this to work. This is called before it shows, and you will have to put flags here on what items to show for the user.
Try something like this:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
// Clear the previous layout
menu.clear();
if(showMainMenu)
{
// Add main menu items..
menu.add(0, R.id.ordermenu, 0, "True");
}
else
{
// Add sub-menu items
menu.add(0, R.id.ordermenuASC, 0, "True");
}
return super.onPrepareOptionsMenu(menu);
}
So when user clicks a main menu item, change the boolean flag a redo the process.
Finally, it worked only by adding :
case R.id.Cancel:
openContextMenu(findViewById(selected_view_id));
return true;
in public boolean onContextItemSelected(MenuItem item)
selected_view_id is stored by
selected_view_id=v.getId();
in onCreateContextMenu
Hope it will help others.