Create context menu on menu item click - android

I am in strange situation and I tried to search everywhere but i didn't find anything useful. May be I am following bad design. But here is my situation:
I have AppBar in my app and I have added ActionButton on app bar which we do normally. Now I want to display context menu when user clicks on any of the action button of app bar.
For example: If I have setting button on app bar and if user clicks on that button then I want to display context menu having multiple options.
I know how to create context menu and handle context menu item clicks but i don't know how to transfer control from action button click which lead to display ContextMenu.
Here is my code:
//inflating context menu which will display when user clicks app bar button example like setting
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu_sort, menu);
}
//handling context menu item clicks
#Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
But I am not sure how to handle app bar button clicks which will display context menu:
//Below code is to handle app bar item clicks
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
//handling the menu clicks on menu.xml
switch (id){
//on below action_add click i want to display context menu
case R.id.action_add:
//not sure what to code here
break;
}
Thanks for your help

If i am understanding you right you want to show a submenu?
Then you need to add a menu tag in your R.menu.context_menu_sort.
Like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/file"
android:title="#string/file" >
<!-- "file" submenu -->
<menu>
<item android:id="#+id/create_new"
android:title="#string/create_new" />
<item android:id="#+id/open"
android:title="#string/open" />
</menu>
</item>
</menu>
For more informations see https://developer.android.com/guide/topics/ui/menus.html

Related

create a right side menu in Android actionbar

I have an action bar with a drawer on the left. I want to add another menu on the right side of the action bar. It can be three dots or a button or anything else. Is there any way to do this?
Thanks :)
For achieving 3 dots menu in your actionBar, in your activity (which extends AppCompatActivity or ActionBarActivity), you override the creation of options menu like below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
Where this R.menu.your_menuis a resource item present in your res/menu folder. One sample menu resource file content
<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="your_package.your_activity">
<item
android:id="#+id/action_edit"
android:orderInCategory="100"
android:title="#string/action_edit"
app:showAsAction="collapseActionView"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="101"
android:title="#string/action_settings"
app:showAsAction="collapseActionView"/>
</menu>
This will show a menu with both the options collapsed by default. To listen for clicks on these menu items, you override onOptionsItemSelected and perform the necessary action
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//Do something
...
return true;
case R.id.action_edit:
//Do something else
...
return true;
}
return super.onOptionsItemSelected(item);
}
You can create a layout for your ActionBar and then use something like the following in your activity:
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_action_bar);
And in your R.layout.custom_action_bar create the buttons you need.

Can't create "Share" menu in popup menu on Android

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)));
}

Open the settings preferences on clicking the vertical dotted line on the upper-left corner on android app

I want to make a simple settings page, I went to Google's documentation below:
http://developer.android.com/guide/topics/ui/settings.html
created new android testing project, then I created the PreferenceScreen xml, and PreferenceFragment class inside my mainActivity class.
I want the settings screen to appear when I press the settings icon, but instead it only shows one unclickable item named settings.
I know my question is basic, but I tried a lot to make it work with no results.
any help please?
If you want to open the settings from the Menu inside the action bar, as shown below :
First, you need to define a Menu item inside the menu.xml file for that activity :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" android:showAsAction="never" />
</menu>
Second, you need to infate the menu, and then the events for the menu item click can be handles using the methods shown below :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
// write code to handle the click for the settings menu item!
return true;
}
return super.onOptionsItemSelected(item);
}

Android Submenu, how to go back to main menu?

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.

checked context menu items don't stay checked

I'm new to this forum and to android development itself so my question will probably be a very stupid one and i apologize for this .
I began reading the Dev Guide on developer.android.com and until the part with the context menus everything worked pretty fine.
Now i tried to have a context menu with a submenu that contains some checkable items. So i added the submenu and the items to my menu.xml and some item.setchecked(true) methods to my onContextItemSelected(...) method.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/info"
android:title="#string/info" />
<item android:title="#string/change">
<menu>
<item android:id="#+id/checkable_item1"
android:checked="true"
android:checkable="true"
android:title="#string/hello"/>
<item android:id="#+id/checkable_item2"
android:checkable="true"
android:title="#string/moin"/>
<item android:id="#+id/checkable_item3"
android:checkable="true"
android:title="#string/aloha"/>
</menu>
</item>
</menu>
part of my .java file
...
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater1 = getMenuInflater();
inflater1.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.checkable_item1:
if(item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.checkable_item2:
if(item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.checkable_item3:
if(item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
default:
return super.onContextItemSelected(item);
}
}
...
Now the problem is that when i open the menu and press one of the checkable items and i can see that the green tick pops up in the little box just before the context menu closes but when i open up the menu again the tick is gone.
Now i don't really know why the tick doesn't stay in the box.
It would be nice if someone could give me hint and tell me what i'm doing wrong.
thanking you in anticipation
jean-claude91
I haven't tried it myself but if I read the description here properly (http://developer.android.com/reference/android/app/Activity.html#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)),
your layout resource will be called every time the menu is created. Since it's "not safe to hold on to a menu after the method returns" , you would need to process the selected item and persist that selection somewhere and then pass the current state of the selectable items into the onCreate with menuInfo, setting checked/unchecked using that info.
If you don't, then the menu will be recreated every time based on your default settings (menu.xml).

Categories

Resources