Android Action bar not showing Icons - android

How can i display Icons in Actions bar, here is my 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">
<item android:id="#+id/search_icon"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:orderInCategory="0"
android:title="Search" >
</item>
</menu>
and
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater mif = getMenuInflater();
mif.inflate(R.menu.custom_action_bar,menu);
return super.onCreateOptionsMenu(menu);
}
Thanks for Help !

If using app compat use app:showAsAction
<item android:id="#+id/search_icon"
android:icon="#drawable/ic_action_search"
app:showAsAction="always"
android:orderInCategory="100"
android:title="Search" >
</item>

In Manifest.xml File
<activity android:name="your activity name"
android:theme="#style/Theme.Holo.Light"></activity>

Related

How do i make sure the Menu Item is always on the topbar?

the 2 menu item connect and disconnect coded such that only 1 of them shows at a time.
I want to make it on the top bar, and not under the ... button.
following is my menu xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_refresh"
android:checkable="false"
android:orderInCategory="1"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_connect"
android:icon="#android:color/holo_blue_bright"
android:orderInCategory="100"
android:title="#string/menu_connect"
app:showAsAction="ifRoom|withText" />
<item
android:id="#+id/menu_disconnect"
android:orderInCategory="101"
android:title="#string/menu_disconnect"
app:showAsAction="ifRoom|withText" />
You can try:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_refresh"
android:checkable="false"
android:orderInCategory="1"
app:showAsAction="always" />
<item
android:id="#+id/menu_connect"
android:icon="#android:color/holo_blue_bright"
android:orderInCategory="100"
android:title="#string/menu_connect"
app:showAsAction="always" />
<item
android:id="#+id/menu_disconnect"
android:orderInCategory="101"
android:title="#string/menu_disconnect"
app:showAsAction="always" />
I hope it will help your problem!
If you still have same problem even you set app:showAsAction="always", you should check onCreateOptionsMenu. Please try this if you're creating menu differently, it will help you.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
Have you tried setting...
app:showAsAction="always"
... in the item you want to be shown in the top bar (app bar) always?
EDIT:
or...
app:showAsAction="always|withText"
if yout want to show the title too.

How to display menu item in action bar in android

menu item is not appearing in action bar. I am trying this code on API 19, kitkat 4.4
using this XML code.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="Sign out"
android:id="#+id/iSignout"
android:icon="#drawable/signout"
android:orderInCategory="100"
android:showAsAction="always"/>
<item
android:title="My Profile"
android:id="#+id/iEditProfile"/>
<item
android:title="Edit Profile"
android:id="#+id/iMyProfile"/>
</menu>
and
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_menu, menu);
return true;
}
Try this:
<?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:title="Sign out"
android:id="#+id/iSignout"
android:icon="#drawable/signout"
android:orderInCategory="100"
app:showAsAction="always"/>
<item
android:title="My Profile"
android:id="#+id/iEditProfile"/>
<item
android:title="Edit Profile"
android:id="#+id/iMyProfile"/>
</menu>
Replace android:showAsAction="always" with app:showAsAction="always"
Try
android:showAsAction="ifRoom"
How to display settings menu on click of settings icon? settings-menu-on-click-of-settings-icon/41716117#41716117

Android: Action button icons always appear in overlay

Action items always appear in overlay. Here is the code:
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"
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_event_black_18dp"
android:orderInCategory="1"
app:showAsAction="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
and in the main activity:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
Shouldn't the first item appear on the action bar because of "app:showAsAction="always" ?
Use this inside your item ,to display in Action bar
app:showAsAction="always"
When you dont want to display item in Action bar
app:showAsAction="never"
Try this piece of code , instead of your creating Option Menu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
If it is for fragments
custom namespace will prevent showAsAction from showing. so
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:icon="#drawable/ic_event_black_18dp"
android:orderInCategory="1"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_add" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>

getActionView from MenuItem returns null if wrong order

As the title says, I can't figure it out. I'm NOT using compat support library. Using android:Theme.Holo.Light.DarkActionBar
This is my action_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/action_search"/>
</menu>
This is my activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_menu, menu);
mi = menu.findItem(R.id.action_search);
search = (SearchView)mi.getActionView();
search.setOnQueryTextListener(getOnQueryTextListener());
new SearchTask().execute(getIntent().getStringExtra("query"));
return true;
}
mi is a MenuItem. search is a SearchView.
If the order of declaration of the items in action_menu.xml is as written, getActionView returns null.
If the order is inverted, it works. Why?
try like this,
<?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:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
android:title="#string/action_search"/>
</menu>
hope it will help you

android setting up the Action bar items

The below layout not displaying search icon only the another icon is displaying and the search comes as the drop down menu of other icon.I want it as separate icon in the action bar
<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.diveintojava1.MainActivity" >
<item android:id="#+id/search"
android:title="search"
android:icon="#drawable/search1"
android:actionViewClass="android.support.v7.widget.SearchView"
android:showAsAction="collapseActionView|ifRoom"
/>
<item android:id="#+id/file"
android:title="drop"
android:icon="#drawable/moremenu" >
<menu>
<item android:id="#+id/create_new"
android:title="java" />
<item android:id="#+id/open"
android:title="rate us" />
</menu>
</item>
</menu>
And below is the java code of implementing menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}
Try this, see more here : http://developer.android.com/guide/topics/ui/actionbar.html and here http://sourceoncloud.wordpress.com/2013/07/26/actionbar-compact/
<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"
xmlns:appcompact="http://schemas.android.com/apk/res-auto"
tools:context="com.example.diveintojava1.MainActivity" >
<item android:id="#+id/search"
android:title="search"
android:icon="#drawable/search1"
android:actionViewClass="android.support.v7.widget.SearchView"
appcompact:showAsAction="collapseActionView|ifRoom"
/>
<item android:id="#+id/file"
android:title="drop"
android:icon="#drawable/moremenu" >
<menu>
<item android:id="#+id/create_new"
android:title="java" />
<item android:id="#+id/open"
android:title="rate us" />
</menu>
</item>
</menu>
android:showAsAction="collapseActionView|ifRoom" Change ifRoom to always
Change this:
android:showAsAction="collapseActionView|ifRoom"
into this:
android:showAsAction="always"

Categories

Resources