Action Bar doesn't appear on galaxy S3 - android

I followed the android developers training and
wrote application that should have action bar.
I added two items to the Action Bar:
the first has icon and android:showAsAction="IfRoom"
the second, has only string and android:showAsAction="Never".
main_menu_actions.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.shaniandroid.myfirstapp.MainActivity" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/search_icon"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
However the Action Bar is not displayed!
The items are only displayed when the menu button
is pressed.
I read in earlier answers that the "overflow" button doesn't appear
on 4.3 if a hardware menu button is present - and that is OK for the second item.
But what about the first item - shouldn't it appear with its icon on top of screen?
Notice: I run the app on galaxy S3 4.3.

If you are using AppCompat then you should use app namespace for showAsAction which you have declared. Your main_menu_actions.xml should then look like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.shaniandroid.myfirstapp.MainActivity" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/search_icon"
android:title="#string/action_search"
app:showAsAction="ifRoom" /> <!--here-->
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never" /> <!--and here-->

Related

I cannot get icons on the action bar

I tried to add an icon for "Create Order" to be displayed on the action bar, but the icon is not being displayed instead on the action bar only 3 dots are being showed and inside them there is written "Create Order".
The following is my xml code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:icon="#drawable/ic_add_black_24dp"
android:orderInCategory="1"
app:showAsAction="always">
</item>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
Use app:showAsAction to ifRoom as below:
<?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/create_order"
android:icon="#drawable/ic_add_black_24dp"
android:title="#string/action_create_order"
app:showAsAction="ifRoom" />
</menu>
Note:
The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar. If you set app:showAsAction="ifRoom" (as in the example code's favorite action), the action is displayed as a button if there is room in the app bar for it; if there is not enough room, excess actions are sent to the overflow menu. If you set app:showAsAction="never" (as in the example code's settings action), the action is always listed in the overflow menu, not displayed in the app bar.
Refer below link for more details:
Add Action Buttons
If you want to show the Icon and the Text "Create Order" you can use:
app:showAsAction="ifRoom|withText"
of course, it can show only, if your view has enough space for these elements.

Action item always appears in the overflow menu

I am using v7 Support library and using app namespace in the menu_main.xml file. Even then, the action is never displayed in the action bar but in the overflow bar. This happens even when I use app:showAsAction="always"
menu_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:orderInCategory="1"
app:showAsAction="always"
android:icon="#drawable/add_box_black_icon"
/>
</menu>
The order of the items is important - swap the create order and settings if you want to achieve your original idea.

How to hide action bar on clicking search menu item and display when search is done?

I've an action bar and three tabs each includes some list to display.
When I click search I want the rest of the action bar should be hidden except the list in a tab and search item it self should be enabled, where want to search.
How can I achieve it?
Edit:
<?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">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item
android:id="#+id/action_search"
android:title="#string/action_search"
android:orderInCategory="100"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/home"
android:icon="#drawable/icon_home"
android:title="Home" />
<item android:id="#+id/totalCash"
android:icon="#drawable/icon_delivered"
android:title="Cod" />
<item android:id="#+id/statusForEach"
android:icon="#drawable/icon_cancelled"
android:title="Status" />
<item android:id="#+id/logout4"
android:icon="#drawable/icon_cancelled"
android:title="Logout" />
<!--<item android:id="#+id/menu_delete"
android:icon="#drawable/icon_delete"
android:title="Delete" />
<item android:id="#+id/menu_preferences"
android:icon="#drawable/icon_preferences"
android:title="Preferences" />-->
add below line to all menu items except search
app:showAsAction="ifRoom"
and in your search item add
app:iconifiedByDefault="true"
You have to implement MenuItemCompat interface to get those events. that will allow you to listen open and close and many more events. so check it.

Android - The 3-dot dropdown menus does not appears if the device has a menu hardware button [duplicate]

This question already has answers here:
How to force action bar overflow icon to show
(2 answers)
Closed 7 years ago.
I noticed that if you are using the actionbar with a device that has a "menu" hardware button, the 3-dot dropdown menus does not shows off. So in my app i have this menu layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never"
android:orderInCategory="100"
/>
<item
android:id="#+id/action_credits"
android:title="#string/action_credits"
android:orderInCategory="100"
app:showAsAction="never"
/>
<item android:title="#string/action_project"
android:id="#+id/action_project"
android:orderInCategory="100"
app:showAsAction="never"
/>
</menu>
If i try my app in a device without the hardware menu button, then the 3-dots appear in the action bar, otherwise the 3-dots dropdown menu is not displayed. Why? How can I force the display of the menu to leaving aside whether the device is or is not in possession of the key hardware ?
Overflow menu (3 dot) not shown by design on devices with hardware menu button.
This is correct behaviour. Yom must not force it to show or mimic in any way.
Android phones with traditional navigation hardware keys don't display
the virtual navigation bar at the bottom of the screen. Instead, the
action overflow is available from the menu hardware key.
Try like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:icon="#drawable/menu_icon"
app:showAsAction="always">
<menu>
<item
android:id="#+id/action_settings1"
android:title="#string/action_settings1"
app:showAsAction="never"
android:orderInCategory="100"
/>
<item
android:id="#+id/action_credits"
android:title="#string/action_credits"
android:orderInCategory="100"
app:showAsAction="never"
/>
<item android:title="#string/action_project"
android:id="#+id/action_project"
android:orderInCategory="100"
app:showAsAction="never"
/>
</menu>
</item>
</menu>

Create ActionBar and options menu in same activity

I'm trying to show same menu options both in the ActionBar and in the menu options but it shows up only in one of them, is there a simple way to do it?
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.app.MainActivity" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="always"/>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
Now the items appear in the action bar.
Changing the app:showAsAction to android:app:showAsAction shows the menu items at the options menu only, any way to make them appear in both menus?
Thanks
Change showAsAction="always" to showAsAction="never" for which you want to display in option menu at bottom...
See showAsAction element in below link for more details...
http://developer.android.com/guide/topics/resources/menu-resource.html

Categories

Resources