[Android Apps and Games] daxgirl:
ListPreference with Image thumbnails

So this is the deal:

I have created a new preference class for handling list preference with image thumbnails. It's working right now and I will post what to add and where.

1. Download the attached files

2. Put the java class inside the prefs package and put the layout files inside the layout folder in res.

3. In android studio open a file called attr.xml inside values folder and add the following (in bold blue):

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ScrimInsetsView">
<attr name="insetForeground" format="reference|color" />
</declare-styleable>
<declare-styleable name="NavDrawerIcon">
<attr name="tint" format="reference|color" />
</declare-styleable>
[COLOR="Blue"][B] <declare-styleable name="ThumbnailListPreference">
<attr name="drawableArray" format="reference" />
<attr name="entryList" format="reference" />
<attr name="entryValuesList" format="reference" />
<attr name="entryDefault" format="string" />
</declare-styleable>[/B][/COLOR]
<attr name="nav_drawer_bg_color" format="color" />
<attr name="myAccentColor" format="color" />
<attr name="nav_item_color_selector" format="reference|color" />
<attr name="nav_drawer_header" format="reference" />
</resources>

4. In order to create the prefernce you need to:

a. Create the necessary arrays: 2 string arrays and one drawable array. I recomment opening a new resource file for that. F.e. inside values folder create a new resource files called arrays.xml. Inside of the file you will manage all your arrays. Here is an example from a working app right now based on [MENTION=1042140]tdunham[/MENTION] images he uploaded

<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="pulldown_drawables">
<item>@drawable/pulldown1</item>
<item>@drawable/pulldown2</item>
<item>@drawable/pulldown3</item>
<item>@drawable/pulldown4</item>
<item>@drawable/pulldown5</item>
<item>@drawable/pulldown6</item>
<item>@drawable/pulldown7</item>
<item>@drawable/pulldown8</item>
<item>@drawable/pulldown9</item>
<item>@drawable/pulldown10</item>
</array>
<string-array name="pulldown_entries">
<item>Image 1</item>
<item>Image 2</item>
<item>Image 3</item>
<item>Image 4</item>
<item>Image 5</item>
<item>Image 6</item>
<item>Image 7</item>
<item>Image 8</item>
<item>Image 9</item>
<item>Image 10</item>
</string-array>
<string-array name="pulldown_values">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
</string-array>
</resources>

Note that the array of images is a regular array, not integer array, not string array, Just an array. Whereas the arrays for entries and values are string-array. You need to have your images imported into the drawable folder and your arrays created before we start creating the preference.

b. Now open HandlePreferenceFragment and let's make this thing a part of our conspiracy.

Navigate to the method initAllKeys() and at the end of it add the following (in bold blue):

private void initAllKeys() {
Map<String, ?> keys = pm.getSharedPreferences().getAll();

for (Map.Entry<String, ?> entry : keys.entrySet()) {
.....
.....
.....
if (p instanceof MyEditTextPreference) {
if (actualString == null) {
Settings.System.putString(cr, key, prefString);
}
if (!prefString.equals(t)) {
Toast.makeText(c, t + "/" + prefString, Toast.LENGTH_SHORT).show();

ed.putString(key, t).commit();
}
MyEditTextPreference et = (MyEditTextPreference) pf.findPreference(key);
et.setSummary(t);
}

[COLOR="Blue"][B] if(p instanceof ThumbnailListPreference) {
if(actualString == null) {
Settings.System.putString(cr, key, prefString);
} else if(!prefString.equals(t)){
ed.putString(key, t).apply();
}
}[/B][/COLOR]
}
}

}

Now navigate to method called updateDatabase(...) and add the following statement (in bold blue):

private void updateDatabase(String key, Object o, SharedPreferences sp) {
boolean isEnabled;
int dbInt;
String value = "";

if (o instanceof SwitchPreference || o instanceof CheckBoxPreference) {
isEnabled = sp.getBoolean(key, true);
dbInt = (isEnabled) ? 1 : 0;
Settings.System.putInt(cr, key, dbInt);
} else if (o instanceof MyEditTextPreference || o instanceof MyListPreference || o instanceof IntentDialogPreference [COLOR="Blue"][B]|| o instanceof ThumbnailListPreference[/B][/COLOR]) {
value = sp.getString(key, "");
Settings.System.putString(cr, key, value);
} else if (o instanceof ColorPickerPreference) {
dbInt = sp.getInt(key, Color.WHITE);
Settings.System.putInt(cr, key, dbInt);
} else if (o instanceof SeekBarPreference) {
dbInt = sp.getInt(key, 0);
Settings.System.putInt(cr, key, dbInt);
}
}

So we're all set to go and we can create our preferences. Navigate to the xml folder and find the preference file in which you wish to add the preference. We will build the preference like this:

<com.wubydax.romcontrol.prefs.ThumbnailListPreference
android:key="pulldown_image"
android:title="Pulldown Image"
[COLOR="Green"][B] app:drawableArray="@array/pulldown_drawables"
app:entryDefault="@string/defaultValue"
app:entryList="@array/pulldown_entries"
app:entryValuesList="@array/pulldown_values" [/B][/COLOR]/>

Please play close attention to the fields marked in bold green color. Those are custom attributes that are specific to this preference class. You cannot use any other attributes like you use for regular list preference. I have built this class from scratch and it only uses general android attributes such as title, summary, icon and key. YOU HAVE TO SPECIFY FIELDS IN GREEN THE WAY I SHOWED!!!!!!!

Provided you followed the instructions to the dot you get this beauty:

Now, this is a new class that was just created last night. It needs testing and error handling (managing exceptions and so on... programming stuff). We will officially add it to the repo once we have it perfected. But it's rather safe to use already.

Note, the image format inside the image views is 1x1.5 width/height ratio. which is for full screen thumbnails. If you need to show previews of square size, just edit the ratios in thumbnail_item_view.xml and thumbnail_preference_icon.xml inside layout folder.

Best of luck and let us know your thoughts and ideas. But mostly bugs and errors.

P.S. please don't post your logcat as dumpstate_app_error.txt.gz or if you do, just copy past the error entry. Android Studio does superb job creating log entries specific for the running apps. Just copy the error to the reply and show us your code.