/Android-ImageListPreference

ImageListPreference for android to create per item image list in preferences. It's a library project. It's been updated to support latest Android versions, Old version can be found here: https://github.com/Proficient-Apps/Android-ImageListPreference

Primary LanguageJava

ImageListPreference

Based on ImageListPreference guide at :

'http://www.cmwmobile.com/index.php?option=com_content&view=article&id=4&Itemid=12'.

ImageListPreference class by Arnab Jain. Old version can be found here: https://github.com/Proficient-Apps/Android-ImageListPreference

Features

  • Image/Icon for each list item
  • Option to set Rounded Images [Circle shape]
  • Can be created in xml and as well as in Java
  • Bitmap or Drawable can be used for images when created using Java
  • Exception handling present along with log inputs for developers to fix issues while using the Preference in their application

Requirements

Tested with APIv28, but will work from APIv21 onwards. For older APIs use: https://github.com/Proficient-Apps/Android-ImageListPreference

Installation

Add "maven { url 'https://jitpack.io' }" (without quotes) to your root build.gradle file like this:

allprojects {
        repositories {
                ...
                maven { url 'https://jitpack.io' }
        }
}

In your applicationModule(app)/build.gradle add the ImageListPreference library as a dependency:

dependencies {
  ...
  implementation 'com.github.arnabJ:Android-ImageListPreference:1.0.1'
}

Sync project, clean and build. You can use the ImageListPreference library as part of your project now.

Usage

xml

/res/xml/preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"> <!-- You need to declare an application name space. -->

    <tk.arnabjportfolio.preference.imagelist.ImageListPreference
        android:key="pref_key_image_list_preference_1"
        android:title="@string/test_image_list_preference"
        android:dialogTitle="@string/test_image_list_preference"
        android:entries="@array/entries"
        android:entryValues="@array/entryValues"
        app:entryImages="@array/entryImages" <!-- This is an array containing references to the image drawables. -->
        app:roundedImage="false" /> <!-- 'COMPULSORY' | Set to false if you don't want rounded images else set to true. -->

    <tk.arnabjportfolio.preference.imagelist.ImageListPreference
        android:key="pref_key_image_list_preference_2"
        android:title="@string/test_image_list_preference_2"
        android:dialogTitle="@string/test_image_list_preference_2"
        android:entries="@array/entries"
        android:entryValues="@array/entryValues"
        app:entryImages="@array/entryImages"
        app:roundedImage="true" /> <!-- Images will be converted to Circular Shape. -->

</PreferenceScreen>

/res/values/arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="entries">
        <item>Item 1</item>
        <item>Item 2</item>
        <item>Item 3</item>
        <item>Item 4</item>
        <item>Item 5</item>
    </string-array>

    <string-array name="entryValues">
        <item>0</item>
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
    </string-array>

    <array name="entryImages">
        <item>@drawable/image_item_1</item>
        <item>@drawable/image_item_2</item>
        <item>@drawable/image_item_3</item>
        <item>@drawable/image_item_4</item>
        <item>@drawable/image_item_5</item>
    </array>
</resources>

Java

Import ImageListPreference:

import tk.arnabjportfolio.preference.imagelist.ImageListPreference;

Inside onCreate(...) { ... } of your PreferenceActivity/PreferenceFragment

Get the base PreferenceScreen to which you want to add the Dynamically created ImageListPreferences:

PreferenceScreen prefScreen = this.getPreferenceScreen();
/*
 * Alternatively you can do,
 * PreferenceScreen prefScreen = (PreferenceScreen) findPreference("pref_screen_key");
 */

Declare 2 String arrays and 1 Drawable/Bitmap array for entries, entryValues and entryImages respectively:

Resources res = getResources();
String[] entries = new String[] {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
String[] entryValues = new String[] {"0", "1", "2", "3", "4"};
Drawable[] entryImages = new Drawable[] {res.getDrawable(R.drawable.image_item_1),
              res.getDrawable(R.drawable.image_item_2), res.getDrawable(R.drawable.image_item_3),
              res.getDrawable(R.drawable.image_item_4), res.getDrawable(R.drawable.image_item_5)};

Than create ImageListPreference object and add title, key and other requrired details:

ImageListPreference imgListPref1 = new ImageListPreference(getActivity());
imgListPref1.setKey("pref_key_dynamic_image_list_pref_1");
imgListPref1.setTitle("Dynamic Image List Preference 1");
imgListPref1.setDialogTitle("Dynamic Image List Preference 1");
imgListPref1.setEntries(entries);
imgListPref1.setEntryValues(entryValues);
/*
 * Pass the Drawable/Bitmap Array to setEntryImages() Method,
 * the second parameter requires boolean value true or false for setting roundedImage.
 * false = images will be added as is.
 * true = images will be added in circular shape.
 */
imgListPref1.setEntryImages(entryImages, false);
prefScreen.addPreference(imgListPref1);

Screens

Credits

  • CMWmobile.com[http://www.cmwmobile.com/] for guide on how to start with ImageListPreference
  • attenzione [Github user] for his ColorPickerPreference README.rst. Used it as base for this README.rst