aspose file tools*
The moose likes Android and the fly likes styling items of a ListView Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "styling items of a ListView" Watch "styling items of a ListView" New topic
Author

styling items of a ListView

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
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?


Ping & DNS - my free Android networking tools app
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

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.



[List of FAQs] | [Android FAQ] | [Samuh Varta]
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Something like:


Note: getView() code is not optimized but it conveys what I am trying to say
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
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

Joined: Oct 12, 2008
Posts: 1369
    
    1

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
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
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

Joined: Oct 12, 2008
Posts: 1369
    
    1

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

Joined: Jan 07, 2009
Posts: 629

[ UD: Removed hijack. Continue that discussion in one of the various other threads that you have started for this subject. ]


http://www.androcid.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: styling items of a ListView