How to add SearchView to action bar in android - android

In the following snippet, what does yourapp refer to? I try using the package name, then the project name, but they aren't the answer as the code does not compile
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

if you are going to use SearchView in API less than 11 (obviously you are not) you must extend your activity from ActionBarCompatand in your xml layout of your menu you must use another xml name space for showAsAction and actionViewClass. then you must use searchview from support library.(android.support.v7.widget.SearchView) the xml name space is arbitary and here you called it yourapp. So you must use below code because of android:minSdkVersion="11":
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>

Related

How to mock menuitem where menu icon is null?

I have to following menu 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/menu_add"
android:contentDescription="#string/menu_item_add"
android:icon="#drawable/ic_add"
android:title="#string/menu_item_add"
app:showAsAction="ifRoom" />
</menu>
But I want to create a test where icon is null and run it .
Any idea how to mock the menu item without icon?

How to show icon in toolbar instead of text

I am trying to show some icons in the toolbar using the following menu but in the output Instead of icons, only text is displaying. How to display icons instead of text
menu.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">
<item
android:id="#+id/toEditName"
android:title="#string/buttonEditName"
app:showAsAction="ifRoom"
tools:icon="#drawable/ic_edit_name" />
<item
android:id="#+id/toHistory"
android:title="#string/buttonHistory"
app:showAsAction="ifRoom"
tools:icon="#drawable/ic_history" />
</menu>
Preview Image
Actual Output Image
You have to use android:icon attribute not tools:icon Try this
<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">
<item
android:id="#+id/toEditName"
android:title="#string/buttonEditName"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_edit_name" />
<item
android:id="#+id/toHistory"
android:title="#string/buttonHistory"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_history" />
</menu>
Your icons are not displayed because you are using tools attribute, which show the icon when you are working in the android studio. If you run the app the icons are not displayed.
<?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/toEditName"
android:icon="#drawable/ic_edit_name"
android:title="#string/buttonEditName"
app:showAsAction="ifRoom" />
<item
android:id="#+id/toHistory"
android:icon="#drawable/ic_history"
android:title="#string/buttonHistory"
app:showAsAction="ifRoom" />
</menu>
I hope it helps.
it was a bit late, but tools attribute usually is used to display something in your Android Studio xml preview. That is why your icon not showing.
<?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/multi_select_reply"
android:title="#string/reply"
android:icon="#drawable/ic_reply_svg" <--- change tools:icon to android:icon
android:iconTint="#color/colorWhite"
app:showAsAction="ifRoom"/>
</menu>
Hope it helps to you!

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.

app:showAsAction="always|withText" not showing text

I have an action bar with the follwing menu:
<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="br.com.putzbati2.PrincipalActivity" >
<item android:id="#+id/action_mensagem"
android:title="Mensagem"
android:icon="#drawable/barra_superior_envelope"
android:showAsAction="always|withText" />
<item android:id="#+id/action_carro"
android:icon="#drawable/barra_superior_carro"
app:showAsAction="always"
android:title="Meus Carros"/>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
But the withText doesnt work. It shows only the icon.
I've already try to use android:showAsAction instead of app:showAsAction and when I do this, it doesnt show the icon neither the text. Does any one know how to help me?
I've found this solution, but as I said, it doesnt work for me
app:showAsAction ifRoom is not working on appcompat action bar
You can use like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_mensagem"
android:title="#string/id_item_mensagem"
android:icon="#drawable/id_item_mensagem"
android:showAsAction="always|withText" />
<!-- ... another items -->
</menu>
'always|withText' will work if there is enough room, otherwise it will only place icon! You are using so much items with text, probably you don't have room. So you will see only the icon.
You can check it if you rotate your application to landscape.

Invisible ActionBar Search Icon

So i set the theme as "android:Theme.Holo.Light.DarkActionBar", and i've added a search icon from holo.dark. However, it's impossible(nearly) to see this icon. Anyway to make it 'whiter'?
try this
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:title="#string/search_actionbar"/>
</menu>
am also using same theme for me action bar search icon showing white color only..

Categories

Resources