I am using ActionBarSherlock in my application and I want to implement popup menu like the below image, having a logo and respective text.
Please help me to achieve this, any help would be appreciable.
Thanks
Here it is.This is actually done in the app which you posted screenshot for.I think you are familiar with ActionbarSherlok.The button for this dropdown menu will be on actionbar.
public boolean onCreateOptionsMenu(Menu menu) {
// Used to put dark icons on light action bar
SubMenu subMenu1 = menu.addSubMenu("");
subMenu1.add("Item1").setIcon(R.drawable.icon).setOnMenuItemClickListener(
new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
});
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.abs__ic_menu_moreoverflow_holo_dark);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
Added PopupMenu in ActionBarSherlock.
Related
I am trying to present a custom action bar while long pressing a textview. My menu has more than 5 items which causes some of the items to be present under the overflow menu.
When I press the overflow icon, the action bar gets destroyed and I am not able to choose any item inside the overflow.
ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.add_rule_menu, menu);
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (!mOptionsList.contains(item.getItemId()))
item.setVisible(false);
}
return false;
}
// Clicking on overflow button does not trigger this method at all.
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
// Rest of the code
}
}
public void onDestroyActionMode(ActionMode mode) {}
};
textView.setCustomSelectionActionModeCallback(mActionModeCallback);
I filed an issue about this years ago, which has never been resolved.
A cheesy workaround is to use nested action modes. By this, I mean you have an item in an action mode that finishes the current mode and starts a new one, to provide a "drill-down menu" effect. I use this in my recently-resuscitated RichEditText widget, which offers an action mode for formatting text. I add a "format" item to the default action mode via setCustomSelectionActionModeCallback(). Tapping on "format" opens another action mode that offers options like bold and italic, along with further drill-downs to get to thinks like font changes.
How to add a menu popup when an action bar item is clicked (see screenshot)? I want the menu item to show an icon.
tHings I have tried:
Setting actionProvider (support lib v7) for the action bar item. In the actionProvider, return null for onCreateActionView. In onPrepareSubMenu, populate the submenu. This works on Android 2.x but not Android 4.0, and for Android 2.x, there is no icon.
In the actionProvider, create a imageview and on clicking, shows a PopupMenu, but popup menu has no icon, when I have specifically used setIcon to show it.
I don't understand why PopupMenu does not show any icon. I followed the "official" code as closely as possible but to no avail.
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/widget/ShareActionProvider.java#195
Please help!
Thanks!
Use popUpMenu ->>> Follow>
res/menu/horario.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_MudaDia"
android:titleCondensed="Mudar Dia"
android:title="Mudar Dia"
android:icon="#drawable/ic_menu_popup"
android:showAsAction="always">
</item>
activity.class
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.horario, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_MudaDia:
View vItem = getActivity().findViewById(R.id.menu_MudaDia);
PopupMenu popMenu = new PopupMenu(getActivity(), vItem);
for (int i = 0; i < diaSemana.length; i++)
{
popMenu.getMenu().add(0, i, i, diaSemana[i]);
}
popMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
DIA = diaSemana[item.getItemId()];
atualizaGUI();
return true;
}
});
popMenu.show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
You can try creating a layout with the ImageView and TextView. Inflate that layout inside a PopUpWindow (refer : http://developer.android.com/reference/android/widget/PopupWindow.html).
Use the showAsDropDown(View actionBarIcon) method to show the menu on your actionbar icon click.
I use support library v7 and works well.
- use ActionProvider
I use custom ActionProvider and it works well at 2.x and 4.x ,
code in onPrepareSubMenu
subMenu.clear();
// labels contain list item text.
int len = labels.length;
for(int i = 0; i < len; i++) {
subMenu.add(0, labels[i], i, labels[i])
.setIcon(icons[i])
.setOnMenuItemClickListener(new MineMenuItemClickListener());
}
super.onPrepareSubMenu(subMenu);
- about PopupMenu
PopupMenu don't show icon as default, but you can do your own PopupMenu and set icon display.
Like this man does CustomPopupMenu
The only modify is add mPopup.setForceShowIcon(true);
So what i want to do, is to have that menu button that we all know, to be displayed in the top right corner.
And as I've searched online, I found that actionbar Sherlock kind of suites my needs.
As you can see from he pic, the code that I use, adds that darker blue part in my design (the one that I ilustrated in a red rectangle using my awesome paint skills), and i want that button that is a green circle, to be placed in the area where the green arrow (nice movie :D) points.
The only code that i used to display this is below, and it is taken from the Sherlock demos:
ActionMode mMode;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMode = startActionMode(new AnActionModeOfEpicProportions());
}
private final class AnActionModeOfEpicProportions implements ActionMode.Callback {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
//Used to put dark icons on light action bar
// boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;
menu.add("Feedback")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add("Share")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add("Preferences")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add("Refresh")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Toast.makeText(AndroidMenu.this, "Got click: " + item, Toast.LENGTH_SHORT).show();
mode.finish();
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
}
I can't seem to figure out where that design comes from (probably the Sherlock framework), and how can i modify it like I want...
Any ideas guys ?
EDIT:
The answer that I found is to use a different Sherlock Sample for my goal :
public class SubMenus extends SherlockActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenu1 = menu.addSubMenu("Action Item");
subMenu1.add("Sample");
subMenu1.add("Menu");
subMenu1.add("Items");
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.ic_title_share_default);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
SubMenu subMenu2 = menu.addSubMenu("Overflow Item");
subMenu2.add("These");
subMenu2.add("Are");
subMenu2.add("Sample");
subMenu2.add("Items");
MenuItem subMenu2Item = subMenu2.getItem();
subMenu2Item.setIcon(R.drawable.ic_compose);
return super.onCreateOptionsMenu(menu);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
((TextView)findViewById(R.id.text)).setText(R.string.submenus_content);
}
The best way to personalize such objects is to set a custom View on them. Perhaps you can inflate a custom layout which provides one TextView and a Button on the right, without the "Done" mark on the left.
This is a snippet of code from this
LayoutInflater inflater = LayoutInflater.from(getSherlockActivity());
View actionView = inflater.inflate(R.layout.actionmode, null);
ActionMode am = getSherlockActivity().startActionMode(mActionModeCallback);
am.setCustomView(actionView);
Is it possible to put a search option on the right of the action bar without having to create a custom action bar ?
Follow this code If you are action bar sherlock, you can implement search menu in action bar
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Search")
.setIcon(R.drawable.search)
.setActionView(R.layout.search_layout)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_COLLAPSE_VIEW);
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//What do i write here?
return true;
}
In search_layout.xml
place a edittext that's enough.
Normal Android Actionbar search widget Implementation
I have a custom menu options that I want to disable it from popping up if a button on screen is clicked..
I thought of using this code but it doesnt work:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
if (Schedule)
menu.getItem(1).setVisible(View.GONE);
return true;
}
Is there a way to prevent the menu button from doing anything? Thanks.
According to the documentation:
You must return true for the menu to be displayed; if you return false it will not be shown.
So I'm guessing this will work:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
.... Code .....
return !Schedule;
}
That is assuming that you want the menu to display when Schedule is equal to false.