Android and XML - android

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.

Related

I can't find the documentation for the layout <fragment> element

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.

Use generic layout for top-level ar locale, but region specific locale for others

I'm developing a multilingual app and need to show a different layout for a specific region
My res structure is as follows
res/
layout/
questions.xml
layout-ar-rLY/
questions.xml
When the locale is English (en) the default layout is shown. When the locale is Libyan the Libyan (ar_LY) layout is shown. However, when the locale is Arabic (ar) the Libyan layout is still shown. I need it to show the default layout.
I know that I can simply create a layout-ar directory and copy the questions.xml layout there, however, I am wondering if there is a more elegant way to achieve the right result, so that I don't have to maintain both files.
res/
layout/
questions.xml
layout-ar/
questions.xml
layout-ar-rLY/
questions.xml
The best solution I have come up with so far is for the default and the Arabic layouts both to include a shared sub-layout. Is there a better way to achieve the same result?
Thanks to MH's comment above I had a look at the Android documentation on the official way to do this using Aliases which suggest this.
<?xml version="1.0" encoding="utf-8"?>
<merge>
<include layout="#layout/main_ltr"/>
</merge>
Unfortunately the method given in the documentation doesn't actually work (as detailed in this StackOverflow) answer. Using a merge tag as a root element gives an InflateException error.
I'm sure that the second suggested fix contained in the StackOverflow thread should work; however, the concept of placing Layouts into the resources seems to me to be just as flawed as creating multiple copies (ie from a maintenance perspective you have to remember they are there and not in the layouts folders where you'd expect them to be) so in the end I compromised on the following:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="#layout/questions_default"/>
</FrameLayout>
Which is a bit more verbose than just a merge tag, but at least it works.

Android - XML Accessbility Service won't build

I have written a very simple accessibility service, the code works fine without an xml file defined for metadata but I want to add the xml file for compatibility purposes.
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeNotificationStateChanged"
android:notificationTimeout="100"
android:description=""
/>
However the error I'm getting is none of the 'android:' resources are defined and also I'm not allowed that description. Although the description happens regardless of what I type or in what string asset I prove.
Anyone have any ideas..
Here it says android 4.0 has the option to configure the accessibility service with xml. Are you using 4.0+?
Also it mentions certain configuration options like canRetrieveWindowContent are only available when using the XML configuration. Are you using such options? If not, I don't think you really need the XML configuration.
It needs the xmlns portion....
Thanks for the suggested solutions. Turns out that you need to have your target build at 14 for eclipse to accept that xml file.

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.

Can't get theme attribute references to work in android

I'm very new (second day!) to android and working my way through the dev guide. There, it says that you can reference attributes inside the current theme from your XML by using the form "?...", for example
<?xml version="1.0" encoding="utf-8"?>
<EditText id="text"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:textColor="?android:textDisabledColor"
android:text="#string/hello_world" />
If I try to build this, I just get "No resource found that matches the given name (at 'textColor' with value '?android:textDisabledColor')
Given that this seems pretty basic stuff, and yet I can't find any references to similar problems elsewhere, I must be missing something obvious or misunderstanding something simple. Can anyone tell me what it is?
I don't think textDisabledColor exists anymore - at least not in the newer SDKs which is probably why it can't be found. Try textColorSecondary instead - e.g.
android:textColor="?android:attr/android:textColorSecondary"
Unfortunately I cant figure out if theres a specific disabled color. I came across the site below which seems to define the default theme: See this link for definition of default theme
try android:textColor="?android:attr/textDisabledColor"

Categories

Resources