Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView themes

There are lots of way to style ListViews to give them elegant look, but all of them involve modyfying the adapter or writing additional code.

With the release of Android 4.0, unfortunetely things have to change. Google polished their Holo theme and gave it new look. All of the developers are now encouraged to use it, in order to make all apps look the same.

And here's the problem. Google rolled out 4.0, but there are still people using older Android versions. We can't just leave our previous custom application themes and use Holo, because it will ruin visual experience for users with older devices. And we can't force 4.0 users just to use Holo, because let's be honest - it's still not perfect.

The goal is to use builtin themes system and prepare some alternatives for Holo, which will look great on all devices. Then we can just switch between Holo and our themes with just setTheme() and no additional problems. Unfortunetely it's not that simple. We are limited to the capabilities of existing theme system and some things are just hard to do. And here comes my question.

Taking everything I've mentioned into consideration, how can we control ListView look? I'm not able to figure out, how to:

  • create list with rounded corners and make sure the selector background doesn't ruin it when selecting first/last element
  • create rounded corners not for the list but sections separated by headers, something like here: enter image description here

The solution should affect ListViews created by PreferenceActivity without any additional lines of code. Everything should be contained in the theme:

<theme name="SampleTheme" parent="android:Theme">
  ...
</theme>

I kindly ask not to post solutions that do not use styles & themes. They can be easily found in another questions, here on Stack Overflow.

Thanks in advance.

like image 584
Sebastian Nowak Avatar asked May 25 '12 17:05

Sebastian Nowak


People also ask

Is ListView deprecated android?

It's worth to mentioned that the ListView is a kind of deprecated because the RecyclerView was introduced with the API 21 (Android Lollipop).

Which is better ListView or RecyclerView?

Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.

What is an ArrayAdapter in android?

android.widget.ArrayAdapter<T> You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .

What is a ListView in android?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.


1 Answers

I can see two ways to solve this.

One is simply to use a theme for your listviews specifying the background, which in turn is a 9 patch with rounded corners or an xml shape you specify (with rounded corners as well). This will have the side-effect of the listview row selector appearing 'over' the background you specified, therefore kind of spoiling the effect. It is quite straightforward to implement though.

The second option is to simply always add headers and footers to your listviews, which have backgrounds that are selectors with rounded corners on top (and bottom). You can specify styles for these as well if you really want to. Sorry for this last comment, but I had to say it. Please don't try to make your app look like an iPhone app :)

like image 118
Tas Morf Avatar answered Oct 01 '22 15:10

Tas Morf