3

In the Android documentation for BottomSheetBehavior, it says I can use the following attribute in XML:

BottomSheetBehavior_Layout_behavior_hideable

I tried this:

android:BottomSheetBehavior_Layout_behavior_hideable="true"

But that gave me the following error:

Unknown attribute android:BottomSheetBehavior_Layout_behavior_hideable

That error is discussed at Unknown attribute android:layout_width, layout_height, id, gravity, layout_gravity, padding but none of those solutions worked for me because they were about syncing project files. Mine are synced. Nobody questioned the validity of the attribute name, which is what I think is my problem here.

Then I tried this:

app:BottomSheetBehavior_Layout_behavior_hideable="true"

But that gave me the following error:

Unexpected namespace prefix "app" found for tag

That error is discussed at Unexpected namespace prefix "app" found for tag RelativeLayout - Android? but none of those solutions worked for me, and--more central to my question--there the attribute seems to be written like this:

app:behavior_hideable="true"

Is app:behavior_hideable the correct way to write BottomSheetBehavior_Layout_behavior_hideable? What is the name of the mechanism that performs this translation? Where is its documentation?

Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113

1 Answers1

3

There are a couple of components to the answer.

  1. In the constructor for a BottomSheetBehavior, xml attributes are read out as follows Source:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BottomSheetBehavior_Layout); setHideable(a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_hideable, false));

  1. These attributes are typically defined in an attrs.xml file. Here's the attrs.xml for the BottomSheetBehavior.

So what's happening here is a LayoutInflater is calling the constructor, and xml attributes are accessed via R.styleable.[name_of_style]_[name_of_attribute]. When you want to apply the style in xml, you simply use the name of the attribute. In the case, the name of the style is "BottomSheetBehavior_Layout", and the name of the attribute is "behavior_hideable". Similarly, you could also use "behavior_skipCollapsed" and "behavior_fitToContents".

For more information on styling, the official docs are here: https://developer.android.com/training/custom-views/create-view#customattr

chessdork
  • 1,999
  • 1
  • 19
  • 20
  • Thank you, that's helpful information. So is `app:` the proper prefix? From the official docs link you provided, I can tell that `android:` is not the proper prefix because it says "Instead of belonging to the http://schemas.android.com/apk/res/android namespace, they belong to http://schemas.android.com/apk/res/[your package name]." However, why would `app:` be the proper namespace if I didn't create https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/bottomsheet/res/values/attrs.xml#L24 ? What is the right namespace? – Michael Osofsky Oct 30 '18 at 18:16
  • 1
    @MichaelOsofsky Here's a good answer https://stackoverflow.com/a/26692768/3781068. Basically because `BottomSheetBehavior` is in the support library and not "vanilla android", it uses the app namespace. So in general, if it's made available via a gradle dependency, you need to use `app:`, otherwise fall back to `android:` – chessdork Oct 30 '18 at 18:41