├── .gitignore
├── sample
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── mipmap-hdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── values
│ │ │ ├── colors.xml
│ │ │ ├── strings.xml
│ │ │ ├── dimens.xml
│ │ │ └── styles.xml
│ │ ├── values-w820dp
│ │ │ └── dimens.xml
│ │ ├── values-v21
│ │ │ └── styles.xml
│ │ ├── drawable
│ │ │ └── settings_box.xml
│ │ ├── xml
│ │ │ ├── pref_general.xml
│ │ │ └── pref_general_compat.xml
│ │ └── layout
│ │ │ └── view_demo.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── pavelsikun
│ │ └── seekbarpreference
│ │ └── sample
│ │ ├── ExamplePreferenceActivity.java
│ │ ├── ExamplePreferenceCompatActivity.java
│ │ └── ExampleViewActivity.java
├── proguard-rules.pro
└── build.gradle
├── library
├── .gitignore
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ ├── res
│ │ ├── values
│ │ │ ├── dimens.xml
│ │ │ ├── colors.xml
│ │ │ ├── strings.xml
│ │ │ ├── styles.xml
│ │ │ └── attrs.xml
│ │ ├── values-w320dp
│ │ │ └── dimens.xml
│ │ ├── values-w400dp
│ │ │ └── dimens.xml
│ │ ├── values-w480dp
│ │ │ └── dimens.xml
│ │ ├── values-w600dp
│ │ │ └── dimens.xml
│ │ ├── values-w720dp
│ │ │ └── dimens.xml
│ │ ├── drawable
│ │ │ ├── msbp_bottom_line_enabled.xml
│ │ │ ├── msbp_bottom_line_disabled.xml
│ │ │ ├── msbp_bottom_line.xml
│ │ │ └── msbp_math_compass.xml
│ │ └── layout
│ │ │ ├── seekbar_view_layout.xml
│ │ │ └── value_selector_dialog.xml
│ │ └── java
│ │ └── com
│ │ └── pavelsikun
│ │ └── seekbarpreference
│ │ ├── ChangeValueListener.java
│ │ ├── PersistValueListener.java
│ │ ├── SeekBarPreference.java
│ │ ├── SeekBarPreferenceView.java
│ │ ├── SeekBarPreferenceCompat.java
│ │ ├── CustomValueDialog.java
│ │ └── PreferenceControllerDelegate.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── ART
├── screen_2.jpg
├── screen_3.jpg
└── screen_4.jpg
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 |
--------------------------------------------------------------------------------
/sample/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | sample.iml
3 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | library.iml
3 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':library', ':sample'
2 |
--------------------------------------------------------------------------------
/ART/screen_2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/green-green-avk/MaterialSeekBarPreference/master/ART/screen_2.jpg
--------------------------------------------------------------------------------
/ART/screen_3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/green-green-avk/MaterialSeekBarPreference/master/ART/screen_3.jpg
--------------------------------------------------------------------------------
/ART/screen_4.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/green-green-avk/MaterialSeekBarPreference/master/ART/screen_4.jpg
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/library/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 160dp
4 |
--------------------------------------------------------------------------------
/library/src/main/res/values-w320dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 200dp
4 |
--------------------------------------------------------------------------------
/library/src/main/res/values-w400dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 268dp
4 |
--------------------------------------------------------------------------------
/library/src/main/res/values-w480dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 412dp
4 |
--------------------------------------------------------------------------------
/library/src/main/res/values-w600dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 520dp
4 |
--------------------------------------------------------------------------------
/library/src/main/res/values-w720dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 640dp
4 |
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/green-green-avk/MaterialSeekBarPreference/master/sample/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/green-green-avk/MaterialSeekBarPreference/master/sample/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/green-green-avk/MaterialSeekBarPreference/master/sample/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/green-green-avk/MaterialSeekBarPreference/master/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/library/src/main/java/com/pavelsikun/seekbarpreference/ChangeValueListener.java:
--------------------------------------------------------------------------------
1 | package com.pavelsikun.seekbarpreference;
2 |
3 | public interface ChangeValueListener {
4 | boolean onChange(int value);
5 | }
6 |
--------------------------------------------------------------------------------
/library/src/main/res/drawable/msbp_bottom_line_enabled.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/library/src/main/res/drawable/msbp_bottom_line_disabled.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #ca3a4b
4 | #ff862732
5 | #5E35B1
6 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Hello world!
3 | Settings
4 | SeekBarPreference Lib Sample :D
5 |
6 |
--------------------------------------------------------------------------------
/library/src/main/java/com/pavelsikun/seekbarpreference/PersistValueListener.java:
--------------------------------------------------------------------------------
1 | package com.pavelsikun.seekbarpreference;
2 |
3 | /**
4 | * Created by Pavel Sikun on 21.05.16.
5 | */
6 |
7 | public interface PersistValueListener {
8 | boolean persistInt(int value);
9 | }
--------------------------------------------------------------------------------
/sample/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 16dp
6 |
7 |
--------------------------------------------------------------------------------
/library/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | - #BDBDBD
5 | - #EEEEEE
6 |
7 | - #607D8B
8 |
9 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Must be between
4 | And
5 | Enter custom value
6 | apply
7 |
--------------------------------------------------------------------------------
/library/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
--------------------------------------------------------------------------------
/library/src/main/res/drawable/msbp_bottom_line.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
--------------------------------------------------------------------------------
/sample/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/sample/src/main/res/values-v21/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/library/src/main/res/drawable/msbp_math_compass.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/mrbimc/dev/sdks/android-sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/sample/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/mrbimc/dev/sdks/android-sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.3"
6 |
7 | defaultConfig {
8 | minSdkVersion 7
9 | targetSdkVersion 23
10 | versionCode 13
11 | versionName "2.3.0"
12 | }
13 |
14 | lintOptions {
15 | abortOnError false
16 | }
17 | }
18 |
19 | dependencies {
20 | compile 'com.android.support:appcompat-v7:23.4.0'
21 | compile 'com.android.support:preference-v7:23.4.0'
22 | }
23 |
24 | ext {
25 | PUBLISH_GROUP_ID = 'com.pavelsikun'
26 | PUBLISH_ARTIFACT_ID = 'material-seekbar-preference'
27 | PUBLISH_VERSION = '2.3.0'
28 | }
29 |
30 | apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle'
31 | // ./gradlew clean build generateRelease
--------------------------------------------------------------------------------
/sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.pavelsikun.seekbarpreference.sample"
9 | minSdkVersion 7
10 | targetSdkVersion 23
11 | versionCode 6
12 | versionName "6.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile project(':library')
24 | // for your projects use:
25 | // compile 'com.pavelsikun:material-seekbar-preference:2.3.0'
26 |
27 | compile 'com.android.support:appcompat-v7:23.4.0'
28 | compile 'com.android.support:cardview-v7:23.4.0'
29 | compile 'com.android.support:preference-v7:23.4.0'
30 | }
31 |
--------------------------------------------------------------------------------
/library/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
13 |
17 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
11 |
12 |
15 |
19 |
20 |
21 |
22 |
23 |
24 |
27 |
28 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable/settings_box.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/pavelsikun/seekbarpreference/sample/ExamplePreferenceActivity.java:
--------------------------------------------------------------------------------
1 | package com.pavelsikun.seekbarpreference.sample;
2 |
3 | import android.annotation.TargetApi;
4 | import android.os.Build;
5 | import android.os.Bundle;
6 | import android.preference.PreferenceFragment;
7 | import android.support.v7.app.ActionBar;
8 | import android.support.v7.app.AppCompatActivity;
9 | import android.view.MenuItem;
10 | import android.view.View;
11 | import com.pavelsikun.seekbarpreference.SeekBarPreference;
12 |
13 | /**
14 | * Created by mrbimc on 02.04.16.
15 | */
16 |
17 | @TargetApi(Build.VERSION_CODES.HONEYCOMB)
18 | public class ExamplePreferenceActivity extends AppCompatActivity {
19 |
20 | @Override
21 | protected void onCreate(Bundle savedInstanceState) {
22 | super.onCreate(savedInstanceState);
23 | getFragmentManager()
24 | .beginTransaction()
25 | .replace(android.R.id.content, new PreferencesScreen())
26 | .commit();
27 |
28 | ActionBar toolbar = getSupportActionBar();
29 | if(toolbar != null) {
30 | toolbar.setDisplayHomeAsUpEnabled(true);
31 | }
32 | }
33 |
34 | @Override
35 | public boolean onOptionsItemSelected(MenuItem item) {
36 | if(item.getItemId() == android.R.id.home) {
37 | onBackPressed();
38 | }
39 | return super.onOptionsItemSelected(item);
40 | }
41 |
42 | public static class PreferencesScreen extends PreferenceFragment {
43 | public void onCreate(Bundle savedInstanceState) {
44 | super.onCreate(savedInstanceState);
45 | addPreferencesFromResource(R.xml.pref_general);
46 | }
47 |
48 | @Override
49 | public void onViewCreated(View view, Bundle savedInstanceState) {
50 | super.onViewCreated(view, savedInstanceState);
51 |
52 | SeekBarPreference pref = new SeekBarPreference(getActivity());
53 | pref.setTitle("Dynamic Preference");
54 | pref.setSummary("This one was added from Java");
55 | pref.setMinValue(222);
56 | pref.setMaxValue(999);
57 | pref.setCurrentValue(444);
58 | pref.setMeasurementUnit("nectarines");
59 |
60 | getPreferenceScreen().addPreference(pref);
61 | }
62 |
63 |
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/sample/src/main/res/xml/pref_general.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
13 |
14 |
22 |
23 |
32 |
33 |
44 |
45 |
56 |
57 |
63 |
64 |
75 |
76 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/pavelsikun/seekbarpreference/sample/ExamplePreferenceCompatActivity.java:
--------------------------------------------------------------------------------
1 | package com.pavelsikun.seekbarpreference.sample;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.preference.PreferenceFragmentCompat;
5 | import android.support.v7.app.ActionBar;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.view.MenuItem;
8 | import android.view.View;
9 |
10 | import com.pavelsikun.seekbarpreference.SeekBarPreferenceCompat;
11 |
12 | /**
13 | * Created by mrbimc on 02.04.16.
14 | */
15 |
16 | public class ExamplePreferenceCompatActivity extends AppCompatActivity {
17 |
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | getSupportFragmentManager()
22 | .beginTransaction()
23 | .replace(android.R.id.content, new PreferencesScreen())
24 | .commit();
25 |
26 | ActionBar toolbar = getSupportActionBar();
27 | if(toolbar != null) {
28 | toolbar.setDisplayHomeAsUpEnabled(true);
29 | }
30 | }
31 |
32 | @Override
33 | public boolean onOptionsItemSelected(MenuItem item) {
34 | if(item.getItemId() == android.R.id.home) {
35 | onBackPressed();
36 | }
37 | return super.onOptionsItemSelected(item);
38 | }
39 |
40 | public static class PreferencesScreen extends PreferenceFragmentCompat {
41 |
42 | //I also recommend you to use some 3rd-party lib to style PreferenceFragmentCompat
43 | //because by default v7 prefs don't respect device theme and look ugly,
44 | //even on v21+(thought afaik since v23.4.0 look on lollipop+ was fixed)
45 |
46 | //theoretically useful links:
47 | // * http://stackoverflow.com/questions/32070670/preferencefragmentcompat-requires-preferencetheme-to-be-set
48 | // * https://github.com/consp1racy/android-support-preference
49 |
50 | @Override
51 | public void onViewCreated(View view, Bundle savedInstanceState) {
52 | super.onViewCreated(view, savedInstanceState);
53 |
54 | SeekBarPreferenceCompat pref = new SeekBarPreferenceCompat(getActivity());
55 | pref.setTitle("Dynamic PreferenceCompat Example");
56 | pref.setSummary("This one was added from Java");
57 | pref.setMinValue(222);
58 | pref.setMaxValue(999);
59 | pref.setCurrentValue(444);
60 | pref.setMeasurementUnit("nectarines");
61 |
62 | getPreferenceScreen().addPreference(pref);
63 | }
64 |
65 | @Override
66 | public void onCreatePreferences(Bundle bundle, String s) {
67 | addPreferencesFromResource(R.xml.pref_general_compat); // load your SeekBarPreferenceCompat prefs from xml
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/sample/src/main/res/xml/pref_general_compat.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
13 |
14 |
22 |
23 |
32 |
33 |
44 |
45 |
56 |
57 |
63 |
64 |
75 |
76 |
--------------------------------------------------------------------------------
/library/src/main/res/layout/seekbar_view_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
20 |
21 |
29 |
30 |
34 |
35 |
41 |
42 |
54 |
55 |
59 |
64 |
72 |
80 |
81 |
82 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
--------------------------------------------------------------------------------
/library/src/main/res/layout/value_selector_dialog.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
14 |
15 |
21 |
22 |
31 |
32 |
33 |
40 |
47 |
48 |
55 |
56 |
64 |
65 |
72 |
73 |
80 |
81 |
82 |
87 |
88 |
94 |
95 |
101 |
102 |
103 |
104 |
--------------------------------------------------------------------------------
/library/src/main/java/com/pavelsikun/seekbarpreference/SeekBarPreference.java:
--------------------------------------------------------------------------------
1 | package com.pavelsikun.seekbarpreference;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.Context;
5 | import android.os.Build;
6 | import android.preference.Preference;
7 | import android.util.AttributeSet;
8 | import android.view.View;
9 |
10 | /**
11 | * Created by Pavel Sikun on 21.05.16.
12 | */
13 |
14 | public class SeekBarPreference extends Preference implements View.OnClickListener, PreferenceControllerDelegate.ViewStateListener, PersistValueListener, ChangeValueListener {
15 |
16 | private PreferenceControllerDelegate controllerDelegate;
17 |
18 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
19 | public SeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
20 | super(context, attrs, defStyleAttr, defStyleRes);
21 | init(attrs);
22 | }
23 |
24 | public SeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
25 | super(context, attrs, defStyleAttr);
26 | init(attrs);
27 | }
28 |
29 | public SeekBarPreference(Context context, AttributeSet attrs) {
30 | super(context, attrs);
31 | init(attrs);
32 | }
33 |
34 | public SeekBarPreference(Context context) {
35 | super(context);
36 | init(null);
37 | }
38 |
39 | private void init(AttributeSet attrs) {
40 | setLayoutResource(R.layout.seekbar_view_layout);
41 | controllerDelegate = new PreferenceControllerDelegate(getContext(), false);
42 |
43 | controllerDelegate.setViewStateListener(this);
44 | controllerDelegate.setPersistValueListener(this);
45 | controllerDelegate.setChangeValueListener(this);
46 |
47 | controllerDelegate.loadValuesFromXml(attrs);
48 | }
49 |
50 | @Override
51 | protected void onBindView(View view) {
52 | super.onBindView(view);
53 | controllerDelegate.onBind(view);
54 | }
55 |
56 | @Override
57 | protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
58 | super.onSetInitialValue(restorePersistedValue, defaultValue);
59 | controllerDelegate.setCurrentValue(getPersistedInt(controllerDelegate.getCurrentValue()));
60 | }
61 |
62 | @Override
63 | public boolean persistInt(int value) {
64 | return super.persistInt(value);
65 | }
66 |
67 | @Override
68 | public boolean onChange(int value) {
69 | return callChangeListener(value);
70 | }
71 |
72 | @Override
73 | public void onClick(final View v) {
74 | controllerDelegate.onClick(v);
75 | }
76 |
77 | public int getMaxValue() {
78 | return controllerDelegate.getMaxValue();
79 | }
80 |
81 | public void setMaxValue(int maxValue) {
82 | controllerDelegate.setMaxValue(maxValue);
83 | }
84 |
85 | public int getMinValue() {
86 | return controllerDelegate.getMinValue();
87 | }
88 |
89 | public void setMinValue(int minValue) {
90 | controllerDelegate.setMinValue(minValue);
91 | }
92 |
93 | public int getInterval() {
94 | return controllerDelegate.getInterval();
95 | }
96 |
97 | public void setInterval(int interval) {
98 | controllerDelegate.setInterval(interval);
99 | }
100 |
101 | public int getCurrentValue() {
102 | return controllerDelegate.getCurrentValue();
103 | }
104 |
105 | public void setCurrentValue(int currentValue) {
106 | controllerDelegate.setCurrentValue(currentValue);
107 | persistInt(controllerDelegate.getCurrentValue());
108 | }
109 |
110 | public String getMeasurementUnit() {
111 | return controllerDelegate.getMeasurementUnit();
112 | }
113 |
114 | public void setMeasurementUnit(String measurementUnit) {
115 | controllerDelegate.setMeasurementUnit(measurementUnit);
116 | }
117 |
118 | public boolean isDialogEnabled() {
119 | return controllerDelegate.isDialogEnabled();
120 | }
121 |
122 | public void setDialogEnabled(boolean dialogEnabled) {
123 | controllerDelegate.setDialogEnabled(dialogEnabled);
124 | }
125 |
126 | public void setDialogStyle(int dialogStyle) {
127 | controllerDelegate.setDialogStyle(dialogStyle);
128 | }
129 | }
130 |
--------------------------------------------------------------------------------
/library/src/main/java/com/pavelsikun/seekbarpreference/SeekBarPreferenceView.java:
--------------------------------------------------------------------------------
1 | package com.pavelsikun.seekbarpreference;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.Context;
5 | import android.os.Build;
6 | import android.util.AttributeSet;
7 | import android.view.View;
8 | import android.widget.FrameLayout;
9 |
10 | /**
11 | * Created by Pavel Sikun on 21.05.16.
12 | */
13 |
14 | public class SeekBarPreferenceView extends FrameLayout implements View.OnClickListener {
15 |
16 | private PreferenceControllerDelegate controllerDelegate;
17 |
18 | public SeekBarPreferenceView(Context context) {
19 | super(context);
20 | init(null);
21 | }
22 |
23 | public SeekBarPreferenceView(Context context, AttributeSet attrs) {
24 | super(context, attrs);
25 | init(attrs);
26 | }
27 |
28 | public SeekBarPreferenceView(Context context, AttributeSet attrs, int defStyleAttr) {
29 | super(context, attrs, defStyleAttr);
30 | init(attrs);
31 | }
32 |
33 |
34 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
35 | public SeekBarPreferenceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
36 | super(context, attrs, defStyleAttr, defStyleRes);
37 | init(attrs);
38 | }
39 |
40 | private void init(AttributeSet attrs) {
41 | controllerDelegate = new PreferenceControllerDelegate(getContext(), true);
42 | controllerDelegate.loadValuesFromXml(attrs);
43 | }
44 |
45 | @Override
46 | protected void onAttachedToWindow() {
47 | super.onAttachedToWindow();
48 | View view = inflate(getContext(), R.layout.seekbar_view_layout, this);
49 | controllerDelegate.onBind(view);
50 | }
51 |
52 | @Override
53 | public void onClick(final View v) {
54 | controllerDelegate.onClick(v);
55 | }
56 |
57 | @Override
58 | public void setEnabled(boolean enabled) {
59 | controllerDelegate.setEnabled(enabled);
60 | }
61 |
62 | @Override
63 | public boolean isEnabled() {
64 | return controllerDelegate.isEnabled();
65 | }
66 |
67 | public int getMaxValue() {
68 | return controllerDelegate.getMaxValue();
69 | }
70 |
71 | public void setMaxValue(int maxValue) {
72 | controllerDelegate.setMaxValue(maxValue);
73 | }
74 |
75 | public String getTitle() {
76 | return controllerDelegate.getTitle();
77 | }
78 |
79 | public void setTitle(String title) {
80 | controllerDelegate.setTitle(title);
81 | }
82 |
83 | public String getSummary() {
84 | return controllerDelegate.getSummary();
85 | }
86 |
87 | public void setSummary(String summary) {
88 | controllerDelegate.setSummary(summary);
89 | }
90 |
91 | public int getMinValue() {
92 | return controllerDelegate.getMinValue();
93 | }
94 |
95 | public void setMinValue(int minValue) {
96 | controllerDelegate.setMinValue(minValue);
97 | }
98 |
99 | public int getInterval() {
100 | return controllerDelegate.getInterval();
101 | }
102 |
103 | public void setInterval(int interval) {
104 | controllerDelegate.setInterval(interval);
105 | }
106 |
107 | public int getCurrentValue() {
108 | return controllerDelegate.getCurrentValue();
109 | }
110 |
111 | public void setCurrentValue(int currentValue) {
112 | controllerDelegate.setCurrentValue(currentValue);
113 | }
114 |
115 | public String getMeasurementUnit() {
116 | return controllerDelegate.getMeasurementUnit();
117 | }
118 |
119 | public void setMeasurementUnit(String measurementUnit) {
120 | controllerDelegate.setMeasurementUnit(measurementUnit);
121 | }
122 |
123 | public void setOnValueSelectedListener(PersistValueListener persistValueListener) {
124 | controllerDelegate.setPersistValueListener(persistValueListener);
125 | }
126 |
127 | public boolean isDialogEnabled() {
128 | return controllerDelegate.isDialogEnabled();
129 | }
130 |
131 | public void setDialogEnabled(boolean dialogEnabled) {
132 | controllerDelegate.setDialogEnabled(dialogEnabled);
133 | }
134 |
135 | public void setDialogStyle(int dialogStyle) {
136 | controllerDelegate.setDialogStyle(dialogStyle);
137 | }
138 | }
139 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/pavelsikun/seekbarpreference/sample/ExampleViewActivity.java:
--------------------------------------------------------------------------------
1 | package com.pavelsikun.seekbarpreference.sample;
2 |
3 | import android.content.Intent;
4 | import android.os.Build;
5 | import android.os.Bundle;
6 | import android.support.annotation.Nullable;
7 | import android.support.v7.app.ActionBar;
8 | import android.support.v7.app.AppCompatActivity;
9 | import android.support.v7.widget.SwitchCompat;
10 | import android.util.Log;
11 | import android.view.MenuItem;
12 | import android.view.View;
13 | import android.widget.CompoundButton;
14 | import android.widget.LinearLayout;
15 | import android.widget.RelativeLayout;
16 | import android.widget.Toast;
17 |
18 | import com.pavelsikun.seekbarpreference.PersistValueListener;
19 | import com.pavelsikun.seekbarpreference.SeekBarPreferenceView;
20 |
21 | /**
22 | * Created by Pavel Sikun on 21.05.16.
23 | */
24 |
25 | public class ExampleViewActivity extends AppCompatActivity {
26 |
27 | private LinearLayout root;
28 |
29 | boolean isCompatPrefs = false;
30 |
31 | @Override
32 | protected void onCreate(@Nullable Bundle savedInstanceState) {
33 | super.onCreate(savedInstanceState);
34 | setContentView(R.layout.view_demo);
35 |
36 | findViewById(R.id.uselessTempButton).setOnClickListener(new View.OnClickListener() {
37 | @Override
38 | public void onClick(View v) {
39 | if(isCompatPrefs) {
40 | startActivity(new Intent(ExampleViewActivity.this, ExamplePreferenceCompatActivity.class));
41 | }
42 | else {
43 | if(Build.VERSION.SDK_INT < 11) {
44 | Toast.makeText(
45 | ExampleViewActivity.this,
46 | "API-11+ required for this. Switch to preference-v7 mode instead",
47 | Toast.LENGTH_LONG).show();
48 | }
49 | else {
50 | startActivity(new Intent(ExampleViewActivity.this, ExamplePreferenceActivity.class));
51 | }
52 | }
53 | }
54 | });
55 |
56 | ActionBar toolbar = getSupportActionBar();
57 | if(toolbar != null) {
58 | toolbar.setDisplayHomeAsUpEnabled(true);
59 | }
60 |
61 | root = (LinearLayout) findViewById(R.id.root);
62 |
63 | final SwitchCompat s = (SwitchCompat) findViewById(R.id.switchWidget);
64 | RelativeLayout text = (RelativeLayout) findViewById(R.id.switchHolder);
65 |
66 | text.setOnClickListener(new View.OnClickListener() {
67 | @Override
68 | public void onClick(View v) {
69 | s.toggle();
70 | }
71 | });
72 |
73 | s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
74 | @Override
75 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
76 | isCompatPrefs = isChecked;
77 | Log.d(getClass().getSimpleName(), "switch state changed " + isChecked);
78 | }
79 | });
80 | }
81 |
82 | @Override
83 | public void onPostCreate(@Nullable Bundle savedInstanceState) {
84 | super.onPostCreate(savedInstanceState);
85 |
86 | SeekBarPreferenceView view = new SeekBarPreferenceView(this);
87 | view.setTitle("Dynamic View");
88 | view.setSummary("This one was added from Java");
89 | view.setMaxValue(5000);
90 | view.setMinValue(-5000);
91 | view.setInterval(10);
92 | view.setCurrentValue(0);
93 | view.setMeasurementUnit("points");
94 | view.setOnValueSelectedListener(new PersistValueListener() {
95 | @Override
96 | public boolean persistInt(int value) {
97 | Toast.makeText(ExampleViewActivity.this, "callback from view: " + value, Toast.LENGTH_SHORT).show();
98 | return true;
99 | }
100 | });
101 |
102 | root.addView(view);
103 | Log.d(getClass().getSimpleName(), "added new view");
104 | }
105 |
106 | @Override
107 | public boolean onOptionsItemSelected(MenuItem item) {
108 | if(item.getItemId() == android.R.id.home) {
109 | onBackPressed();
110 | }
111 | return super.onOptionsItemSelected(item);
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/library/src/main/java/com/pavelsikun/seekbarpreference/SeekBarPreferenceCompat.java:
--------------------------------------------------------------------------------
1 | package com.pavelsikun.seekbarpreference;
2 |
3 | import android.annotation.TargetApi;
4 | import android.content.Context;
5 | import android.os.Build;
6 | import android.support.v7.preference.Preference;
7 | import android.support.v7.preference.PreferenceViewHolder;
8 | import android.util.AttributeSet;
9 | import android.view.View;
10 |
11 | /**
12 | * Created by Pavel Sikun on 22.05.16.
13 | */
14 |
15 | public class SeekBarPreferenceCompat extends Preference implements View.OnClickListener, PreferenceControllerDelegate.ViewStateListener, PersistValueListener, ChangeValueListener {
16 |
17 | private PreferenceControllerDelegate controllerDelegate;
18 |
19 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
20 | public SeekBarPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
21 | super(context, attrs, defStyleAttr, defStyleRes);
22 | init(attrs);
23 | }
24 |
25 | public SeekBarPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr) {
26 | super(context, attrs, defStyleAttr);
27 | init(attrs);
28 | }
29 |
30 | public SeekBarPreferenceCompat(Context context, AttributeSet attrs) {
31 | super(context, attrs);
32 | init(attrs);
33 | }
34 |
35 | public SeekBarPreferenceCompat(Context context) {
36 | super(context);
37 | init(null);
38 | }
39 |
40 | private void init(AttributeSet attrs) {
41 | setLayoutResource(R.layout.seekbar_view_layout);
42 | controllerDelegate = new PreferenceControllerDelegate(getContext(), false);
43 |
44 | controllerDelegate.setViewStateListener(this);
45 | controllerDelegate.setPersistValueListener(this);
46 | controllerDelegate.setChangeValueListener(this);
47 |
48 | controllerDelegate.loadValuesFromXml(attrs);
49 | }
50 |
51 | @Override
52 | public void onBindViewHolder(PreferenceViewHolder viewRoot) {
53 | super.onBindViewHolder(viewRoot);
54 | controllerDelegate.onBind(viewRoot.itemView);
55 | }
56 |
57 | @Override
58 | protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
59 | super.onSetInitialValue(restorePersistedValue, defaultValue);
60 | controllerDelegate.setCurrentValue(getPersistedInt(controllerDelegate.getCurrentValue()));
61 | }
62 |
63 | @Override
64 | public boolean persistInt(int value) {
65 | return super.persistInt(value);
66 | }
67 |
68 | @Override
69 | public boolean onChange(int value) {
70 | return callChangeListener(value);
71 | }
72 |
73 | @Override
74 | public void onClick(final View v) {
75 | controllerDelegate.onClick(v);
76 | }
77 |
78 | public int getMaxValue() {
79 | return controllerDelegate.getMaxValue();
80 | }
81 |
82 | public void setMaxValue(int maxValue) {
83 | controllerDelegate.setMaxValue(maxValue);
84 | }
85 |
86 | public int getMinValue() {
87 | return controllerDelegate.getMinValue();
88 | }
89 |
90 | public void setMinValue(int minValue) {
91 | controllerDelegate.setMinValue(minValue);
92 | }
93 |
94 | public int getInterval() {
95 | return controllerDelegate.getInterval();
96 | }
97 |
98 | public void setInterval(int interval) {
99 | controllerDelegate.setInterval(interval);
100 | }
101 |
102 | public int getCurrentValue() {
103 | return controllerDelegate.getCurrentValue();
104 | }
105 |
106 | public void setCurrentValue(int currentValue) {
107 | controllerDelegate.setCurrentValue(currentValue);
108 | persistInt(controllerDelegate.getCurrentValue());
109 | }
110 |
111 | public String getMeasurementUnit() {
112 | return controllerDelegate.getMeasurementUnit();
113 | }
114 |
115 | public void setMeasurementUnit(String measurementUnit) {
116 | controllerDelegate.setMeasurementUnit(measurementUnit);
117 | }
118 |
119 | public boolean isDialogEnabled() {
120 | return controllerDelegate.isDialogEnabled();
121 | }
122 |
123 | public void setDialogEnabled(boolean dialogEnabled) {
124 | controllerDelegate.setDialogEnabled(dialogEnabled);
125 | }
126 |
127 | public void setDialogStyle(int dialogStyle) {
128 | controllerDelegate.setDialogStyle(dialogStyle);
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/library/src/main/java/com/pavelsikun/seekbarpreference/CustomValueDialog.java:
--------------------------------------------------------------------------------
1 | package com.pavelsikun.seekbarpreference;
2 |
3 | import android.app.Dialog;
4 | import android.content.Context;
5 | import android.content.res.TypedArray;
6 | import android.support.v7.app.AlertDialog;
7 | import android.util.Log;
8 | import android.util.TypedValue;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.widget.Button;
12 | import android.widget.EditText;
13 | import android.widget.LinearLayout;
14 | import android.widget.TextView;
15 |
16 | /**
17 | * Created by Pavel Sikun on 21.05.16.
18 | */
19 | class CustomValueDialog {
20 |
21 | private final String TAG = getClass().getSimpleName();
22 |
23 | private Dialog dialog;
24 | private EditText customValueView;
25 |
26 | private int minValue, maxValue, currentValue;
27 | private PersistValueListener persistValueListener;
28 |
29 | CustomValueDialog(Context context, int theme, int minValue, int maxValue, int currentValue) {
30 | this.minValue = minValue;
31 | this.maxValue = maxValue;
32 | this.currentValue = currentValue;
33 |
34 | init(new AlertDialog.Builder(context, theme));
35 | }
36 |
37 | private void init(AlertDialog.Builder dialogBuilder) {
38 | View dialogView = LayoutInflater.from(dialogBuilder.getContext()).inflate(R.layout.value_selector_dialog, null);
39 | dialog = dialogBuilder.setView(dialogView).create();
40 |
41 | TextView minValueView = (TextView) dialogView.findViewById(R.id.minValue);
42 | TextView maxValueView = (TextView) dialogView.findViewById(R.id.maxValue);
43 | customValueView = (EditText) dialogView.findViewById(R.id.customValue);
44 |
45 | minValueView.setText(String.valueOf(minValue));
46 | maxValueView.setText(String.valueOf(maxValue));
47 | customValueView.setHint(String.valueOf(currentValue));
48 |
49 | LinearLayout colorView = (LinearLayout) dialogView.findViewById(R.id.dialog_color_area);
50 | colorView.setBackgroundColor(fetchAccentColor(dialogBuilder.getContext()));
51 |
52 | Button applyButton = (Button) dialogView.findViewById(R.id.btn_apply);
53 | Button cancelButton = (Button) dialogView.findViewById(R.id.btn_cancel);
54 |
55 | applyButton.setOnClickListener(new View.OnClickListener() {
56 | @Override
57 | public void onClick(View v) {
58 | tryApply();
59 | }
60 | });
61 |
62 | cancelButton.setOnClickListener(new View.OnClickListener() {
63 | @Override
64 | public void onClick(View v) {
65 | dialog.dismiss();
66 | }
67 | });
68 | }
69 |
70 | private int fetchAccentColor(Context context) {
71 | TypedValue typedValue = new TypedValue();
72 |
73 | TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[] { R.attr.colorAccent });
74 | int color = a.getColor(0, 0);
75 | a.recycle();
76 |
77 | return color;
78 | }
79 |
80 | CustomValueDialog setPersistValueListener(PersistValueListener listener) {
81 | persistValueListener = listener;
82 | return this;
83 | }
84 |
85 | void show() {
86 | dialog.show();
87 | }
88 |
89 | private void tryApply() {
90 | int value;
91 |
92 | try {
93 | value = Integer.parseInt(customValueView.getText().toString());
94 | if (value > maxValue) {
95 | Log.e(TAG, "wrong input( > than required): " + customValueView.getText().toString());
96 | notifyWrongInput();
97 | return;
98 | }
99 | else if (value < minValue) {
100 | Log.e(TAG, "wrong input( < then required): " + customValueView.getText().toString());
101 | notifyWrongInput();
102 | return;
103 | }
104 | }
105 | catch (Exception e) {
106 | Log.e(TAG, "worng input(non-integer): " + customValueView.getText().toString());
107 | notifyWrongInput();
108 | return;
109 | }
110 |
111 | if(persistValueListener != null) {
112 | persistValueListener.persistInt(value);
113 | dialog.dismiss();
114 | }
115 | }
116 |
117 | private void notifyWrongInput() {
118 | customValueView.setText("");
119 | customValueView.setHint("Wrong Input!");
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## MaterialSeekBarPreference
2 |
3 | [](http://android-arsenal.com/details/1/1756)
4 |
5 | As far as I checked, there are no cool implementations of SeekBarPreference. So I decided to make one. Works on API-v7+
6 |
7 |
8 |
9 |
10 |
11 | #Usage
12 |
13 | Add this to your module dependencies:
14 | ```groovy
15 | compile 'com.pavelsikun:material-seekbar-preference:2.3.0+'
16 | ````
17 |
18 | Reference namespace on top of your layout file:
19 | ```xml
20 | xmlns:sample="http://schemas.android.com/apk/res-auto">
21 | ````
22 |
23 | Now you can use this view in your preferences layout, just like any other normal preference(API-v11+).
24 | ```xml
25 |
37 | ````
38 |
39 | If you have to support API-v7+, this lib provides also SeekBarPreferenceCompat that works with preference-v7.
40 | ```xml
41 |
53 | ````
54 |
55 | Or use MaterialSeekBarView if you prefer to use views instead of preferences(works on v7+):
56 | ```xml
57 |
71 | ```
72 |
73 | Either of way, View/Preference provides next methods to modify and manage it from Java:
74 | ```java
75 | public int getMaxValue();
76 | public void setMaxValue(int maxValue);
77 |
78 | public int getMinValue();
79 | public void setMinValue(int minValue);
80 |
81 | public String getTitle();
82 | public void setTitle(String title);
83 |
84 | public String getSummary();
85 | public void setSummary(String summary);
86 |
87 | public boolean isEnabled();
88 | public void setEnabled(boolean enabled);
89 |
90 | public int getInterval();
91 | public void setInterval(int interval);
92 |
93 | public int getCurrentValue();
94 | public void setCurrentValue(int currentValue);
95 |
96 | public String getMeasurementUnit();
97 | public void setMeasurementUnit(String measurementUnit);
98 |
99 | public void setDialogEnabled(boolean dialogEnabled);
100 |
101 | public void setDialogStyle(int dialogStyle);
102 |
103 | // AND for view-only(at least for now), there's a way to get a callback whenever value changes:
104 | public void setOnValueSelectedListener(PersistValueListener onValuePersisted);
105 | ```
106 |
107 | As you can see, lib provides 4 universal custom attributes(msbp_minValue, msbp_maxValue, msbp_interval and msbp_measurementUnit).
108 |
109 | There are also 4 additional attributes for view bacause it can't use corresponding ones from "android:" (msbp_view_title, msbp_view_summary, msbp_view_enabled and msbp_defaultValue)
110 |
111 | Use them to define look and desired behavior.
112 |
113 | Prefixes used to avoid attribute collisions with other libs.
114 |
115 | # Collaborators
116 | I'd really want to thank:
117 |
118 | * [krage](https://github.com/krage) for adding support for referenced resources.
119 | * [NitroG42](https://github.com/NitroG42) for pointing out to attribute collisions.
120 | * [Dmytro Karataiev](https://github.com/dmytroKarataiev) for a fix for defaultValue.
121 | * [Mehmet Akif Tütüncü](https://github.com/mehmetakiftutuncu) for adding support to disable customInputDialog.
122 |
123 | #Licence
124 | Lib is licenced under *Apache2 licence*, so you can do whatever you want with it.
125 | I'd highly recommend to push changes back to make it cooler :D
126 |
127 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/view_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
16 |
17 |
22 |
28 |
35 |
41 |
42 |
43 |
50 |
57 |
63 |
64 |
65 |
71 |
72 |
83 |
84 |
95 |
96 |
108 |
109 |
121 |
122 |
123 |
124 |
125 |
--------------------------------------------------------------------------------
/library/src/main/java/com/pavelsikun/seekbarpreference/PreferenceControllerDelegate.java:
--------------------------------------------------------------------------------
1 | package com.pavelsikun.seekbarpreference;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.util.AttributeSet;
6 | import android.util.Log;
7 | import android.view.View;
8 | import android.widget.FrameLayout;
9 | import android.widget.LinearLayout;
10 | import android.widget.SeekBar;
11 | import android.widget.TextView;
12 |
13 | /**
14 | * Created by Pavel Sikun on 28.05.16.
15 | */
16 |
17 | class PreferenceControllerDelegate implements SeekBar.OnSeekBarChangeListener, View.OnClickListener {
18 |
19 | private final String TAG = getClass().getSimpleName();
20 |
21 | private static final int DEFAULT_CURRENT_VALUE = 50;
22 | private static final int DEFAULT_MIN_VALUE = 0;
23 | private static final int DEFAULT_MAX_VALUE = 100;
24 | private static final int DEFAULT_INTERVAL = 1;
25 | private static final boolean DEFAULT_DIALOG_ENABLED = true;
26 | private static final boolean DEFAULT_IS_ENABLED = true;
27 |
28 | private static final int DEFAULT_DIALOG_STYLE = R.style.MSB_Dialog_Default;
29 |
30 | private int maxValue;
31 | private int minValue;
32 | private int interval;
33 | private int currentValue;
34 | private String measurementUnit;
35 | private boolean dialogEnabled;
36 |
37 | private int dialogStyle;
38 |
39 | private TextView valueView;
40 | private SeekBar seekBarView;
41 | private TextView measurementView;
42 | private LinearLayout valueHolderView;
43 | private FrameLayout bottomLineView;
44 |
45 | //view stuff
46 | private TextView titleView, summaryView;
47 | private String title;
48 | private String summary;
49 | private boolean isEnabled;
50 |
51 | //controller stuff
52 | private boolean isView = false;
53 | private Context context;
54 | private ViewStateListener viewStateListener;
55 | private PersistValueListener persistValueListener;
56 | private ChangeValueListener changeValueListener;
57 |
58 | interface ViewStateListener {
59 | boolean isEnabled();
60 | void setEnabled(boolean enabled);
61 | }
62 |
63 | PreferenceControllerDelegate(Context context, Boolean isView) {
64 | this.context = context;
65 | this.isView = isView;
66 | }
67 |
68 | void setPersistValueListener(PersistValueListener persistValueListener) {
69 | this.persistValueListener = persistValueListener;
70 | }
71 |
72 | void setViewStateListener(ViewStateListener viewStateListener) {
73 | this.viewStateListener = viewStateListener;
74 | }
75 |
76 | void setChangeValueListener(ChangeValueListener changeValueListener) {
77 | this.changeValueListener = changeValueListener;
78 | }
79 |
80 | void loadValuesFromXml(AttributeSet attrs) {
81 | if(attrs == null) {
82 | currentValue = DEFAULT_CURRENT_VALUE;
83 | minValue = DEFAULT_MIN_VALUE;
84 | maxValue = DEFAULT_MAX_VALUE;
85 | interval = DEFAULT_INTERVAL;
86 | dialogEnabled = DEFAULT_DIALOG_ENABLED;
87 |
88 | isEnabled = DEFAULT_IS_ENABLED;
89 | }
90 | else {
91 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SeekBarPreference);
92 | try {
93 | minValue = a.getInt(R.styleable.SeekBarPreference_msbp_minValue, DEFAULT_MIN_VALUE);
94 | interval = a.getInt(R.styleable.SeekBarPreference_msbp_interval, DEFAULT_INTERVAL);
95 | int saved_maxValue = a.getInt(R.styleable.SeekBarPreference_msbp_maxValue, DEFAULT_MAX_VALUE);
96 | maxValue = (saved_maxValue - minValue) / interval;
97 | dialogEnabled = a.getBoolean(R.styleable.SeekBarPreference_msbp_dialogEnabled, DEFAULT_DIALOG_ENABLED);
98 |
99 | measurementUnit = a.getString(R.styleable.SeekBarPreference_msbp_measurementUnit);
100 | currentValue = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "defaultValue", DEFAULT_CURRENT_VALUE);
101 |
102 | // TODO make it work:
103 | // dialogStyle = a.getInt(R.styleable.SeekBarPreference_msbp_interval, DEFAULT_DIALOG_STYLE);
104 |
105 | dialogStyle = DEFAULT_DIALOG_STYLE;
106 |
107 | if(isView) {
108 | title = a.getString(R.styleable.SeekBarPreference_msbp_view_title);
109 | summary = a.getString(R.styleable.SeekBarPreference_msbp_view_summary);
110 | currentValue = a.getInt(R.styleable.SeekBarPreference_msbp_view_defaultValue, DEFAULT_CURRENT_VALUE);
111 |
112 | isEnabled = a.getBoolean(R.styleable.SeekBarPreference_msbp_view_enabled, DEFAULT_IS_ENABLED);
113 | }
114 | }
115 | finally {
116 | a.recycle();
117 | }
118 | }
119 | }
120 |
121 |
122 | void onBind(View view) {
123 |
124 | if(isView) {
125 | titleView = (TextView) view.findViewById(android.R.id.title);
126 | summaryView = (TextView) view.findViewById(android.R.id.summary);
127 |
128 | titleView.setText(title);
129 | summaryView.setText(summary);
130 | }
131 |
132 | view.setClickable(false);
133 |
134 | seekBarView = (SeekBar) view.findViewById(R.id.seekbar);
135 | measurementView = (TextView) view.findViewById(R.id.measurement_unit);
136 | valueView = (TextView) view.findViewById(R.id.seekbar_value);
137 |
138 | setMaxValue(maxValue);
139 | seekBarView.setOnSeekBarChangeListener(this);
140 |
141 | measurementView.setText(measurementUnit);
142 |
143 | setCurrentValue(currentValue);
144 | valueView.setText(String.valueOf(currentValue));
145 |
146 | bottomLineView = (FrameLayout) view.findViewById(R.id.bottom_line);
147 | valueHolderView = (LinearLayout) view.findViewById(R.id.value_holder);
148 |
149 | setDialogEnabled(dialogEnabled);
150 | setEnabled(isEnabled(), true);
151 | }
152 |
153 | @Override
154 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
155 | int newValue = minValue + (progress * interval);
156 |
157 | if (changeValueListener != null) {
158 | if (!changeValueListener.onChange(newValue)) {
159 | return;
160 | }
161 | }
162 | currentValue = newValue;
163 | valueView.setText(String.valueOf(newValue));
164 | }
165 |
166 | @Override
167 | public void onStartTrackingTouch(SeekBar seekBar) {
168 | }
169 |
170 | @Override
171 | public void onStopTrackingTouch(SeekBar seekBar) {
172 | setCurrentValue(currentValue);
173 | }
174 |
175 | @Override
176 | public void onClick(final View v) {
177 | new CustomValueDialog(context, dialogStyle, minValue, maxValue, currentValue)
178 | .setPersistValueListener(new PersistValueListener() {
179 | @Override
180 | public boolean persistInt(int value) {
181 | setCurrentValue(value);
182 | seekBarView.setOnSeekBarChangeListener(null);
183 | seekBarView.setProgress(currentValue - minValue);
184 | seekBarView.setOnSeekBarChangeListener(PreferenceControllerDelegate.this);
185 |
186 | valueView.setText(String.valueOf(currentValue));
187 | return true;
188 | }
189 | })
190 | .show();
191 | }
192 |
193 |
194 | String getTitle() {
195 | return title;
196 | }
197 |
198 | void setTitle(String title) {
199 | this.title = title;
200 | if(titleView != null) {
201 | titleView.setText(title);
202 | }
203 | }
204 |
205 | String getSummary() {
206 | return summary;
207 | }
208 |
209 | void setSummary(String summary) {
210 | this.summary = summary;
211 | if(seekBarView != null) {
212 | summaryView.setText(summary);
213 | }
214 | }
215 |
216 | boolean isEnabled() {
217 | if(!isView && viewStateListener != null) {
218 | return viewStateListener.isEnabled();
219 | }
220 | else return isEnabled;
221 | }
222 |
223 | void setEnabled(boolean enabled, boolean viewsOnly) {
224 | Log.d(TAG, "setEnabled = " + enabled);
225 | isEnabled = enabled;
226 |
227 | if(viewStateListener != null && !viewsOnly) {
228 | viewStateListener.setEnabled(enabled);
229 | }
230 |
231 | if(seekBarView != null) { //theoretically might not always work
232 | Log.d(TAG, "view is disabled!");
233 | seekBarView.setEnabled(enabled);
234 | valueView.setEnabled(enabled);
235 | valueHolderView.setClickable(enabled);
236 | valueHolderView.setEnabled(enabled);
237 |
238 | measurementView.setEnabled(enabled);
239 | bottomLineView.setEnabled(enabled);
240 |
241 | if(isView) {
242 | titleView.setEnabled(enabled);
243 | summaryView.setEnabled(enabled);
244 | }
245 | }
246 |
247 | }
248 |
249 | void setEnabled(boolean enabled) {
250 | setEnabled(enabled, false);
251 | }
252 |
253 | int getMaxValue() {
254 | return maxValue;
255 | }
256 |
257 | void setMaxValue(int maxValue) {
258 | this.maxValue = maxValue;
259 |
260 | if (seekBarView != null) {
261 | if (minValue <= 0 && maxValue >= 0) {
262 | seekBarView.setMax(maxValue - minValue);
263 | }
264 | else {
265 | seekBarView.setMax(maxValue);
266 | }
267 |
268 | seekBarView.setProgress(currentValue - minValue);
269 | }
270 | }
271 |
272 | int getMinValue() {
273 | return minValue;
274 | }
275 |
276 | public void setMinValue(int minValue) {
277 | this.minValue = minValue;
278 | setMaxValue(maxValue);
279 | }
280 |
281 | int getInterval() {
282 | return interval;
283 | }
284 |
285 | void setInterval(int interval) {
286 | this.interval = interval;
287 | }
288 |
289 | int getCurrentValue() {
290 | return currentValue;
291 | }
292 |
293 | void setCurrentValue(int value) {
294 | if(value < minValue) value = minValue;
295 | if(value > maxValue) value = maxValue;
296 |
297 | if (changeValueListener != null) {
298 | if (!changeValueListener.onChange(value)) {
299 | return;
300 | }
301 | }
302 | currentValue = value;
303 | if(seekBarView != null)
304 | seekBarView.setProgress(currentValue - minValue);
305 |
306 | if(persistValueListener != null) {
307 | persistValueListener.persistInt(value);
308 | }
309 | }
310 |
311 | String getMeasurementUnit() {
312 | return measurementUnit;
313 | }
314 |
315 | void setMeasurementUnit(String measurementUnit) {
316 | this.measurementUnit = measurementUnit;
317 | if(measurementView != null) {
318 | measurementView.setText(measurementUnit);
319 | }
320 | }
321 |
322 | boolean isDialogEnabled() {
323 | return dialogEnabled;
324 | }
325 |
326 | void setDialogEnabled(boolean dialogEnabled) {
327 | this.dialogEnabled = dialogEnabled;
328 |
329 | if(valueHolderView != null && bottomLineView != null) {
330 | valueHolderView.setOnClickListener(dialogEnabled ? this : null);
331 | valueHolderView.setClickable(dialogEnabled);
332 | bottomLineView.setVisibility(dialogEnabled ? View.VISIBLE : View.INVISIBLE);
333 | }
334 | }
335 |
336 | void setDialogStyle(int dialogStyle) {
337 | this.dialogStyle = dialogStyle;
338 | }
339 | }
340 |
--------------------------------------------------------------------------------