Squashed 'AndroidSliderPreference/' content from commit 05b2441
git-subtree-dir: AndroidSliderPreference git-subtree-split: 05b244145dd2512e0cbe7e2395149f2be303aca1
This commit is contained in:
commit
4250af2cc1
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="src" path="gen"/>
|
||||||
|
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||||
|
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
|
||||||
|
<classpathentry kind="output" path="bin/classes"/>
|
||||||
|
</classpath>
|
|
@ -0,0 +1,2 @@
|
||||||
|
# Auto detect text files and perform LF normalization
|
||||||
|
* text=auto
|
|
@ -0,0 +1,3 @@
|
||||||
|
/.settings
|
||||||
|
/bin
|
||||||
|
/gen
|
|
@ -0,0 +1,33 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>SliderPreference</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
|
@ -0,0 +1,16 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
Copyright 2012 Jay Weisskopf
|
||||||
|
|
||||||
|
Licensed under the MIT License (see LICENSE.txt)
|
||||||
|
-->
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="net.jayschwa.android.preference"
|
||||||
|
android:versionCode="1"
|
||||||
|
android:versionName="1.0" >
|
||||||
|
|
||||||
|
<uses-sdk
|
||||||
|
android:minSdkVersion="1"
|
||||||
|
android:targetSdkVersion="17" />
|
||||||
|
|
||||||
|
</manifest>
|
|
@ -0,0 +1,7 @@
|
||||||
|
Copyright 2012 Jay Weisskopf
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,104 @@
|
||||||
|
Android Slider Preference Library
|
||||||
|
=================================
|
||||||
|
|
||||||
|
![Screenshot](https://raw.github.com/jayschwa/AndroidSliderPreference/master/screenshot.png)
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
* Slider represents a `float` between `0.0` and `1.0`
|
||||||
|
* Access with `SliderPreference.getValue()` or [`SharedPreferences.getFloat()`][shar]
|
||||||
|
* Supports multiple summaries (e.g. "Low", "Medium", "High") and selects one based on the slider's position
|
||||||
|
* Java: `SliderPreference.setSummary(CharSequence[] summaries)`
|
||||||
|
* XML: `android:summary="@array/string_array_of_summaries"`
|
||||||
|
* A single `String` still works too
|
||||||
|
* Subclass of [`DialogPreference`][diag]
|
||||||
|
* Supports all [dialog-specific attributes][datr] such as `android:dialogMessage`
|
||||||
|
* Visually-consistent with Android's built-in preferences
|
||||||
|
* Less error-prone than displaying the slider directly on the settings screen
|
||||||
|
* [MIT License](#license)
|
||||||
|
|
||||||
|
[datr]: https://developer.android.com/reference/android/preference/DialogPreference.html#lattrs "DialogPreference attributes"
|
||||||
|
[diag]: https://developer.android.com/reference/android/preference/DialogPreference.html "DialogPreference"
|
||||||
|
[shar]: https://developer.android.com/reference/android/content/SharedPreferences.html#getFloat(java.lang.String,%20float) "getFloat()"
|
||||||
|
|
||||||
|
## How To Use
|
||||||
|
|
||||||
|
### Add the library to your application
|
||||||
|
|
||||||
|
Before you can add a `SliderPreference` to your application, you must first add a library reference:
|
||||||
|
|
||||||
|
1. Clone or download a copy of the library
|
||||||
|
2. Import the library into Eclipse: File menu -> Import -> Existing Project into Workspace
|
||||||
|
3. Open your application's project properties and [add a library reference][ref] to "SliderPreference"
|
||||||
|
|
||||||
|
[ref]: https://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject
|
||||||
|
|
||||||
|
### Add a slider to your application
|
||||||
|
|
||||||
|
``` XML
|
||||||
|
<!-- preferences.xml -->
|
||||||
|
<net.jayschwa.android.preference.SliderPreference
|
||||||
|
android:key="my_slider"
|
||||||
|
android:title="@string/slider_title"
|
||||||
|
android:summary="@array/slider_summaries"
|
||||||
|
android:defaultValue="@string/slider_default"
|
||||||
|
android:dialogMessage="@string/slider_message" />
|
||||||
|
```
|
||||||
|
``` XML
|
||||||
|
<!-- strings.xml -->
|
||||||
|
<string name="slider_title">Temperature</string>
|
||||||
|
<string-array name="slider_summaries">
|
||||||
|
<!-- You can define as many summaries as you'd like -->
|
||||||
|
<!-- The active summary will reflect the preference's current value -->
|
||||||
|
<item>Freezing</item> <!-- 0.00 to 0.25 -->
|
||||||
|
<item>Chilly</item> <!-- 0.25 to 0.50 -->
|
||||||
|
<item>Warm</item> <!-- 0.50 to 0.75 -->
|
||||||
|
<item>Boiling</item> <!-- 0.75 to 1.00 -->
|
||||||
|
</string-array>
|
||||||
|
<item name="slider_default" format="float" type="string">0.5</item>
|
||||||
|
<string name="slider_message">Optional message displayed in the dialog above the slider</string>
|
||||||
|
```
|
||||||
|
|
||||||
|
It is possible to define the default value directly in the attribute. The summary can also be a regular string, instead of a string array:
|
||||||
|
|
||||||
|
``` XML
|
||||||
|
<net.jayschwa.android.preference.SliderPreference
|
||||||
|
android:summary="This summary is static and boring"
|
||||||
|
android:defaultValue="0.5" />
|
||||||
|
```
|
||||||
|
|
||||||
|
## Background
|
||||||
|
|
||||||
|
Sliders are recommended by Android's official [design documentation][ptrn] for specific types of settings:
|
||||||
|
|
||||||
|
> Use this pattern for a setting where the range of values are not discrete and fall along a continuum.
|
||||||
|
>
|
||||||
|
> ![Slider design pattern example](https://developer.android.com/design/media/settings_slider.png)
|
||||||
|
|
||||||
|
Despite this recommendation, the Android SDK does not actually provide a [`Preference`][pref] with slider functionality. Various custom implementations can be found around the web, but many have issues such as:
|
||||||
|
|
||||||
|
* The slider is displayed directly on the settings screen
|
||||||
|
* Higher chance of accidental clicks
|
||||||
|
* No way to confirm or cancel potential changes
|
||||||
|
* Discrete values are displayed to the user
|
||||||
|
* Not ideal for this design pattern
|
||||||
|
|
||||||
|
This library aims to be as consistent as possible with the design pattern and Android's built-in [`Preference`][pref] implementations.
|
||||||
|
|
||||||
|
[ptrn]: https://developer.android.com/design/patterns/settings.html#patterns "Settings Design Patterns"
|
||||||
|
[pref]: https://developer.android.com/reference/android/preference/Preference.html "Preference"
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This library is licensed under the [MIT License][mit]. A copy of the license is provided in [LICENSE.txt][copy]:
|
||||||
|
|
||||||
|
> Copyright 2012 Jay Weisskopf
|
||||||
|
>
|
||||||
|
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
>
|
||||||
|
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
>
|
||||||
|
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
[copy]: https://raw.github.com/jayschwa/AndroidSliderPreference/master/LICENSE.txt
|
||||||
|
[mit]: http://opensource.org/licenses/MIT "Open Source Initiative - The MIT License"
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="src" path="gen"/>
|
||||||
|
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||||
|
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
|
||||||
|
<classpathentry kind="output" path="bin/classes"/>
|
||||||
|
</classpath>
|
|
@ -0,0 +1,3 @@
|
||||||
|
/.settings
|
||||||
|
/bin
|
||||||
|
/gen
|
|
@ -0,0 +1,33 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>SliderPreferenceExample</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="net.jayschwa.android.preference.SliderPreferenceExample"
|
||||||
|
android:versionCode="1"
|
||||||
|
android:versionName="1.0" >
|
||||||
|
|
||||||
|
<uses-sdk
|
||||||
|
android:minSdkVersion="1"
|
||||||
|
android:targetSdkVersion="17" />
|
||||||
|
|
||||||
|
<application
|
||||||
|
android:allowBackup="false"
|
||||||
|
android:label="@string/app_name" >
|
||||||
|
<activity
|
||||||
|
android:name=".ExampleActivity"
|
||||||
|
android:label="@string/app_name" >
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
|
</application>
|
||||||
|
|
||||||
|
</manifest>
|
|
@ -0,0 +1,20 @@
|
||||||
|
# To enable ProGuard in your project, edit project.properties
|
||||||
|
# to define the proguard.config property as described in that file.
|
||||||
|
#
|
||||||
|
# Add project specific ProGuard rules here.
|
||||||
|
# By default, the flags in this file are appended to flags specified
|
||||||
|
# in ${sdk.dir}/tools/proguard/proguard-android.txt
|
||||||
|
# You can edit the include path and order by changing the ProGuard
|
||||||
|
# include property in project.properties.
|
||||||
|
#
|
||||||
|
# For more details, see
|
||||||
|
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||||
|
|
||||||
|
# Add any project specific keep options here:
|
||||||
|
|
||||||
|
# If your project uses WebView with JS, uncomment the following
|
||||||
|
# and specify the fully qualified class name to the JavaScript interface
|
||||||
|
# class:
|
||||||
|
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||||
|
# public *;
|
||||||
|
#}
|
|
@ -0,0 +1,15 @@
|
||||||
|
# This file is automatically generated by Android Tools.
|
||||||
|
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||||
|
#
|
||||||
|
# This file must be checked in Version Control Systems.
|
||||||
|
#
|
||||||
|
# To customize properties used by the Ant build system edit
|
||||||
|
# "ant.properties", and override values to adapt the script to your
|
||||||
|
# project structure.
|
||||||
|
#
|
||||||
|
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
|
||||||
|
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
|
||||||
|
|
||||||
|
# Project target.
|
||||||
|
target=android-17
|
||||||
|
android.library.reference.1=..
|
|
@ -0,0 +1,17 @@
|
||||||
|
<resources>
|
||||||
|
|
||||||
|
<string name="app_name">Slider Preference Example</string>
|
||||||
|
<string name="slider_title">Basic Slider</string>
|
||||||
|
<string name="slider_message">Optional message is defined in android:dialogMessage.</string>
|
||||||
|
|
||||||
|
<item name="slider_default" format="float" type="string">0.5</item>
|
||||||
|
|
||||||
|
<string-array name="slider_summaries">
|
||||||
|
<item>Low</item>
|
||||||
|
<item>Medium-Low</item>
|
||||||
|
<item>Medium</item>
|
||||||
|
<item>Medium-High</item>
|
||||||
|
<item>High</item>
|
||||||
|
</string-array>
|
||||||
|
|
||||||
|
</resources>
|
|
@ -0,0 +1,10 @@
|
||||||
|
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||||
|
|
||||||
|
<net.jayschwa.android.preference.SliderPreference
|
||||||
|
android:defaultValue="@string/slider_default"
|
||||||
|
android:dialogMessage="@string/slider_message"
|
||||||
|
android:key="basic_slider"
|
||||||
|
android:summary="@array/slider_summaries"
|
||||||
|
android:title="@string/slider_title" />
|
||||||
|
|
||||||
|
</PreferenceScreen>
|
|
@ -0,0 +1,29 @@
|
||||||
|
package net.jayschwa.android.preference.SliderPreferenceExample;
|
||||||
|
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.preference.PreferenceActivity;
|
||||||
|
|
||||||
|
// Deprecated PreferenceActivity methods are used for API Level 10 (and lower) compatibility
|
||||||
|
// https://developer.android.com/guide/topics/ui/settings.html#Overview
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
public class ExampleActivity extends PreferenceActivity implements
|
||||||
|
SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
|
|
||||||
|
protected void onCreate(Bundle icicle) {
|
||||||
|
super.onCreate(icicle);
|
||||||
|
addPreferencesFromResource(R.xml.preferences);
|
||||||
|
getPreferenceManager().getSharedPreferences()
|
||||||
|
.registerOnSharedPreferenceChangeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void onDestroy() {
|
||||||
|
getPreferenceManager().getSharedPreferences()
|
||||||
|
.unregisterOnSharedPreferenceChangeListener(this);
|
||||||
|
super.onDestroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
|
||||||
|
String key) {
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
# To enable ProGuard in your project, edit project.properties
|
||||||
|
# to define the proguard.config property as described in that file.
|
||||||
|
#
|
||||||
|
# Add project specific ProGuard rules here.
|
||||||
|
# By default, the flags in this file are appended to flags specified
|
||||||
|
# in ${sdk.dir}/tools/proguard/proguard-android.txt
|
||||||
|
# You can edit the include path and order by changing the ProGuard
|
||||||
|
# include property in project.properties.
|
||||||
|
#
|
||||||
|
# For more details, see
|
||||||
|
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||||
|
|
||||||
|
# Add any project specific keep options here:
|
||||||
|
|
||||||
|
# If your project uses WebView with JS, uncomment the following
|
||||||
|
# and specify the fully qualified class name to the JavaScript interface
|
||||||
|
# class:
|
||||||
|
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||||
|
# public *;
|
||||||
|
#}
|
|
@ -0,0 +1,15 @@
|
||||||
|
# This file is automatically generated by Android Tools.
|
||||||
|
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||||
|
#
|
||||||
|
# This file must be checked in Version Control Systems.
|
||||||
|
#
|
||||||
|
# To customize properties used by the Ant build system edit
|
||||||
|
# "ant.properties", and override values to adapt the script to your
|
||||||
|
# project structure.
|
||||||
|
#
|
||||||
|
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
|
||||||
|
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
|
||||||
|
|
||||||
|
# Project target.
|
||||||
|
target=android-17
|
||||||
|
android.library=true
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
Copyright 2012 Jay Weisskopf
|
||||||
|
|
||||||
|
Licensed under the MIT License (see LICENSE.txt)
|
||||||
|
-->
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/slider_preference_layout"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:paddingBottom="8dip"
|
||||||
|
android:paddingLeft="16dip"
|
||||||
|
android:paddingRight="16dip"
|
||||||
|
android:paddingTop="8dip" >
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+android:id/message"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
<SeekBar
|
||||||
|
android:id="@+id/slider_preference_seekbar"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingBottom="12dip"
|
||||||
|
android:paddingTop="12dip" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
Copyright 2012 Jay Weisskopf
|
||||||
|
|
||||||
|
Licensed under the MIT License (see LICENSE.txt)
|
||||||
|
-->
|
||||||
|
<resources>
|
||||||
|
|
||||||
|
<declare-styleable name="SliderPreference">
|
||||||
|
<attr name="android:summary" />
|
||||||
|
</declare-styleable>
|
||||||
|
|
||||||
|
</resources>
|
Binary file not shown.
After Width: | Height: | Size: 30 KiB |
|
@ -0,0 +1,149 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012 Jay Weisskopf
|
||||||
|
*
|
||||||
|
* Licensed under the MIT License (see LICENSE.txt)
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.jayschwa.android.preference;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.TypedArray;
|
||||||
|
import android.preference.DialogPreference;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.SeekBar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Jay Weisskopf
|
||||||
|
*/
|
||||||
|
public class SliderPreference extends DialogPreference {
|
||||||
|
|
||||||
|
protected final static int SEEKBAR_RESOLUTION = 10000;
|
||||||
|
|
||||||
|
protected float mValue;
|
||||||
|
protected int mSeekBarValue;
|
||||||
|
protected CharSequence[] mSummaries;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param context
|
||||||
|
* @param attrs
|
||||||
|
*/
|
||||||
|
public SliderPreference(Context context, AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
setup(context, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param context
|
||||||
|
* @param attrs
|
||||||
|
* @param defStyle
|
||||||
|
*/
|
||||||
|
public SliderPreference(Context context, AttributeSet attrs, int defStyle) {
|
||||||
|
super(context, attrs, defStyle);
|
||||||
|
setup(context, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setup(Context context, AttributeSet attrs) {
|
||||||
|
setDialogLayoutResource(R.layout.slider_preference_dialog);
|
||||||
|
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SliderPreference);
|
||||||
|
try {
|
||||||
|
setSummary(a.getTextArray(R.styleable.SliderPreference_android_summary));
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
a.recycle();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object onGetDefaultValue(TypedArray a, int index) {
|
||||||
|
return a.getFloat(index, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
|
||||||
|
setValue(restoreValue ? getPersistedFloat(mValue) : (Float) defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CharSequence getSummary() {
|
||||||
|
if (mSummaries != null && mSummaries.length > 0) {
|
||||||
|
int index = (int) (mValue * mSummaries.length);
|
||||||
|
index = Math.min(index, mSummaries.length - 1);
|
||||||
|
return mSummaries[index];
|
||||||
|
} else {
|
||||||
|
return super.getSummary();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSummary(CharSequence[] summaries) {
|
||||||
|
mSummaries = summaries;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setSummary(CharSequence summary) {
|
||||||
|
super.setSummary(summary);
|
||||||
|
mSummaries = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setSummary(int summaryResId) {
|
||||||
|
try {
|
||||||
|
setSummary(getContext().getResources().getStringArray(summaryResId));
|
||||||
|
} catch (Exception e) {
|
||||||
|
super.setSummary(summaryResId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getValue() {
|
||||||
|
return mValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(float value) {
|
||||||
|
value = Math.max(0, Math.min(value, 1)); // clamp to [0, 1]
|
||||||
|
if (shouldPersist()) {
|
||||||
|
persistFloat(value);
|
||||||
|
}
|
||||||
|
if (value != mValue) {
|
||||||
|
mValue = value;
|
||||||
|
notifyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected View onCreateDialogView() {
|
||||||
|
mSeekBarValue = (int) (mValue * SEEKBAR_RESOLUTION);
|
||||||
|
View view = super.onCreateDialogView();
|
||||||
|
SeekBar seekbar = (SeekBar) view.findViewById(R.id.slider_preference_seekbar);
|
||||||
|
seekbar.setMax(SEEKBAR_RESOLUTION);
|
||||||
|
seekbar.setProgress(mSeekBarValue);
|
||||||
|
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||||
|
if (fromUser) {
|
||||||
|
SliderPreference.this.mSeekBarValue = progress;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDialogClosed(boolean positiveResult) {
|
||||||
|
final float newValue = (float) mSeekBarValue / SEEKBAR_RESOLUTION;
|
||||||
|
if (positiveResult && callChangeListener(newValue)) {
|
||||||
|
setValue(newValue);
|
||||||
|
}
|
||||||
|
super.onDialogClosed(positiveResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Save and restore preference state.
|
||||||
|
}
|
Loading…
Reference in New Issue