Why This Line Xmlns:Android="Http://Schemas.Android.Com/Apk/Res/Android" Must Be the First in the Layout Xml File

Why this line xmlns:android=http://schemas.android.com/apk/res/android must be the first in the layout xml file?

In XML, xmlns declares a Namespace. In fact, when you do:

<LinearLayout android:id>
</LinearLayout>

Instead of calling android:id, the xml will use http://schemas.android.com/apk/res/android:id to be unique. Generally this page doesn't exist (it's a URI, not a URL), but sometimes it is a URL that explains the used namespace.

The namespace has pretty much the same uses as the package name in a Java application.

Here is an explanation.

Uniform Resource Identifier (URI)

A Uniform Resource Identifier (URI) is a string of characters which
identifies an Internet Resource.

The most common URI is the Uniform Resource Locator (URL) which
identifies an Internet domain address. Another, not so common type of
URI is the Universal Resource Name (URN).

In our examples we will only use URLs.

Kotlin- xmlns:app=http://schemas.android.com/apk/res-auto is unavailable

This is because the srcCompat attribute only exists in the app (http://schemas.android.com/apk/res-auto) namespace for the AppCompatImageView class (see the API documentation for more info), not the default android (http://schemas.android.com/apk/res/android) namespace.

To fix this, simply rename the android portion of android:srcCompat to app:srcCompat:

<android.support.v7.widget.AppCompatImageView 
app:srcCompat="@drawable/empty_dice" />

(P.S. Consider self-closing the XML tag such that you don't have to write more code to close the XML element. See this question for more info)

Do we need to add xmlns:android=http://schemas.android.com/apk/res/android to every root view of an XML file?

Yes the namespace needs to be declared on every root view if you want to use andorid: prefixed attributes. However no, you don't need to declare the namespace on a different ViewGroup in the same file (and having multiple roots is not permitted).

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android">

<FrameLayout />
</FrameLayout>

You can take a look at this article from W3Schools which describes what namespaces are and why they're important.

What is the meaning of xmlns:tools in Android XML layout?

It defines the XML namespace of the document. You should put it, otherwise tags like <RelativeLayout> could be not recognied by the parser.

Namespaces are a way for XML documents to include tags from various vendors. By using xmlns attribute you declare, that, by default, you're using XML elements defined here: http://schemas.android.com/apk/res/android (note that this link is broken - this discussion explains why).

You also declare additional namespace, tools, which is not your default namespace, thus when referencing elements or attributes defined there, you must add tools prefix, on example:

tools:context=".SomeActivity"

What is the meaning of such lines like xmlns:app=http://schemas.android.com/apk/res-auto in android xml file?

When you want to import attributes in AndroidManifest.xml you have to kind of import their libraries. which you can then access by their prefixs {android:,tools:,app:}
Example of of usage are

android:name="yourpackege.App"

tools:overrideLibrary="yourpackege.App"

app:showAsAction="never"

In the above examples attributes being

android:name, tools:overrideLibrary, app:showAsAction

Of course every single prefix has many more attributes for example

android:name,android:theme,android:value

Kindly note though that

The app namespace is not specific to a library, but it is used for all attributes defined in your app readhere

Just to test the explanation above if you remove lets say
xmlns:android="http://schemas.android.com/apk/res/android" and rebuild your android application, you will get and error that looks something like Sample Image

Android Material and appcompat Manifest merger failed

Just change "rc01" in first and last line inside dependencies to "alpha1"
This is working for me

How to constraint a relative layout to take maximum of specified width on screen

I would suggest usage of linear layout. It is the fastest one, and you could achieve what you want in, on my opinion, easy way:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:weightSum="1">

<LinearLayout
android:layout_weight="0.6"
android:layout_width="0dp"
android:gravity="end"
android:layout_height="wrap_content">

<TextView
android:id="@+id/txtSentMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="This is a very long message that will go in two lines"
android:background="@color/colorAccent"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>


Related Topics



Leave a reply



Submit