Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

styling items of a ListView

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to apply styles (bold, specifically) to selected items in a ListView. To that end, I've assigned the android:bufferType="spannable" attribute to its individual TextView elements, so that the code can obtain a Spannable like this:

I was under the impression that this should be sufficient to style the elements; at least that's how I read http://developer.android.com/guide/appendix/faq/commontasks.html#selectingtext. But neither the emulator nor an actual device show any styles; is there something else that needs to be done?
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since, you are using ArrayAdapter class directly I am not sure of how you would control the formatting of the individual TextView; have not tried anything like this before.

Usually, I would extend an appropriate Adapter class(e.g.: BaseAdapter) and override all its methods to cater to my needs. I would've put the following code inside getView() method of my adapter, which is called by the framework once for every row to be drawn, and applied these styles conditionally there.

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like:


Note: getView() code is not optimized but it conveys what I am trying to say
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked like a charm with just a few minor tweaks. Kinda cool that it's possible to style each row of the list individually. Not that one would (or should) do that, but it's nice to know that it's possible :-)

Thanks very much!
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:That worked like a charm with just a few minor tweaks. Kinda cool that it's possible to style each row of the list individually. Not that one would (or should) do that, but it's nice to know that it's possible :-)

Thanks very much!

Glad to know that I could be of any help.

If you are working with ListView, I would suggest going through the video of Romain Guy's "Turbo Charge your UI" talk he delivered during Google I/O conference in May2009.
It is available on YouTube and is about an hour long but worth the time.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an interesting and useful talk. So I guess instead of

one should really use this for performance reasons, since inflating a View from the XML file is a comparatively costly operation:
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that way, recycled views are used for newer rows instead a new View Object, for every row to be drawn.(Total number of View objects created would be number of rows visible on the screen at a time)

There is still one more optimization you can apply here using the ViewHolder pattern. This would reduce the findViewById(..) calls which are considered expensive too.(Note that this would affect the performance had the layout of your row been a little more "dense" or complex and also if the number of rows in the list were high).

Here is the pattern:

 
chetan dhumane
Ranch Hand
Posts: 641
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ UD: Removed hijack. Continue that discussion in one of the various other threads that you have started for this subject. ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic