what I'm looking for is to make an options menu but without the ActionBar. In the Google music app I saw that they have a options menu sort of thing with no action bar. Below is a picture of what I was talking about in the Google music app.
Thank you in advance! :)
That's just a simple popop. You can do that on any view. Throw an icon on a view, like the overflow menu icone and set a click listener on it.
This example is a list of devices (smartphones) in a catalog. I populate the tag with an object so I know which one the user clicks on.
public void showDeviceMenu(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.inflate(R.menu.cart_device_menu);
DeviceTag tag = (DeviceTag) v.getTag();
final String groupId = tag.groupId;
final String sku = tag.sku;
final String productId = tag.productId;
SpannableStringBuilder text = new SpannableStringBuilder(tag.name);
text.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
popup.getMenu().findItem(R.id.menu_name).setTitle(text);
invalidateOptionsMenu();
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.duplicate_device:
duplicateDevice(sku, productId);
return true;
case R.id.update_device:
updateWirelessItemInCart(sku,groupId);
return true;
case R.id.delete_device:
removeItemFromCart(groupId);
return true;
default:
return false;
}
}
});
popup.show();
}
Related
I have a bottom navigation, I check the selected item on its selection, and this prevent it from re selecting the same item again.
So if I am on the home screen and I click home screen navigation item again, it does not do anything.
I have a case where I need to make home navigation item selectable even if its already selected.
This is my code that I run in onCreate method.
public void updateNavigationBarState() {
int actionId = getNavigationMenuItemId();
selectBottomNavigationBarItem(actionId);
}
void selectBottomNavigationBarItem(int itemId) {
Menu menu = bottomNavigationView.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem item = menu.getItem(i);
boolean shouldBeChecked = item.getItemId() == itemId;
if (shouldBeChecked) {
item.setChecked(true);
item.setEnabled(false);
break;
}
}
}
Is there any way to make item clickable even if its selected.
Use setNavigationItemSelectedListener.
Like below
navigationView.setNavigationItemSelectedListener(menuItem -> {
switch (menuItem.getItemId()) {
case R.id.menuItemId:
// do your stuff here
return true;
default:
return true;
}
});
I have a problem with changing of popupmenu title.
My goal is that there is a join menu in popupmenu list.
After user join the app by using the popupmenu button , I want to change the "join" title to "User profile".
But I don't know how to change the title of popupmenu.
If there has a solution, let me know how to change.
Here is the code
<item android:id="#+id/menu6"
android:title="join"/>
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_menu://popupbutton
PopupMenu popup = new PopupMenu(getApplicationContext(), v);
getMenuInflater().inflate(R.menu.main_menu, popup.getMenu());
popup.setOnMenuItemClickListener(popupClick);
popup.show();
}
PopupMenu.OnMenuItemClickListener popupClick = new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuitem) {
switch (menuitem.getItemId()) {
case R.id.menu6: // here is a code of join
break;
}
You can follow this answer
Change PopupMenu items' title programatically
.
First create a boolean variable
private boolean menu6;
Create a menu object to check which popup item is clicked
Menu menuOpts = popup.getMenu();
if (menu6) {
menuOpts.getItem(1).setTitle("User profile");
}
Modify onMenuItemClick to this
switch (menuitem.getItemId()) {
case R.id.menu6: // here is a code of join
menu6 = true
break;
}
I have an imageButton in a xml file. Now want to make it a menu button, so that when a user click on the button it should show the drop down menus. But I am not able to figure out what are the possible solution(s).
Can anyone help?
If you're trying to show a drop down menu when clicking an ImageButton (or any other View), try this:
final ImageButton imageButton = // get your ImageButton from the XML here
final PopupMenu dropDownMenu = new PopupMenu(getContext(), imageButton);
final Menu menu = dropDownMenu.getMenu();
// add your items:
menu.add(0, 0, 0, "An item");
menu.add(0, 1, 0, "Another item");
// OR inflate your menu from an XML:
dropDownMenu.getMenuInflater().inflate(R.menu.some_menu, menu);
dropDownMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case 0:
// item ID 0 was clicked
return true;
case 1:
// item ID 1 was clicked
return true;
}
return false;
}
});
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dropDownMenu.show();
}
});
// if you want to be able to open the menu by dragging on the button:
imageButton.setOnTouchListener(dropDownMenu.getDragToOpenListener());
When Android Studio asks to import PopupMenu you may see two options:
android.support.v7.widget.PopupMenu this is the best option, it ensures your menu will work in any Android version
android.widget.PopupMenu this one only works on Android 2.1 and up, which is probably fine. However, if new Android versions come with new features in PopupMenu, the first option may also let you use those new features in older Android versions.
I want to open the menu, the problem is my game uses full screen so there is no toolbar, no room for a FAB and no menu button. I have tried to use
openOptionsMenu();
in an onClickListener from a button on the screen but it does nothing. If you have any suggestions please reply here.
The answer I found was to use the PopupMenu, see the example and comments below.
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
public void onClick(View view) {
switch (view.getId()) {
case R.id.new_game_button:
//start new game
break;
case R.id.menu_button:
/** Instantiating PopupMenu class */
PopupMenu popup = new PopupMenu(getBaseContext(), view);
/** Adding menu items to the popumenu */
popup.getMenuInflater().inflate(R.menu.game_menu, popup.getMenu());
/** Defining menu item click listener for the popup menu */
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_more_apps:
//do something
return true;
case R.id.menu_about:
// do something
return true;
case R.id.menu_like_us:
//do something
return true;
break;
I have an actionBar with multiple items, I would like to change the colour of the text when the item is clicked. Is there anyway to do this programmatically? Please provide and example or any resources.
Thanks
public void catalogClick(MenuItem item){
//highlight menuitem etc.
}
To change without defining a style resource, we can use SpannableString.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
//To style first menu item
MenuItem menuItem = menu.getItem(0);
CharSequence menuTitle = menuItem.getTitle();
SpannableString styledMenuTitle = new SpannableString(menuTitle);
styledMenuTitle.setSpan(new ForegroundColorSpan(Color.parseColor("#00FFBB")), 0, menuTitle.length(), 0);
menuItem.setTitle(styledMenuTitle);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
Toast.makeText(this, item.getTitle() + " clicked!", Toast.LENGTH_LONG).show();
return true;
}
As you format the text style, you will get "Invalid payload item type" exception. To avoid that, override onMenuItemSelected, and use return true or false.
Reference:
Android: java.lang.IllegalArgumentException: Invalid payload item type
http://vardhan-justlikethat.blogspot.in/2013/02/solution-invalid-payload-item-type.html
Follow this link which explains how to change menuitem text programmatically.
http://developer.android.com/guide/topics/ui/actionbar.html#Style
Check for android:actionMenuTextColor for defining a style resource for text.
Try Firewall_Sudhan answer but iterating the submenu of the menu
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
SubMenu subMenu = menu.getItem(0).getSubMenu();
for (int i = 0; i < subMenu.size(); i++) {
MenuItem menuItem = subMenu.getItem(i);
CharSequence menuTitle = menuItem.getTitle();
SpannableString styledMenuTitle = new SpannableString(menuTitle);
styledMenuTitle.setSpan(new ForegroundColorSpan(Color.BLACK), 0, menuTitle.length(), 0);
menuItem.setTitle(styledMenuTitle);
}
}