I have added some checkboxs in my overflow menu. I want my overflow menu to hold rather than disappear once I click the checkbox in the overflow menu. How can I do that? Thanks for help.
This is my menu xml file.
<item
android:id="#+id/action_check"
android:title="#string/action_check"
android:orderInCategory="1"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item android:id="#+id/notification"
android:orderInCategory="2"
android:title="#string/notification"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item android:id="#+id/about"
android:orderInCategory="3"
android:title="#string/about"
app:showAsAction="never"></item>
This is how i did it.
Add following code in your activity where option menu is implemented.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//your checking other stuff
item.setChecked(!item.isChecked());
//main part for holding onto the menu
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(this));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
return false;
}
By adding the line: item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); I am marking the item as it has expandable/collapsible behavior so it will call the setOnActionExpandListener.
This line here: item.setActionView(new View(this)); is a view when item is in expanded state.It is just a dummy view because we will never let it expand how i am going to explain next.
You see that i am returning false from both the methods of setOnActionExpandListener to suppress expansion and collapsing of the item so the view we gave in previous step would never show up and menu would remain open.
Following would be your menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="all">
<item
android:id="#+id/action_check"
android:orderInCategory="1"
android:title="Title 1"
app:showAsAction="never" />
<item
android:id="#+id/notification"
android:orderInCategory="2"
android:title="Title 2"
app:showAsAction="never" />
<item
android:id="#+id/about"
android:orderInCategory="3"
android:title="Title 3"
app:showAsAction="never" />
</group>
</menu>
Notice the line group android:checkableBehavior="all" is to tell that all items in the group will have checkable behavior so that you don't have to write checkable in each item.
group is a collection of menu items that shares certain traits, for more information see
<group android:id="#+id/group>
<item
android:id="#+id/action_check"
android:title="#string/follow"
android:orderInCategory="1"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item android:id="#+id/notification"
android:orderInCategory="2"
android:title="#string/notification"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item android:id="#+id/about"
android:orderInCategory="3"
android:title="#string/about_us"
app:showAsAction="never"/>
</group>
Related
I am trying to make a favourite item in the menu that can display a hollow heart when unchecked and a solid one when checked
However the selector approach is not working, I managed to make it work in code with a boolean value that is toggled and a item.setIcon() method. But I want to understand why my selector approach didn't work.
here's what I tried:
I tried to make a checkable attribute in the item xml with no luck
also tried to set the setChecked directly from code checked the debugger the item.isChecked() is returning true as intended but the icon doesn't change
tried to set the checked state from inside the xml to true to see if it will change icon still didn't work
The code is showing that the checking state is changing as intended but the selector is not responding by changing the icon accordingly the Question is why is that so and how to solve it.
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">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_contact"
android:title="#string/action_contact"
android:orderInCategory="10"
app:showAsAction="never" />
<item
android:id="#+id/action_order"
android:icon="#drawable/ic_shopping_cart"
android:title="#string/action_order"
app:showAsAction="always" />
<item
android:id="#+id/action_status"
android:title="#string/action_status"
android:icon="#drawable/ic_status_info"
app:showAsAction="always" />
<item
android:id="#+id/action_favourites"
android:title="#string/action_favourites"
android:icon="#drawable/favourite_toggle_image"
android:checkable="true"
app:showAsAction="always" />
</menu>
favourite_toggle_image.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/ic_favourite_info"
android:state_checked="true" />
<item
android:drawable="#drawable/ic_favourite_unchecked_info"
android:state_checked="false" />
</selector>
MainActivity.java
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.action_order:
displayToast("Order");
break;
case R.id.action_status:
displayToast("Status");
break;
case R.id.action_favourites:
lastState = !lastState;
//if (lastState) item.setIcon(R.drawable.ic_favourite_info);
//else if (!lastState) item.setIcon(R.drawable.ic_favourite_unchecked_info);
item.setChecked(lastState);
displayToast("Favourites");
break;
case R.id.action_contact:
displayToast("Contact");
break;
case R.id.action_settings:
displayToast("Settings");
break;
}
return super.onOptionsItemSelected(item);
}
I have a menu which is shown in a NavigationView (Side navigation).
The menu has this structure in which menu tags not only have their own children item tags, but they(the menu tags) are also enclosed within parent item tags.
Example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<item android:title="User Profile">
<menu>
<item
android:id="#+id/profile"
android:icon="#drawable/ic_user"
android:title="Profile" />
<item
android:id="#+id/account"
android:icon="#android:drawable/ic_secure"
android:title="Account" />
</menu>
</item>
<item android:title="Ride Now">
<menu>
<item
android:id="#+id/find_ride"
android:icon="#drawable/car_1"
android:title="Find A Ride" />
<item
android:id="#+id/offer_ride"
android:icon="#drawable/car_1"
android:title="Offer A Ride" />
</menu>
</item>
<item android:title="Booking History">
<menu>
<item
android:id="#+id/offered_rides"
android:icon="#drawable/ic_menu_share"
android:title="Rides offered"
/>
<item
android:id="#+id/booked_rides"
android:icon="#drawable/ic_menu_send"
android:title="Rides Booked" />
<item
android:id="#+id/cancelled_rides"
android:icon="#drawable/ic_menu_send"
android:title="Rides Cancelled" />
</menu>
</item>
</menu>
I have been able to set the item title colors within the menu tags using:
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/app_dark_green_scheme"
app:itemTextColor="#color/app_white_scheme"
app:itemIconTint="#color/app_white_scheme"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
but I need to set the title colors for items that are parents of the menu tags. How do I accomplish this?
Note: #+id/nav_view is the NavigationView that owns the menu
Please, I would prefer the answer to be xml based, instead of Java based
You need add android:textColorSecondary into your style.xml
paste the following code into the style you are using as the theme. You can change the color as you like.
<item name="android:textColorSecondary">#color/colorPrimary</item>
it works for me
Since you don't want to change the color of all items you have to do it by code:
public SpannableString getColoredSpannableString(String s, int color) {
SpannableString str = new SpannableString(s);
str.setSpan(new ForegroundColorSpan(color), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return str;
}
public void makeMenuItemColored(MenuItem mi, int color) {
mi.setTitle(getColoredSpannableString(mi.getTitle().toString(), color));
}
and use it like this:
makeMenuItemColored(mi, ContextCompat.getColor(this, R.color.colorPrimary));
or makeMenuItemColored(mi, yourColor);
where mi is your menu item and you can choose a color resource or a color by int
I like to show alert like this:
And when I click the first settings icon, show an alert like this one:
To achive that you have to group the subitems and embed in a one menu item.
Here is an example you can refer.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/id_group1"
android:icon="#drawable/v3_actionbar_setting"
android:orderInCategory="120"
android:showAsAction="always"
android:title="#string/action_settings">
<menu>
<item
android:id="#+id/subitem1"
android:icon="#drawable/actionbar_settings"
android:title="subitem 1"/>
<item
android:id="#+id/subitem2
android:icon="#drawable/image2"
android:title="subitem 2">
</item>
</menu>
</item>
<item
android:id="#+id/group2"
android:icon="#drawable/group2"
android:orderInCategory="90"
android:showAsAction="always"
android:title="group 2">
<menu>
<item
android:id="#+id/subitem21"
android:icon="#drawable/subitem1"
android:title="subitem1"/>
<item
android:id="#+id/subitem22"
android:icon="#drawable/sub2"
android:title="sub2"/>
</menu>
</item>
Hope this helps :)
Take a good look at this: Android Menus
You'll possibly find all you need.
I put an ActionBar into my app and changed the background color, as I'll show below. Oddly enough, the color behind the text on the ActionBar won't change. (picture)
I looked around and found this:
<item name="android:actionBarItemBackground">#null</item>
But I would prefer not to use it because then my minSdk has to be set to 14, whereas I have it at 11 now. I did try boosting it up to 14 to see if this tag would work, but I again couldn't get the color to change.
Does anyone have any experience with this? Many thanks in advance for your help.
Styles.xml:
<style name="AppBaseTheme" parent="Theme.AppCompat">
</style>
<!-- Application theme. -->
<style name="MyAppTheme" parent="AppBaseTheme">
<item name="android:colorBackground">#color/background</item>
<item name="android:background">#color/background</item>
<item name="android:textColor">#color/myWhite</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#drawable/actionbar_background</item>
<item name="actionMenuTextColor">#color/myWhite</item>
<!--Support Library compatibility-->
<item name="background">#color/myBlack</item>
</style>
drawable/actionbar_background:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/myBlack" />
</shape>
menu/login.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/settings_label"
android:showAsAction="never" />
</menu>
And finally, inside the LoginActivity class, the onCreateOptionsMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return super.onCreateOptionsMenu(menu);
}
Thanks again!
I figured it out. Turns out I was using the wrong attribute -- instead of using android:background in the styles.xml file, you need to use android:windowBackground. android:background changes all sorts of other things, too.
There is the folliwing problem: I've made some Android app and there are 3 menu items (1 dropdown and 2 simple items):
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:icon="#android:drawable/ic_menu_sort_by_size"
android:showAsAction="ifRoom"
android:orderInCategory="100"
android:title="СSort">
<menu>
<item
android:id="#+id/girls_action_all"
android:title="All"/>
<item
android:id="#+id/girls_action_best"
android:title="Best"/>
<item
android:id="#+id/girls_action_new_people"
android:title="New users today"/>
<item
android:id="#+id/girls_action_50_plus"
android:title="Top 50"/>
<item
android:id="#+id/girls_action_100_plus"
android:title="Top 100"/>
</menu>
</item>
<item
android:id="#+id/girls_action_home"
android:icon="#drawable/ic_menu_home"
android:orderInCategory="101"
android:showAsAction="ifRoom"
android:title="My page"/>
<item
android:id="#+id/girls_action_search"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="102"
android:showAsAction="ifRoom"
android:title="Search"/>
</menu>
But one item ("Search") doesn't appear on the Action Bar if I use some devices; it appears in the Menu (after click by "Menu" button). I don't like it. Is there any way to fix it?
Try to implement
<item
android:id="#+id/girls_action_search"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="102"
android:showAsAction="always"
android:title="Search"/>
Change android:showAsAction="ifRoom" to android:showAsAction="always" for the search menu item.
android:showAsAction="ifRoom" will do as it is told - it will display the menu in the action bar if there is enough room; if not, it will overflow to the menu. Currently, all your menu items have this flag set and will compete for the limited space.
android:showAsAction="always" will force the item to be displayed in the action bar. This may cause them to overlap with other UI items if there is not enough room.
Or set android:showAsAction="never" for some of the other items that you don't want in the action bar.