3

I'm using Android Support Library AppCompat for my app, and I try to add Search Widget into my view. Firstly, I meet a very frustrated error, that Search Widget is not appear on screen. Here is the menu xml file :

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_example"
        android:title="@string/action_example"
        app:showAsAction="withText|ifRoom" />

    <item android:id="@+id/search_bar"
        android:title="@string/search_title"
        android:icon="@drawable/ic_menu_search"
        android:showAsAction="always" (LINE ONE)
        android:actionViewClass="android.support.v7.widget.SearchView"/> (LINE TWO)
</menu>

After hours for debugging. I have noticed error and change to below xml file:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_example"
        android:title="@string/action_example"
        app:showAsAction="withText|ifRoom" />

    <item android:id="@+id/search_bar"
        android:title="@string/search_title"
        android:icon="@drawable/ic_menu_search"
        app:showAsAction="always"  (LINE ONE)
        app:actionViewClass="android.support.v7.widget.SearchView"/> (LINE TWO)
</menu>

As you notice that there is just a slight difference at LINE ONE and LINE TWO that I change android to app. That works magically. But, I don't know what difference behind that two lines. what difference and meaning when we declare:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

I have google but no post relates to my problem and question.

Thanks :)

hqt
  • 27,058
  • 46
  • 161
  • 235

3 Answers3

4

you use your own custom namespace "app" because the attribute "showasaction" does not exists in android framework for older android version. hence when you use action compat library you need to declare your own namespace for that attribute.

from developers doc

Using XML attributes from the support library Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library. If your menu item supplies both a title and an icon—with the title and icon attributes—then the action item shows only the icon by default. If you want to display the text title, add "withText" to the showAsAction attribute. For example:

http://developer.android.com/guide/topics/ui/actionbar.html

Maulik Sheth
  • 574
  • 3
  • 10
1

Looks like AppCompat declares these XML tags as custom elements. Here is a Q/A about custom elements, probably it makes it clearer: Declaring a custom android UI element using XML

Community
  • 1
  • 1
Andrew
  • 199
  • 1
  • 15
1
  1. First of all this happened because you are trying to use custom view custom properties which is define for android.support.v7.widget.SearchView custom view

Example:

app:showAsAction="always", 
app:actionViewClass="android.support.v7.widget.SearchView" 
  1. when we use android default properties like android:layout_width="match_parent" or android:layout_height="wrap_content" then it doesn't give error because we have already defined xmlns for that like xmlns:android="http://schemas.android.com/apk/res/android"
  2. So if we try to use custom view custom properties then we have to define xmlns for that.
  3. In your case i see you define this way xmlns for SearchView xmlns:app="http://schemas.android.com/apk/res-auto" fine there is one way and other way is xmlns:app="http://schemas.android.com/apk/res/**packagenameofSearchViewClass**"

In first way you gave all external custom view custom properties can be used in are xml like support.v7 and any other lib and in second way you gave specifics external lib custom view custom properties can be use in are xml.

hqt
  • 27,058
  • 46
  • 161
  • 235
Haresh Chhelana
  • 23,930
  • 5
  • 51
  • 66