Android: Adding As Well As Referencing An Xml Resource


Adding an XML resource

Inside your res folder is a values folder you lot tin transportation away right-click on this in addition to practise a New > Values resources file. Inside this, for the utilization of creating a elementary listing of strings, nosotros tin transportation away only place tags in addition to strings inward an identical means to that inward which nosotros honor inward the strings.xml file:
<resources><string name="app_name">DraggingView</string></resources> 
If nosotros desire to practise an array however, whether that last an integer or string array nosotros must kickoff position the array every bit such yesteryear additional degree of nesting:
<resources>     <string-array name="font_sizes">         <item value="10">"10"</item>         <item value="15">"15"</item>         <item value="20">"20"</item>     </string-array> </resources> 

Referencing the XML resource

To in addition to then reference that resources inside your coffee code, nosotros tin transportation away write simply:
R.array.font_sizes 
For a string you lot would write:
R.string.app_name 
No notice is taken of the filename when references are made exclusively of the type contained inside them. While the resources tin transportation away last inward dissever files, they are treated every bit if all values are contained inward i unmarried file, so no postulate to position filenames.

When this volition last useful

This volition assay out useful, where for event you lot mightiness desire to practise an array adapter for a spinner (more on this inward the side yesteryear side Android post).
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.font_sizes, android.R.layout.simple_spinner_dropdown_item); 
Or only recall a string array:
String[] myStringArray = getResources().getStringArray(R.array.font_sizes); 

Where next?

Sticking amongst the subject of XML, I'll last looking at toolbars in addition to their carte du jour items next time (not exclusively the designated activity bar but also secondary toolbars).

Comments

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?