-1

I just started my journey on Android Development and there's one thing that is confusing me.

When creating menu items why do we need to specify an alias for properties when android studio's autocomplete suggests the use of android:showAsAction="value"?

when using android:showAsAction the property is ignored. If i replace it for anything else it works. Why?

Related https://stackoverflow.com/a/17914095/1084568

Community
  • 1
  • 1
Lothre1
  • 3,523
  • 7
  • 43
  • 64
  • It's not `anything else`. It's the namespace you defined earlier: `xmlns:[yourapp]="http://schemas.android.com/apk/res-auto"`. Nothing stops you defining `xmlns:android="http://schemas.android.com/apk/res-auto"` – Phantômaxx Jun 13 '15 at 14:53
  • by define the namespace you mean use this: xmlns:doo="http://schemas.android.com/apk/res-auto" /////// where doo is my namespace? – Lothre1 Jun 13 '15 at 14:55
  • 2
    **Yes**, exactly. `xmlns = XML NameSpace` – Phantômaxx Jun 13 '15 at 14:55
  • 2
    After all it was simple and i was complicating. From the linked post, the yellow rectangle cites a reference from the documentation, where it says that `android: ` namespace is not able to provide access to the property if the we pretend to support older android versions such as 2.1. Thanks for the clarification @DerGolem – Lothre1 Jun 13 '15 at 15:02

1 Answers1

3

If you are using the native action bar, you use android:showAsAction. Any time you see android: as a prefix, you know that it is an attribute defined by the Android framework.

If you are using the appcompat-v7 backport of the action bar, that comes from a library (appcompat-v7). Libraries cannot invent new android: attributes. Instead, for library-defined attributes, you use a new namespace (e.g., app:) tied to a http://schemas.android.com/apk/res-auto URL.

it says that android: namespace is not able to provide access to the property if the we pretend to support older android versions such as 2.1

Correct. In this case, while android:showAsAction was added to the framework in Android 3.0, part of the goal of appcompat-v7 is to support back to Android 2.1. While Google has a time machine, they have not been using it to "retcon" Android and add in attributes that formerly did not exist.

(though, if they did, we wouldn't know about it, as our past would have been altered to have those attributes, unless we somehow have an existence outside the normal space-time continuum, which frequently seems to involve wearing a cape)

So, appcompat-v7 can use attributes like android:icon, which existed from Android 1.0, but cannot support an android:showAsAction prior to Android 3.0. Hence, they have their own attribute.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Superb, this gives me even more insight on how things are structured. I wasn't aware that showAsAction had been introduced since v3.0. – Lothre1 Jun 13 '15 at 15:21