difference in naming xml between res-auto and com.package.name - android - 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.

Related

Want Relative Layout from the beginning

Novice in Android Studio. Recently started practising basic coding. I want to have relative layout or linear layout from the beginning. Tried but could not make it possible. Could not find any option provided to select the layout theme. Automatically constraint layout getting launched. Please guide..
Change this line:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
To
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
And do check your ending tag changes to this:
</android.widget.RelativeLayout>
Also (optionally) go ahead and delete this line if it's being shown in grey:
xmlns:app="http://schemas.android.com/apk/res-auto"
And also this is an optional change to make in project,
If the constraint layout is not needed in the project remove the following dependency from build.gradle by deleting this line and then doing gradle sync:
compile 'com.android.support.constraint:constraint-layout:......'

Uri is not registered for layout in test resources

I'm new at Android testing and I'm trying to test a custom view in an android library and i'm using Android Studio.
I found here : Android: How to test a custom view? that I should use ActivityUnitTestCase and a mock activity.
I have put my xml layout inside projectname/library/src/androidTest/res/layout and my test activity know my layout R id thanks to this : Configuring res srcDirs for androidTest sourceSet.
My issues is in the layout content.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<eu.custom.customView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
In my case, "http://schemas.android.com/apk/res/android" display in red with an error "URI not registered" and the IDE doe's not recognize my custom view if I add it in this xml.
Is there a way to have this layout working without putting it in production code ?
Thanks
Well, in my Eclipse IDE your code looks correct (there are no red lines) may I suggest you to download the ADT-Bundle again or use Android Studio? I think your IDE is corrupted or something in your settings is wrong. Also check if you have downloaded the up-to-date SDK build tools.

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?

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

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

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