Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does BottomSheetBehavior_Layout_behavior_hideable get translated to app:behavior_hideable?

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?

like image 885
Michael Osofsky Avatar asked Mar 26 '26 08:03

Michael Osofsky


1 Answers

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

like image 183
chessdork Avatar answered Mar 27 '26 23:03

chessdork