Why Android use separate xmlns for ContraintLayout - android

I'm little confused about ConstraintLayout in Android. I was learning about it and first thing I get confused about it why android use separate xml namespace for it? xmlns:app="http://schemas.android.com/apk/res-auto"
ConstraintLayout is part of Layout then why google didn't added it in the old namespace? xmlns:android="http://schemas.android.com/apk/res/android"
and why android required to write full qualified name in XML? android.support.constraint.ConstraintLayout why not just ConstraintLayout? as RelativeLayout?

ConstraintLayout is part of Layout then why google didn't added it in the old namespace?
That is reserved for platform-defined attributes. ConstraintLayout is from a library, not from the platform.
and why android required to write full qualified name in XML? android.support.constraint.ConstraintLayout why not just ConstraintLayout?
Android only knows to look in a couple of platform-defined packages for views using the shorthand, bare-class-name-only notation. android.support.constraint is from a library, not the platform.

Related

Android Layout - when to use app: vs android:?

I've been writing some Android apps but I don't really understand when to use app: and when to use android:. When styles are not being applied the way they're supposed to, I use trial and error and sometimes find that using app: instead of android: solves the issue but I don't understand why. It'd be great if someone could point me in the right direction. Thanks!
You can use the app namespace to have app compatibility with older API versions.
For example
app:srcCompat="#drawable/customborder" has the same effects with
android:background="#drawable/customborder"
The difference is that the first will work correctly with older API's and the second will not display what you would like.
You are talking about custom namespace.In android we can create custom views in additional to already available views.
As per in Google developer docs..
To add a built-in View to your user interface, you specify it in an XML element and control its appearance and behavior with element attributes. Well-written custom views can also be added and styled via XML. To enable this behavior in your custom view, you must:
Define custom attributes for your view in a resource element
Specify values for the attributes in your XML layout
Retrieve attribute values at runtime
Apply the retrieved attribute values to your view
Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. 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]
So for if you use default views you can use android namespace and if you want to set and use attributes for custom view you can define your own name.
Refer this
If you take a look at the beginning of the your layout xml files (in which you used app:) you will (probably) find lines like this:
<?xml version="1.0" encoding="utf-8"?>
<SOME_LAYOUT xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
in this case app: namespace will be used for custom attributes, specified by you inside attrs.xml file or by someone else in one of used libraries.
Sometime the property with android only available in new Android version like
In this case, you should use app:... to make it work with older version.
moreover you will find two variants
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/[packagename]"
the difference between xmlns lines is res-auto take care of resolving our package as sometime we will add .debug or .test in our package and we already provided the packageid of the app Ex:
xmlns:app="http://schemas.android.com/apk/com.test.io.debug"
xmlns:app="http://schemas.android.com/apk/com.test.io.test"

what is "android" in xml file? Where does definition of Button come from?

We start xml file with some layout. Within that layout we create our views like Button, TextView etc. But how does the system know what are Button and TextView? I mean we are not importing anything. Moreover inside Button, we write android:layout_width = "wrap_content" what is android in this? Since it is inside Button why can't we write layout_width = "wrap_content" directly?
From developer.android.com android: defines the Android namespace. This attribute should always be set to "http://schemas.android.com/apk/res/android".
xmlns:android is for identification that this xml is used for android, not for other function.
Namespaces uniquely identify code/libraries. If I write an api that uses all the same names and such as the android api the only way to distinguish between my api and android api is to use the android namespace, or mine. Read XML NameSpace
Check out tutorial on namespaces
It is called as the namespace. At the top of every XML file you will have this line xmlns:android="http://schemas.android.com/apk/res/android" That's where the namespace android comes from
XML is a document language. The meaning of the elements is given to them by the compiler.
When you compile the XML file using Android's resource compiler, it imparts meaning to them.
The prefix android: establishes the namespace of the tag that follows it.
You can learn more about XML namespaces here: http://www.w3schools.com/xml/xml_namespaces.asp

Ad xmlns attributes in java

I am currently converting an xml file to java coding, but I have met a problem. In my xml file I have a View with two xmlns attributes like this :
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
How can I ad these to my custom FrameLayout in java?
The purpose of the xmlns attributes is to tell the design-time and build-time tools about the various UI elements such as Button, TextView, FrameLayout etc.
Simply importing the relevant widget and view classes if you're writing Java code to build the layout dynamically is basically the equivalent. In other words, don't worry about the xmlns attributes.

Android XML layout files and namespace

Android layouts are defined in XML with this namespace declared in the root element:
xmlns:android="http://schemas.android.com/apk/res/android"
Example of an element:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" />
Why is the android prefix used instead of ommitting it like xmlns="http... ?
Why is the prefix used only in attributes and not in elements?
Interesting question! it sure feel a bit weird.
It was a design choice by Google to be as strict as possible on namespace to handle errors at compile time.
The prefix is not used on elements because theses are representing Java classes: com.android.widget.TextView (com.android.widget.* can always be truncated). The java namespace of this class will be automagically resolved at compile time, so an xml namespace representing a fully qualified java namespace is not welcome here. But attributes can be mapped to any of the inherited java classes of the Element. Hence the namespace on attributes to allow inheritance.
This is done like this mainly because the layout describes Java objects and Google here is using the XML namespace mechanism to help in mapping your Layout with Java objects. So there are collisions between the Java namespace world and XML namespace world.
It also allow us developers to subclass elements, add our own attributes without worrying that the next version of the platform will maybe add an attribute with the same name.
See the two replies to this blog post by Dianne Hackborn, a well-known android engineer working at google: http://www.elharo.com/blog/software-development/xml/2008/09/16/android-xml-weirdness/

Define custom Android components using short namespace?

Working on my first Android application. I'm wondering if there's a way to use the xmlns in the markup in any way. In Flex, for example, I can define a namespace:
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cb="com.typeoneerror.apps.app.views.components.*">
<cb:CustomComponent paramName="demo"></cb:CustomComponent>
</mx:VBox>
Android seems to be slightly different. You use the namespace when defining params but not the tag itself. This is a bit wordy to me, so I'm wondering if there's a way to configure or change this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cb="http://schemas.android.com/apk/res/com.typeoneerror.apps.app">
<com.typeoneerror.apps.app.views.components.CustomComponent cb:paramName="demo"/>
</LinearLayout>
I'd like to use
<cb:CustomComponent cb:paramName="demo"></cb:CustomComponent>
Possible?
No, sorry. The element name is a Java class name, and in the case of custom widgets, is a fully-qualified class name.
I have seen some syntax where the element name is View and there is a class attribute with the widget's class name. I can't find that in the docs and don't have an sample available, though.

Categories

Resources