yas
yas

Reputation: 3620

Namespace of element declaring a namespace?

In XML such as:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"

    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:padding="24dp"
       android:text="@string/question_text" />

</LinearLayout>

What is the namespace of the LinearLayout and TextView elements?

Would it be the http://schemas.android.com/apk/res/android namespace? Or are they not in any namespace at all?

Lastly, do attributes always have to explicitly define namespace they belong to, if any?

Upvotes: 0

Views: 102

Answers (1)

MrEngineer13
MrEngineer13

Reputation: 38866

With widgets supplied by Android, you do not need to use the full package declaration just the bare class names (Ex LinearLayout, TextView, etc.). However if you create a custom widget, then you'll have to use its package based declaration (ex. com.example.android.MyAwesomeWidget).

Only the root widget requires the Android XML namespace (xmlns:android="http://schemas.android.com/apk/res/android") because all of the child elements inherit that namespace. This means once you define a namespace in your root element you do not need to place it in child elements.

And finally, attributes must explicitly use the namespace prefix to avoid confusion and collision. Ex: android:layout_width="match_parent" is valid but layout_width="match_parent" is not.

Upvotes: 2

Related Questions