[TinyTut] - Arrays as XML-Resources

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[TinyTut] - Arrays as XML-Resources

Postby plusminus » Mon Nov 26, 2007 5:49 pm

[TinyTut] - Arrays as XML-Resources


What you learn: You will learn how to specify Arrays in an XML-Resource and how to read them out in your code.

Difficulty: 1 of 5 :)

Description:
Lets take a look at an practical example, which was used in the AndroidFileBrowser 2.0-Tutorial. There we defined the fileEndings for different file-categories(Audio, Text, Images, ...) in an XML-File to make our Application highly generic :!:

You simply have to create an resource of any name (should and with .xml) in your "res/values/"-Directory.
You can define as many Arrays within each file, as you want with as many items you want. :)
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <array name="fileEndingImage">
  4.         <item>.png</item>
  5.         <item>.gif</item>
  6.         <item>.jpg</item>
  7.         <item>.jpeg</item>
  8.         <item>.bmp</item>
  9.     </array>
  10. <array name="fileEndingAudio">
  11.         <item>.mp3</item>
  12.         <item>.wav</item>
  13.         <item>.ogg</item>
  14.         <item>.midi</item>
  15.     </array>
  16. <array name="fileEndingPackage">
  17.         <item>.jar</item>
  18.         <item>.zip</item>
  19.         <item>.rar</item>
  20.         <item>.gz</item>
  21.     </array>
  22. <array name="fileEndingWebText">
  23.         <item>.htm</item>
  24.         <item>.html</item>
  25.         <item>.php</item>
  26.     </array>
  27. </resources>
Parsed in 0.005 seconds, using GeSHi 1.0.8.4

So that was as easy as one can imagine.

Now we surely want to read those "Text"-Arrays into our application as "Real"-Array o fthe Type String. You can do that with the following single line (also from the FileBrowser-Example):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String[] myImageFileEndings = getResources().getStringArray(R.array.fileEndingImage)
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Thats it. :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Where did you find this information ?

Postby konijn » Fri Nov 30, 2007 2:30 am

Hey there,

I was looking for this on the Android site and couldnt find it,
how did you find out ?

T.
konijn
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Nov 30, 2007 2:26 am

Postby plusminus » Fri Nov 30, 2007 7:46 am

Hello konijn,

hm, I do not really know where I've had seen sth. like that first :? , but I thought it would really be useful for others and I posted it here... :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby androooooid » Mon Feb 02, 2009 8:30 am

I am getting the following error when I load 2000 items in the xml... am I the only one getting this?

Code: Select all
02-01 23:21:39.620: WARN/dalvikvm(5352): ReferenceTable overflow (max=512)
02-01 23:21:39.620: WARN/dalvikvm(5352): Last 10 entries in JNI local reference table:
02-01 23:21:39.620: WARN/dalvikvm(5352):   502: 0x43085990 cls=Ljava/lang/String; (28 bytes)
02-01 23:21:39.620: WARN/dalvikvm(5352):   503: 0x43085a10 cls=Ljava/lang/String; (28 bytes)
02-01 23:21:39.620: WARN/dalvikvm(5352):   504: 0x43085aa0 cls=Ljava/lang/String; (28 bytes)
02-01 23:21:39.620: WARN/dalvikvm(5352):   505: 0x43085b30 cls=Ljava/lang/String; (28 bytes)
02-01 23:21:39.620: WARN/dalvikvm(5352):   506: 0x43085bb0 cls=Ljava/lang/String; (28 bytes)
02-01 23:21:39.630: WARN/dalvikvm(5352):   507: 0x43085c20 cls=Ljava/lang/String; (28 bytes)
02-01 23:21:39.630: WARN/dalvikvm(5352):   508: 0x43085cb8 cls=Ljava/lang/String; (28 bytes)
02-01 23:21:39.630: WARN/dalvikvm(5352):   509: 0x43085d50 cls=Ljava/lang/String; (28 bytes)
02-01 23:21:39.630: WARN/dalvikvm(5352):   510: 0x43085de0 cls=Ljava/lang/String; (28 bytes)
02-01 23:21:39.630: WARN/dalvikvm(5352):   511: 0x43085e68 cls=Ljava/lang/String; (28 bytes)
02-01 23:21:39.640: WARN/dalvikvm(5352): JNI local reference table summary (512 entries):
02-01 23:21:39.640: WARN/dalvikvm(5352):     3 of Ljava/lang/Class; 164B (2 unique)
02-01 23:21:39.640: WARN/dalvikvm(5352):   507 of Ljava/lang/String; 28B (507 unique)
02-01 23:21:39.640: WARN/dalvikvm(5352):     1 of [Ljava/lang/String; 28B
02-01 23:21:39.640: WARN/dalvikvm(5352):     1 of [Ljava/lang/String; 9044B
02-01 23:21:39.640: WARN/dalvikvm(5352): Memory held directly by native code is 23596 bytes
02-01 23:21:39.640: ERROR/dalvikvm(5352): Failed adding to JNI local ref table (has 512 entries)
02-01 23:21:39.640: ERROR/dalvikvm(5352): VM aborting
androooooid
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Sep 26, 2008 8:47 pm

Postby MrSnowflake » Mon Feb 02, 2009 9:29 am

Looks like you can only get 512 items from xml...
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby arocha55 » Mon Feb 16, 2009 8:39 pm

Guys, now what if you wanted to change the colors for those array items? How could you proceed to do that?

I've tried several methods but no luck yet. I'm interested in changing the color inside the XML if possible.

Thanks!
arocha55
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Jan 27, 2009 12:53 am

Top

Re: [TinyTut] - Arrays as XML-Resources

Postby midtoad » Sun Aug 23, 2009 5:34 am

[quote="plusminus"]
[TinyTut] - Arrays as XML-Resources


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String[] myImageFileEndings = getResources().getStringArray(R.array.fileEndingImage)
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


Super BIG thanks for that! I've been searching around for this construct and couldn't find it anywhere. All of the examples dwell on creating a spinner from an array of strings in an .xml file. But I want to just pull the items into an array and work with them. Your snippet shows me how to do exactly that!

So, thanks again!

cheers
midtoad
midtoad
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Aug 12, 2009 5:53 am

Postby Mats » Sun Nov 01, 2009 3:45 pm

MrSnowflake wrote:Looks like you can only get 512 items from xml...


Is there a solution for this?
Mats
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Nov 01, 2009 3:44 pm

Postby Jadis » Thu Nov 12, 2009 6:37 pm

Hey guys I know this is really basic, but in the XML layout, how would I display the content of an array that I made in the class file ? Textview is ok with me, but I don't know what command to use.
Jadis
Freshman
Freshman
 
Posts: 7
Joined: Thu Oct 29, 2009 9:11 pm

Postby iamgeef » Mon Dec 21, 2009 4:12 pm

Hi,

Is it possible to do this but instead of creating the array from an xml file in the Resources folder, it is created from xml data pulled from an external source?

Cheers

Geef
iamgeef
Freshman
Freshman
 
Posts: 2
Joined: Fri Nov 27, 2009 2:42 pm

Postby vadimz » Tue Feb 16, 2010 4:44 pm

Thanks!
Is there any way to get values of parameters in XML?
They are code and icon in my sample:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <string-array name="languages_array">
  2.                 <item code="af" icon="icon">AFRIKAANS</item>
  3.                 <item code="sq" icon="icon">ALBANIAN</item>
  4.                 <item code="am" icon="icon">AMHARIC</item>
  5.                 <item code="ar" icon="icon">ARABIC</item>
  6.                 <item code="hy" icon="icon">ARMENIAN</item>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
vadimz
Developer
Developer
 
Posts: 44
Joined: Thu Jul 30, 2009 5:04 pm
Location: Moldova

Advanced XML

Postby rezvani63 » Wed Feb 17, 2010 6:11 am

Good topic,
But how do we can use xml resources like the below one?

<book>
<chapter index="1" name="first one">
<aya index="1" text="second one" />
<aya index="2" text="third one" />
</chapter>
</book>

Thanks in advance
rezvani63
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 17, 2010 5:58 am

Postby bonzaster » Mon Mar 29, 2010 8:37 pm

Hi!

I have string array like this
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <resources>
  2.  
  3.         <string-array name="colors">
  4.  
  5.                 <item>#FF0000</item>
  6.  
  7.                 <item>#FF8000</item>
  8.  
  9.                 <item>#008000</item>
  10.  
  11.                 <item>#0000FF</item>
  12.  
  13.                 <item>#8000FF</item>
  14.  
  15.         </string-array>
  16.  
  17. </resources>
  18.  
  19.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


so I can get value of array item by its index in code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  getResources().getStringArray(R.array.colors)[index]
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Is there any way to get that value from other resource file?
bonzaster
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Mar 29, 2010 8:26 pm

Re: [TinyTut] - Arrays as XML-Resources

Postby Marcubus » Mon Aug 30, 2010 2:29 pm

Hey, I'm actually pretty new to Android dev, but I'm having a problem very similar to this. I was wondering...

Is is possible to create a dynamic array in XML, like say I want to create an array of undefined size, that I want to fill with Absolute Layouts. I want to declare this array in the XML, and define the layout params for the first AbsoluteLayout in XML as well. Then when the program runs I want to be able to add any number of items to this array, all with the same layoutParams as the first AbsoluteLayout I added before. Is this even possible?

The main issue I'm encountering is that when I create an Absolute layout in just Java code, I can't modify the layoutParams because whenever I getLayoutParams() in the absolute layout, it returns null. The only way I can get the LayoutParams is when I define the view in XML first.

I'm trying to create a dynamic menu that I can add absolute layouts to as the program runs. It needs to be an absolute layout so I can directly control the x,y position of all the children views within the layout.

I just don't know how to define this in XML. I know it's a big question, and any tips you could give would be greatly appreciated.

Cheers,
-Marc
Marcubus
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Aug 30, 2010 2:13 pm

Arrays as XML-Resources

Postby droidoine » Mon Mar 26, 2012 8:12 am

Hey, I have the same question as Vadimz. Maybe anyone knows an answer? Would appreciate. Thx!
droidoine
Freshman
Freshman
 
Posts: 2
Joined: Mon Mar 19, 2012 10:35 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests