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.
Related
Okay as everyone somehow seems to think I asked how to create a fragment in xml, let me make this absolutley clear, using the attributes shown in the page that I keep ending up on.
Are there any more attributes that can be used in the <fragment> xml element than these?
<fragment
android:name=""
android:id=""
android:layout_weight=""
android:layout_width=""
android:layout_height=""/>
Looking under Android's R.styleable documentation, I see a bunch of Fragment-related values:
android:fragmentAllowEnterTransitionOverlap
android:fragmentAllowReturnTransitionOverlap
android:fragmentEnterTransition
android:fragmentExitTransition
android:fragmentReenterTransition
android:fragmentReturnTransition
android:fragmentSharedElementEnterTransition
android:fragmentSharedElementReturnTransition
android:id
android:name
android:tag
Search for Fragment_ and you'll see them. Unfortunately, I can't test them myself since I'm not near my IDE, but I think they are what you want.
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?
I am new to Android development (just started today). I am familiarising myself with XML which is new to me. I would really appreciate it if people can help me understand this language.
Please see the XML code I am referring to below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_super"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom|withText" />
</menu>
Questions:
1) What is the best way to think of XML code? My understanding is that they are like trees. Should I think of it like the file systems where there are hierarchies etc?
2) I am not sure what the namespace usage is for. For example:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
does this mean that the menu tag contains xmlns:android attribute and a xmlns:yourapp attribute?
3)I would really like to learn how to use XML more proficiently, any links to resources will be much appreciated (I tried w3 but it really was not answering the questions I had.
4) What do the ":" mean/do. The rest of the code I understand. As the API explains it quite clearly but my basic understanding of XML is preventing me from fully understanding the code.
Questions:
1) What is the best way to think of XML code? My understanding is that they are like trees. Should I think of it like the file systems where there are hierarchies etc?
-- no xml is data tags so there are opening and closing tags and self contained flags like :
will discribe your fridge and if you have multiple dairy products and some veggies in the fridge it will look like this
<Fridge>
<Dairy>
<Milk />
<Cheese />
</Dairy>
<Veggies>
<Carrots />
<Pumpkin />
</Veggies>
</Fridge>
2) I am not sure what the namespace usage is for. For example:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
does this mean that the menu tag contains xmlns:android attribute and a xmlns:yourapp attribute?
so the xmlns is xml namespace and they are defined in schemas like a deffinition of what attributes there can be and namespaces combine a bunch of defined attributes like :
<menu xmlns:android ..
android:name=""
android:orderId="5"
/>
the android schema namespace describes all the attributes that android platform knows about and you can then access them but using android: and then the attributes that is known for that menu type
3)I would really like to learn how to use XML more proficiently, any links to resources will be much appreciated (I tried w3 but it really was not answering the questions I had.
the XML in android isn't XML that you would use elsewhere android only uses XML to describe how the screens and menus should look like and behave and the best place to learn more about how android uses xml is to look at developer.android.com and try to look at linear layout and learn from there
4) What do the ":" mean/do. The rest of the code I understand. As the API explains it quite clearly but my basic understanding of XML is preventing me from fully understanding the code.
the : in "that:this" is use this attribute from that namespace and the namespaces are defined with xmlns:NAMEDITWHATEVER="schema location"
NAMEDITWHATEVER:attribute1="what ever this attribute describes"
Yes, you can think of it as a nested structure, much like the tree of a file system
Yes, it means that menu has two of those attributes. For more information about namespaces, reference this post.
Without knowing what questions W3 didn't help you with, it's hard to tell what it is that you're looking for. XML is very straightforward. There isn't more to it than meets the eye.
The colon in foo:bar means that bar is in the foo namespace.
Is it not possible to simplify the name of a custom element in an Activity XML file?
<com.library.CustomView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.library.customview"
android:id="#+id/myView"
app:newAttr="value" />
Say for example I have a CustomView control. Do I always have to put "com.library." in front of "CustomView" or is it possible to use xmlns:custom in a FrameLayout so that I don't need to?
This is what I would like to see (if possible):
<CustomView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.library.customview"
android:id="#+id/myView"
app:newAttr="value" />
No. You must specify the fully qualified name of the custom view class. The custom attribites belong to a different namespace. So you nee to have
http://schemas.android.com/apk/res/[your package name].
That is why you have
xmlns:app="http://schemas.android.com/apk/res/com.example.library.customview"
It is necessary to specify fully qualified name of the custom view and hence
<com.library.CustomView
http://developer.android.com/training/custom-views/create-view.html
The docs does not mention any other solution.
Edit:
There seems to be a workaround as mentioned in the below link. Notice comments on both answers. The authors feels there is a little overhead involved. So its left you to use the below although i recommend you to follow the above method mentioned in the docs.
Using custom Views in XML without using fully-qualified class name
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