Monday, April 21, 2008

Custom widget adapter based on XML layout

Lex from anddev.org pointed out an annoying property of my custom Weather adapter example: the view row the Weather objects are transformed to are encoded in Java instead of being defined in XML layout resource. This makes the weather display style harder to modify and every application using the WeatherAdapter has the same weather screen layout.

You can download the example program from here.

I created a new version of WeatherAdapter that takes an additional parameter in its constructor, an ID of a composite view (a LinearLayout in our case) that has three children views having the IDs "city", "temperature" and "sky" respectively. The code is flexible enough to handle the case when one or more of these child views are missing, in this case the field will simply not set from the Weather object. Check the res/layout/weather_row.xml file for an example.

The new version is indeed more flexible which is demonstrated by the bit more complicated display of weather rows. Thanks for the feedback, Lex, it really made this example program more valuable.


7 comments:

Damien said...

Hi Gabor

Im my latest blog I link to this article as I had to figure out an adapter. Thanks for the help.

Regards
D.

Unknown said...

Hello,

thanks for this sample, very usefull

one question: how is it possible to get the selected weather, when i click on one, by the method onListItemClick ?

Would be very great if you can help me!

tnx
tom

joshspoon said...

This example is no working for me. import android.view.ViewInflate; doesn't exist. Do you have an update for this post? I'm debugging w/ 1.5.

Gabor Paller said...

joshspoon, the example was written for a pre-1.0 SDK, the API changed since then. I may update it once, now I am busy with something else.

Check out this example, open PageAdapter.java under the src/ directory and search for LayoutInflater.

Trilochan Singh said...

Hi Gabor,

Is it not possible to add custom view in layout without declaring it into xml layout.
like below list_layout.xml code






Above it's my layout now in extended adapter class
I have extended view class which i wanna attach multiple times in single row. in relative layout(rl1)in this case.

ex.
view getView(...){
View v=inflator.inflate(R.layout.list_layout,null);
RelativeLayout rl=(RelativeLayout)v.findViewById(R.id.rl1);
//Now i want to add custom view in this relative layout.Directly without declaring it in xml layout.
like
rl.addView(new CustomView(context));
}
But in this case customView onDraw method is not getting called.

May you please help on this? Why onDraw() method is not getting called when we are adding custom view directly in relative layout on in any layout.
I tried textview or imageview instead custom view they are working damn correct.

Thanks in advance.
TS

Trilochan Singh said...

list_layout.xml didn't show up.

Here it is.

(xml)
(linearlayout android:layout_width="match_parent"
android:layout_height="match_parent"
)
(relativelayout
android:id="@id+/rl1"
android:layout_width="match_parent"
android:layout_height="match_parent)
(/relativelayout)
(/linearlayout)

Unknown said...

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
Android Training in velachery | Android Training in chennai | Android Training in chennai with placement