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<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_sizesFor a string you lot would write:
R.string.app_nameNo 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).ArrayAdapterOr only recall a string array:adapter = ArrayAdapter.createFromResource(this, R.array.font_sizes, android.R.layout.simple_spinner_dropdown_item);
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
Post a Comment