Configurable custom elements possible?

Tutorials with advanced 'difficulty' and more Lines of Code.

Configurable custom elements possible?

Postby aracos » Tue Feb 02, 2010 6:19 pm

Hi All,

Is there a way to define custom elements that are configurable within resource files and possible to store in arrays?

Maybe there is a better way to do this so here is what I want to do:
I want to define elements which I can store within an array to display, i.e. in a customized list view /adapter.
I want to be able to define the order within an array in an xml file and define the id of the elements as well as some text and optionally a drawable. Than I want to display them within a list view. When I select one of these elements I want the ID defined for this element to be the one defined by me.

I would like something like this but I am unable to find a way to get there, any ideas?

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <!-- within values -->
  3.  
  4. <array name="myArray">
  5.  
  6.   <item type="myElement" android:id="@id/elem_1" android:text="some text" android:drawable="@drawable/img_1 />
  7.  
  8.   <item type="myElement" android:id="@id/elem_2" android:text="some text" android:drawable="@drawable/img_2 />
  9.  
  10.   <item type="myElement" android:id="@id/elem_2" android:text="some text" android:drawable="@drawable/img_2 />
  11.  
  12. </array>
  13.  
  14.  
  15.  
  16. <!-- whithin my layout only extra elements listed -->
  17.  
  18. <MyListView listElements="@array/myArray />
  19.  
  20.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
aracos
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 02, 2010 1:14 pm

Top

Postby MichaelEGR » Tue Feb 02, 2010 8:51 pm

First take a look at the "android:entries" tag in ListView. It will take an array from values resources. It may only take a string array though and not use all the additional images as you defined in your XML. I'm mentioning this without trying it.

If the above doesn't work quite exactly how you want then you have to write your own custom LayoutInflater.Factory which is not too hard. You can define and handle the XML yourself.

http://developer.android.com/reference/ ... ctory.html
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Postby aracos » Wed Feb 03, 2010 9:07 am

Hi MichaelEGR,

thank you for your quick reply.
You are right, the List view only takes string-arrays within the android:entries tag, it reads them from a typed array from the entries.

I am able to define my own entry tag and insert my own list. What I can't do, is read my defined array again from the typed array I get from context.obtainStyledAttributes(attrs, R.styleable.MyListView);

I just figured out that i can get the TypedArray from the resource with getResources().obtainTypedArray(resId);
I let you know how that works out.
aracos
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 02, 2010 1:14 pm

Postby aracos » Wed Feb 03, 2010 2:29 pm

Hi,

as promissed, here the update on my research.

I could not figure out, how to define multple values within one resource element. Thats why I now use different lists with the same size to configure my adapter.

this one knows how to extract the information and apply them to my object.
aracos
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 02, 2010 1:14 pm

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests