android fragment activity menu - android

I have a class that extends FragmentActivity. No I want to show my menu items at the top of the screen like in actionbar. How can I do this ?
I have created the menu items and they are visible hen the hardware button for menu is pressesd but I want to sho them at the top of the screen.

Basically, this happens in devices with physical menu button. In devices without that button, all options are displayed in the action bar.
You can force a menu item to be displayed in the action bar by addind "showAlways" to the item that you want.
showAsAction=“always”
An small example
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="always" />
</menu>
You must need to ensure that you have enough space for the menu items.
To ensure that I'll have enough space, I use to create a menu with only one item. Then, inside this item, I create another menu. This way, only one button is displayed and all items appear in a floating pop up menu. The main idea is below:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/overflow_item"
android:icon="#drawable/ic_overflow_icon"
android:title="#string/options"
android:showAsAction="always" />
<menu>
<item android:id="#+id/action_clear"
android:title="#string/clear"
android:showAsAction="always" />
</menu>
</menu>

Related

Popup Dialog as Menu when Menu is clicked

I want to implement Dialog where I will be able to set values.
I also would want to Dialog will Popup when I click on three dots (Menu) on Toolbar.
So far I've got menu
<?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/menu_allclients_min_date"
android:orderInCategory="200"
android:title="#string/menu_min_date"
app:showAsAction="never" />
<item
android:id="#+id/menu_allclients_max_date"
android:orderInCategory="201"
android:title="#string/menu_max_date"
app:showAsAction="never" />
<item
android:id="#+id/menu_allclients_min_amount"
android:orderInCategory="202"
android:title="#string/menu_min_amount"
app:showAsAction="never" />
<item
android:id="#+id/menu_allclients_max_amount"
android:orderInCategory="203"
android:title="#string/menu_max_amount"
app:showAsAction="never" />
Now, when Menu is clicked it show as list of four items. But It isn't a way I want to implement it.
I would also know is there any listener when Menu icon( three dots ) is clicked. It would solve my problem.
For help i sugest, if you dont have more than 2 menu itens, make a menu item set the 3 dots as icon and always as showasaction. After that in on menu item selected in your activity make a switch to check item selected and if is the 3 dots item make the dialog.

What is orderInCategory in ActionBar menu item & why it is use for..?

Im working on action menu item and its over flow item this is my main_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/menu_search"
android:icon="#drawable/search"
android:title="#string/start_new_project"
app:showAsAction="always" />
<item
android:id="#+id/menu_dts_overflow"
android:icon="#drawable/ic_action_overflow_round"
android:orderInCategory="11111"
android:title="Service"
app:showAsAction="always">
<menu>
<item
android:id="#+id/menu_newProject"
android:icon="#drawable/newproject"
android:title="#string/start_new_project"
app:showAsAction="never" />
<item
android:id="#+id/menu_help"
android:icon="#drawable/help"
android:title="Service Tasks"
app:showAsAction="never" />
<item
android:id="#+id/menu_signOut"
android:icon="#drawable/signout"
android:title="#string/menusignout"
app:showAsAction="never" />
</menu>
</item>
I tried to construct a search item and a overflow item which you can see in the above code. I'm new to Action bar menu items so i tried to Google it and was able to make it work as I need.
In this I have to know one more thing.
1. What is orderInCategory with some numbers and what for it is used..?
android:orderInCategory is an integer attribute that dictates the order in which the menu items will appear within the menu when it is displayed.
<menu
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item_first"
android:orderInCategory="1"
android:showAsAction="never"
android:title="#string/string_one"/>
<item
android:id="#+id/menu_item_second"
android:orderInCategory="2"
android:showAsAction="never"
android:title="#string/string_two"/>
</menu>
Menu items in ToolBar are arranged from left to right (or
start to end in RTL mode) in the ascending order (i.e. 1,2,3 ->
left to right).
Menu Items in Overflow menu are arranged from top to bottom in
the ascending order (i.e. 1,2,3 -> top to bottom).
android:orderInCategory is actually useful in two ways.
1. For menu items in ActionBar.
Items will appear from left to right in ActionBar depending on the ascending order.
2. For menu items in overflow menu.
Overflow menu items will be displayed from top to bottom depending upon the ascending order you have specified.
android:orderInCategory Higher value, lower priority.
I have a Activity and a Fragment in it, both of them have option menu, and the item numbers are 1 and 3.
If I set android:orderInCategory=0, the activity menu are above the fragment menu, same effect before I set the value.
But if I set android:orderInCategory=1,the activity menu are below the fragment menu, and that is what I want.(I also test android:orderInCategory=5 tested too, still the same effect.)

Creating a popup options menu in a contextual action bar

I have an app with a NoActionBar theme. In my main activity I have an options menu that I created manually on the top of the screen (or by utilizing the built in device's options button).
In this main activity, I have a fragment with a listView where I apply the action mode long click functionality, to show the contextual action bar (CAB) for further user options.
Now, I try adding an options item to my CAB so it will contain some options like selecting all items in the listView, but since it's an item of the CAB, I can't really show the popup menu like in a regular activity. Further more, I want all the options menu callbacks (such as onOptionsItemSelected) to stay in the context of the CAB, in order to be able to continue to perform actions on the CAB.
Here's the code of my CAB:
<?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_delete"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/ic_action_delete"
android:title="Delete"/>
<item
android:id="#+id/action_overflow"
app:showAsAction="always"
android:orderInCategory="200"
android:icon="#drawable/ic_action_overflow"
android:title="Options"
android:visible="false"/>
</menu>
Apparently I missed the built in feature of the CAB - a built in overflow menu that collapses some of the actions' items once the screen is too small to show them all.
Another manipulation that needs to be done in order to always collapse certain actions under that overflow menu is to set for each one of them:
android:showAsAction="never"
app:showAsAction="never"
So, say we have 3 actions (delete, selece_all, add) in the CAB, and we want two of them (select_all, add) to be collapsed always under the built in overflow menu, we'll set this in the CAB's xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mm="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_delete"
android:orderInCategory="100"
mm:showAsAction="always"
android:icon="#drawable/ic_action_delete"
android:title="Delete"/>
<item
android:id="#+id/action_select"
android:orderInCategory="200"
android:showAsAction="never"
mm:showAsAction="never"
android:title="#string/select_all"/>
<item
android:id="#+id/action_add"
android:orderInCategory="300"
android:showAsAction="never"
mm:showAsAction="never"
android:title="#string/button_add"/>

Hide, unhide ActionBar icons with Search Widget

A common pattern I see in many apps that provide some search functionality is to have the search widget clear the other menu icons off of the ActionBar. This works for me right now just by using ifRoom for showAsAction in my menu items. I'd like to show more menu items in the actionbar though, because it seems that the ActionBar is very conservative with how it determines ifRoom and consequently at least one important menu item is pushed to the overflow (perhaps someone smarter than me knows why they do that).
So how can I set showAsAction as always while still clearing the menu items off of the ActionBar when the Search Widget is activated? When I try that now, the menu items don't move, and the search widget is condensed.
Here's my menu:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="collapseActionView|always"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/action_stores"
android:title="#string/action_stores"
android:icon="#drawable/ic_action_stores"
android:showAsAction="always" />
<item
android:id="#+id/action_shopping_lists"
android:title="#string/action_shopping_lists"
android:icon="#drawable/ic_action_lists"
android:showAsAction="always" />
<item
android:id="#+id/action_preferences"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
My searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
Couldn't you set an onClickListener on the search widget that then runs some few lines of code to make the other MenuItems change from 'always' to 'ifRoom'?
The few lines of code within the onClickListener would contain something like this:
myMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
whereas myMenuItem is your MenuItem that should go from 'always' to 'ifRoom'.
You can change it back later in an appropriate method.

Android menu item not showing drawable

Im using the ActionBarActivity on my activity and inflating the following menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:microecs="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/description_refresh"
android:orderInCategory="1"
microecs:showAsAction="ifRoom" />
<!--TODO: get a good logout icon-->
<item
android:id="#+id/menu_logoff"
android:icon="#drawable/ic_action_logout"
android:title="#string/description_logoff"
android:orderInCategory="2"
microecs:showAsAction="never" />
</menu>
The second menu item drawable that i want to appear as a menu and not in the action bar doesn't not appear in my S4 (running 4.3) and also on lower devices running 2.3.7
Just update your code with following code.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:microecs="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/description_refresh"
android:orderInCategory="1"
microecs:showAsAction="ifRoom" />
<!--TODO: get a good logout icon-->
<item
android:id="#+id/menu_logoff"
android:icon="#drawable/ic_action_logout"
android:title="#string/description_logoff"
android:orderInCategory="2"
microecs:showAsAction="ifRoom" />
</menu>
Whenever you are using never then it dont place that item in the Action Bar.
For about menu refer this link.
This is not possible by design. Overflow menus from the action bar don't show icons - that's just a design decision Google made.
You can check more, e.g. here: Displaying icon for menu items of Action Bar in Honeycomb android 3.0

Categories

Resources