Checkbox on left of an item in NavigationView - android

I'm using the design support library's NavigationView, and in the menu it uses I have included the following:
<item android:title="Check The CheckBox"
app:actionViewClass="android.support.v7.widget.AppCompatCheckBox"/>
Which solved half my problem as now I had a checkbox, however I wished to to have the CheckBox appear where usually the icon is placed, much like how it is done in the Google Calendar app with the list of calendars

Menu items have a checkable attribute.
<item android:title="Check The CheckBox"
android:checkable="true"/>
You can also <group> several checkable items together.
<group android:checkableBehavior="all">
<item android:title="Check The CheckBox"/>
</group>

In order to have your CheckBox appear on the left of your menu items your best bet is to set the icon of your menu items as a checked or unchecked image (depending on the checked state of your menu items). I've just had a look at the Google Calendar app and this is what it appears to be doing. Also, the Android Menus documentation alludes to this:
Note: Menu items in the Icon Menu (from the options menu) cannot
display a checkbox or radio button. If you choose to make items in the
Icon Menu checkable, you must manually indicate the checked state by
swapping the icon and/or text each time the state changes.

Related

How to change Option Menu Item layout?

How to customize the Option Menu item layout from xml or programmatically.
I found many question but no good answer yet.
How to change Option Menu Style
How to change layout by clicking items of menu?
How to change Menu Item Color & Size programmatically?
<item
android:id="#+id/itemId"
android:title="Item title"
app:showAsAction="always"
app:actionLayout="#layout/menu_item_layout" />
Note that this layout will be shown only in the ActionBar. If the item is in the overflow menu only title will be shown.

ActionBarSherlock collapseActionView

I'm having a problem when I try to set one item in my actionbar as always visible and 4 more icons as dropdown items with the following layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search_movies"
android:icon="#drawable/action_search"
android:showAsAction="always"
android:title="Search"/>
<item
android:id="#+id/movies"
android:icon="#drawable/action_video"
android:showAsAction="collapseActionView"
android:title="Movies"/>
<item
android:id="#+id/theaters"
android:icon="#drawable/action_location_map"
android:showAsAction="collapseActionView"
android:title="Theaters"/>
<item
android:id="#+id/preferences"
android:icon="#drawable/action_settings"
android:showAsAction="collapseActionView"
android:title="Preferences"/>
<item
android:id="#+id/contact"
android:icon="#drawable/action_about"
android:showAsAction="collapseActionView"
android:title="Contact"/>
</menu>
The result is just the first item showing and the rest are not visible, not even as a dropdown. This is using ActionBarSherlock and a 2.3 Android device.
The question is, how can I get the icons to follow this layout:
EDIT:
The problem I had was because when you are using the actionbar with a device that has a "menu" hardware button the 3-dot dropdown does not shows off, the 4 other items are only displayed if you press the menu hardware button. Does anyone knows if this behaviour can be modified?
Hmmm, maybe I misunderstood, but if you wish to places those remaining four items into the overflow action menu (the 3-dot icon) then using android:showAsAction="never" instead of "collapseActionView" should do it.
...Tried a couple ways, but this did the trick:
Force overflow menu in ABS
I've met the same problem and my solution is quite simple. (I didn't use HoloEverywhere.)
The idea comes from the ABS sample project, whose drop-down menu can be displayed on pre-4.0 devices as well by using a submenu. So, my idea is using a submenu to disguise the 3-dot icon. Here's the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu sub = menu.addSubMenu("More");
sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
sub.getItem().setIcon(R.drawable.ic_menu);
getSupportMenuInflater().inflate(R.menu.activity_main, sub);
return true;
}
Since the "More" menu doesn't have a MenuItem.SHOW_AS_ACTION_WITH_TEXT attribute, so the word "More"(or whatever you named) will actually not be displayed on the action bar. The only displayed icon R.drawable.ic_menu can be copied from ABS source code res/drawable-xxdpi folders named "abs__ic_menu_moreoverflow_normal_holo_dark.png", which is the so-called 3-dot icon. And the R.menu.activity_main is your menu xml.
It works!

Add dropdown on custom action bar view at left, with checkable items

I have a lot of problems with the development of an action bar. Here are my needs:
I want to add a dropdown in my action bar, which is using custom view because of some custom comportment (animation for example).
The dropdown have to be accessible from an image button (not a spinner which show a text area of the selected element).
The image button that allow to show the dropdown have to be at the left of the action bar.
The dropdown that appear have to contain checkable items, and, when the user check an item, the dropdown have to not be hidden (this allow the user to check multiple items at once).
My application have to work from android 2.3 (I'm using action bar sherlock for that, I specify this because of I can't use PopupMenu on android 2.3).
I have successfully added a dropdown in my action bar by combining my custom view (actionBar.setCustomView(...)) with a menu (menuInflater.inflate(...)). I think the code of my customview is not very important, here is the code of my menu :
<item
android:title="test"
android:drawable="#drawable/dropdown_icon"
android:showAsAction="always">
<menu>
<item
android:id="#+id/item1"
android:title="item 1"
android:checkable="true"/>
<item
android:id="#+id/item2"
android:title="item 2"
android:checkable="true"/>
<item
android:id="#+id/item3"
android:title="item 3"
android:checkable="true"/>
</menu>
</item>
With this solution, I have the following problems :
I'm not able to put the dropdown item at left (it's at the right for the moment)
When I check an item on the dropdown list, my dropdown hide itself. I want that the dropdown keep its showing state.
How can I achieve what I want ?
P.S : As you can read, English is not my native language. These problems are difficult to explain (even in my native language), so if some sentences are not understandable, please tell me and I will try to improve them.
I suggest doing the simplest thing possible: remove all the ActionBarSherlock related code and add your own views in layout and program them to animate, pop-up, show and hide.

Contextual Action Bar menu forces items into overflow menu when there is room

I have a contextual action bar with three menu items, so only three icons. There is definitely room on the cab, but only one icon shows and the other two are forced into the oveflow menu. If I use android:showAsAction="always" in all three menu items then they all show and there is room. However, I would rather use android:showAsAction="ifRoom" with the result that all three icons show. Have tried deleting android:title on each item, but that still resulted in the oveflow menu, so I know it's not trying to put the title in.
<item android:id="#+id/delete_quiz"
android:title="DeleteQuiz"
android:icon="#drawable/ic_menu_close_clear_cancel"
android:showAsAction="ifRoom"
/>
<item android:id="#+id/load_quiz"
android:title="LoadQuiz"
android:icon="#drawable/ic_menu_close_clear_cancel"
android:showAsAction="ifRoom"
/>
<item android:id="#+id/start_quiz"
android:title="StartQuiz"
android:icon="#drawable/ic_menu_close_clear_cancel"
android:showAsAction="ifRoom"
/>
The icon I'm using is ic_menu_close_clear_cancel for each menu item.
Has anyone had a similar experience? What did you do? Thank you in advance.
Try using the layoutWeight attribute. layoutWeight will NOT work with Relative Layout.
set the value of layoutWeight between 0 to 1
0 - Greedy (Will take the full space)
1 - Generous (Will take only as much as needed)
Try to fiddle between 0 to 1, and u will hit the spot, try 0.25, or 0.5 for all of them.

Is there a standard way to add dividers between action bar items in Android 3.0?

I have a slight problem trying to customise the look of the action bar in my app. I want to be able to have the pixel wide dividers to group action bar items that you see in many of the native apps (e.g. Gmail, Calendar). I found a way to do this by adding a menu item and setting the 'android:actionLayout' attribute to a custom layout for the divider:
<View
android:background="#color/LightGray"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:layout_width="1dip"
android:layout_height="fill_parent" />
This works nicely, but the issue is it counts as a menu item and the action bar seems to limit the number of menu items to 4 - any others get pushed into the overflow menu.
So I guess what I'm asking is whether there is a standard way to add item dividers without having to use a menu item with a custom view, and in a way that doesn't count towards the limit for action bar items?
Thanks in advance!
I wouldn't try and force dividers into places that the system does not add them automatically as it will make your app inconsistent with the platform. The default behavior is:
Divider between overflow and others.
Divider between text and another item where it would disambiguate which item the text belongs to.
I couldn't find a standard way, but the way I did it was to use the android:actionLayout property for the menu item, and I put the divider in there.
When Google released the 3.0 SDK I got a quick demo app to see how the ActionBar works and just looking back at it, if I use Text Items without Icon drawables, then I get automatic dividers drawn.
My menu.xml file is like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_text" android:showAsAction="ifRoom" android:title="#string/action_label_text" />
<item android:id="#+id/menu_text" android:showAsAction="ifRoom" android:title="#string/action_label_text" />
<item android:id="#+id/menu_text" android:showAsAction="ifRoom" android:title="#string/action_label_text" />
<item android:id="#+id/menu_text" android:showAsAction="ifRoom" android:title="#string/action_label_text" />
</menu>
Maybe this won't work with icons??
Or thinking about it, maybe the size of the icon has an effect?

Categories

Resources