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.
Related
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>
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'm making an android app, and I'm using Spinners in Dialog Mode, defined as follows :
<Spinner android:id="#+id/new_order_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/SpinnerTheme"
style="#style/UnderlinedSpinner"
android:layout_below="#+id/new_order_hint_address"
android:prompt="#string/myString"
android:spinnerMode="dialog"
android:layout_marginBottom="5dp" />
where the styles are
<style name="SpinnerTheme">
<item name="colorControlNormal"> #color/colorPrimaryDark </item>
<item name="android:textColorSecondary"> #color/colorPrimaryDark </item>
<item name="android:colorControlHighlight"> #color/colorPrimaryDark </item>
<item name="android:colorControlActivated"> #color/colorPrimaryDark </item>
<item name="android:minHeight"> 35dp </item>
<item name="android:showDividers"> middle </item>
<item name="android:divider"> #color/colorAccent </item>
<item name="android:dividerHeight"> 0.5dp </item>
</style>
<style name="SpinnerLabel" parent="TextAppearance.Design.Hint">
<item name="android:paddingLeft">#dimen/input_label_horizontal_spacing</item>
<item name="android:paddingRight">#dimen/input_label_horizontal_spacing</item>
<item name="android:textSize"> 14sp </item>
<item name="android:textColor">#color/hintText</item>
</style>
<style name="UnderlinedSpinner" parent="Base.Widget.AppCompat.Spinner.Underlined"/>
In API <23, everything works fine, but in API 23, the prompt is not shown. I've tried setting in from the java code, I've read all the similar questions and answers and have had no luck whatsoever.
Thanks in advance
Ok, after a lot more of trial and error, I found out that the issue was in fact that the color of the prompt text was the same as the color of the background of the prompt (both white).
The reason behind it relays on the "textColorPrimary", which I'm not setting in the style of the spinner, but I did set in the style of the Tabhost where it is located. It looks like it inherited this property.
Therefore, the solution is just adding that parameter to the style, with the color desired.
The only thing I'm not sure is why this only happens in APIs > 23, and not the others.
OK, when I use setBackground on a button, the button no longer shows any feedback when its clicked. How can I fix this. I just want the button to get darker or some type of feedback so the user knows it was clicked.
Hope this makes sense!
Create this drawable and bind it to button background
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/YOURIMAGE" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:drawable="#drawable/YOURIMAGE" />
</selector>
Apply it to button
android:background="#drawable/button"
You need to create a selector inside the XML and have it linked to state_pressed = "true". Inside of the item, you can specify the shape of the object. I've linked to the Android developer site so you can see other options available as well.
<item android:state_pressed="true">
<shape>
<solid android:color="#73E5E4D7" />
</shape>
</item>
<item>
<shape>
<solid android:color="#E6E5E4D7" />
</shape>
</item>
Android Developer State List
hope this works well. first assign your button into a button type reference in your activity class.
ex: newBtn=(Button) findViewById(R.id.yourButton);
after that set an listener to that button.
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
yourButton.setBackgroundColor(int colorCode);
}
});
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.