Using a ListView to create a settings screen in Android?

AndroidListview

Android Problem Overview


I'm developing my first Android application, and I'd like to create a settings screen.

I'd like the screen to have a similar look-and-feel as the native phone-settings screens and the native "create/edit alarm" screen. Thus with different kinds of (statically defined) items vertically stacked with a thin line between them.

How do I define such screen?

I understand I can use the ListView, but this seems to be primarily meant for serving dynamic data using a ListAdapter, where each item is served in the same format. It seems to be possible to create different items (that is, some with checkbox, some with two text-lines, some with an icon) by creating my own ListAdapter and overriding getView but this seems like overkill. Should I be using a ListView for this purpose?

Android Solutions


Solution 1 - Android

There's no need to manually create and format a ListView - there are ways in the API to create Preference screens.

The most obvious is PreferenceActivity.

You can define all your preferences in an XML file, a bit like a layout, and then load them with addPreferencesFromResource() or you can create a number of PreferenceScreen objects in code and populate them with Preference objects that way.

The best thing do would be to look at the API Demos application provided with the Android API. This contains lots of good examples of managing preferences. Here's how it creates preferences from code and here's a sample preferences XML file. There are other examples showing more advanced things like preference dependencies and preference listeners.

Solution 2 - Android

Actually in the built-in alarm application, for edit and create alarms, there are two activities, one for create and one for edit.

The Create Alarm activity is the first one with the digital clock. The Edit Alarm activity is started by clicking on a listed alarm from the Create Alarm activity.

Edit Alarm implements PreferenceActivity, but Create Alarm is more complex (custom cursor adapter to list the alarms).

Have a look at the sources:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomasView Question on Stackoverflow
Solution 1 - AndroidDave WebbView Answer on Stackoverflow
Solution 2 - AndroidtbruyelleView Answer on Stackoverflow