What does notations in XML mean for Android UI design

Issue

I begin to study Android development. I’ confused about the XML meaning for the UI design. Such as the following code, what the keyword xmlns, android, tools, app mean ? Are they some objects? For example, android:layout_width means that the property of the object android.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

As someone answered, ns is a namespace. You can actually call them anything- if you wanted to use "foobar:" instead of "android:" you can, you’d just change xmlns:app to xmls:foobar.

Now there are conventions most people follow, to make code more readable. "android:" is the stuff built into the framework. "app:" are attributes created by your app (or any library you use) and are generated at compile time. "tools:" are meant for things the IDE reads for display, such as default values for preview. Keeping to these names will make it easier for others to read your code.

Answered By – Gabe Sechan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published