Ad xmlns attributes in java - android

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.

Related

How can you use tools in android layout to replace a merge item for designer

My project contains a number of custom layouts that (mostly) extend ConstraintLayout
I use xmls like this:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
......
to create the views and then I inflate them in my code
The problem is that I can't really see the changes I make to the view unless I change the merge tag with ConstraintLayout
Is there a tools parameter that could help me in this, so that they would at least show correctly in the designer?
Adding tools:parentTag attribute to your merge tag with appropriate value should allow you to see the layout in the Preview window. Depending on which library you use it should be either
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout"
or
tools:parentTag="android.support.constraint.ConstraintLayout"
To be able to use the tools namespace you also need to add
xmlns:tools="http://schemas.android.com/tools"
to your merge tag.

Why Android use separate xmlns for ContraintLayout

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.

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

Is it possible to convert java views to xml layout?

I a created a linearlyaout and added views to it using java code. is it possible to convert this layout to xml layout and save it to the storage ?
There is nothing stopping you from parsing all those Views attributes and then building a xml layout file. However you will not be able to use that constructed xml layout(like setContentView(R.layout.built_layout)) like other layouts from the res/layout folder.

Categories

Resources