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

For example, in:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
...
Do I need to put it?

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"

Following is a useful link from Android dev portal: https://developer.android.com/studio/write/tool-attributes.html
It says
Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
i.e. tools namespace helps designing UI and all attributes with prefix 'tools' will be removed at build time.

In fact, when you do :
<RelativeLayout android:id> </RelativeLayout>
Instead of calling android:id, the xml will call http://schemas.android.com/apk/res/android:id . It just the page that declare all the attribute and views that you can use in your xml.
Here is an explanation.
http://www.w3schools.com/xml/xml_namespaces.asp

Related

How to show invisible Views in designer?

So, I have a Layout that is
android:visibility="invisible"
is there a way to force ui designer in intellij idea to display such elements at all times?
According to the documentation you can use the tools xmlns:
tools:visibility="visible"
Android Studio supports a variety of XML attributes in the tools
namespace that enable design-time features (such as which layout to
show in a fragment) or compile-time behaviors (such as which shrinking
mode to apply to your XML resources). When you build your app, the
build tools remove these attributes so there is no effect on your APK
size or runtime behavior.
To use these attributes, add the tools namespace to the root element
of each XML file where you'd like to use them, as shown here:
<RootTag xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
Use this tools:visibility="visible"

xml version of iconifiedByDefault ignored

Why is the line android:iconifiedByDefault="false” always ignored, requiring me to always have to find a way to do it automatically? If it’s always going to be ignored, why include it as an option? Am I missing something?
Like most of the Views in the support libraries, the v7 appcompat SearchView uses attributes specific to it that are defined in the app's namespace, rather than the system namespace. This ensures that the attributes can be used in all Android versions that the library supports.
You just need to use your app's namespace prefix on the iconifiedByDefault attribute. For example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
... >
<android.support.v7.widget.SearchView
...
app:iconifiedByDefault="false" />
</RelativeLayout>

app:something...What is app:?

I am using this famous ViewPagerIndicator library in my project. I wanted to add color on ViewPageIndicator. I found a solution from here on StackOverflow.
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
app:fillColor="#FF888888"
app:pageColor="#88FF0000"
app:strokeColor="#FF000000" />
But one thing I can't understand here: why it is written app: instead of android:? What does app: mean? I have never used app:? What is its namespace? I googled but no solution found.
For the namespace, add this: xmlns:app="http://schemas.android.com/apk/res-auto"
The app namespace is used for all 'new' attributes. For example: if I make a custom view, that has its own attributes (let's say RainbowView with new attribute 'numberOfColors'), then I have to use that attribute in the app namespace because it isn't declared in the android namespace. So use the app namespace for all custom attributes. Either attrs you defined yourself, or attrs the author of a library added (in your case for CirclePageIndicator). But most of the time, when you start typing 'strokeCo...' and you see 'strokeColor' in the autocomplete list, the namespace will be added automatically when you select it.
Probably you only need to add the missing namespace:
xmlns:app="http://schemas.android.com/apk/res-auto"
Or take a look at this answer for referencing a library:
How can I set up custom namespaces in layout file in Android studio?

difference in naming xml between res-auto and com.package.name - android

I have seen custom xml with :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
and
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/com.package.custom"
whats the difference between these two separate names?
Is the latter only points to default location like your package?
Is the former points to the reference lib ?
thanks.
If we add a new custom view and its attributes inside our project, you add this at the beginning of your layout:
xmlns:custom="http://schemas.android.com/apk/res/your_main_app_package
If the new custom view is inside a library project linked to your project, you add this:
xmlns:custom="http://schemas.android.com/apk/res-auto
Note: This problem has been fixed in ADT revision 17+ . For any services or Activities, declare the namespace as follows:
xmlns:custom="http://schemas.android.com/apk/res-auto"
The suffix res-auto will be replaced at build time with the actual project package, so make sure you set up your attribute names to avoid collisions if at all possible.

Android custom attributes on "normal" views?

After taking a look at theming for Fede's UberMusic I came across the file album_screen.xml. Below is the source of that file. Basically I noticed that his themes have the ability to use custom views that are a part of his application, and that they have this XML namespace at the top theme. I am missing the connection as to how he is able to apply his attributes to views that he does not control, and how Eclipse will compile the cod below without fail. I placed the URL http://schemas.uberdroidstudio.com/theme into my browser's address bar but nothing came up, and I cannot figure out where/ how Eclipse knows the attributes that the namespace supports. Thank you ahead of time for your help.
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:theme="http://schemas.uberdroidstudio.com/theme">
<TextView
android:id="#id/artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0"
theme:textFilter="uppercase" /> <!-- This is the interesting line -->
I suspect that the theme:textFilter="uppercase" line isn't actually having an effect on the (apparently vanilla) TextView.
As for the URL of the namespace, it is interesting that you can't access it, since it does not appear to be a local styleable (Android would have you refer to a local styleable namespace as http://schemas.android.com/apk/res/your.package). +1 for novelty.
The solution is actually not as complicated as I originally thought. XML namespaces are arbitrary strings that just need to be unique. If your namespace does not start with http://schemas.android.com/apk/res then it is not validated and the APK package is not checked for declare-styleable or the like.
Now a custom namespace if very simple to implement, as illustrated by this code snippet in GitHub. Finally, applying custom XML attributes to "normal" widgets can be accomplished by using the LayoutInflater.Factory and calling setFactory before you inflate your views. Throw it all together and you have a highly theme-able, XML driven application like Fede's UberMusic.

Categories

Resources