William Hu
William Hu

Reputation: 16149

Android why NameSpace app not found

I see below code to add actions on Toolbar from this link

https://developer.android.com/training/appbar/actions.html

but app:showAsActions error, why this app not found?

<!-- "Mark Favorite", should appear as action button if possible -->
<item
    android:id="@+id/action_favorite"
    android:icon="@drawable/ic_favorite_black_48dp"
    android:title="@string/action_favorite"
    app:showAsAction="ifRoom"/>

<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      app:showAsAction="never"/>

After i searched i added xmlns:app="http://schemas.android.com/apk/res-auto to menu node, but still got Unexpected namespace prefix 'app' for tag item, i know it's a noob question, thanks for you help!

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- "Mark Favorite", should appear as action button if possible -->
    <item
        android:id="@+id/action_favorite"
        android:icon="@drawable/ic_favorite_black_48dp"
        android:title="@string/action_favorite"
        app:showAsAction="ifRoom"/>

    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never"/>
</menu>

enter image description here

Upvotes: 1

Views: 4940

Answers (1)

user5248371
user5248371

Reputation:

Solution :

Have the right style selected as defined at manifest

Manifest

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

Style

 <style name="AppTheme"
    parent="Theme.AppCompat.Light.DarkActionBar">
    </style>

Select the right title for showAsAction

<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:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      **yourapp**:showAsAction="ifRoom"  />
    ...
  </menu>

Conclusion :

If you used xmlns:yourapp="http://schemas.android.com/apk/res-auto"

then you have to used yourapp. in showAsAction.

if you used xmlns:android then you have used to android:showAsAction.

Upvotes: 2

Related Questions