Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : difference namespace in layout file

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 :)

like image 704
hqt Avatar asked Jan 02 '26 03:01

hqt


1 Answers

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

like image 88
Maulik Sheth Avatar answered Jan 03 '26 16:01

Maulik Sheth