CustomSeekbar: Fix auto reset in few cases
Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
Change-Id: Ie377d6b2b3f98cad3c3e9367ab874e6643c34f43
diff --git a/res/layout/preference_custom_seekbar.xml b/res/layout/preference_custom_seekbar.xml
index 53e3982..460cd85 100644
--- a/res/layout/preference_custom_seekbar.xml
+++ b/res/layout/preference_custom_seekbar.xml
@@ -122,7 +122,7 @@
android:layout_alignParentEnd="true"
android:layout_centerVertical="true" />
- <SeekBar
+ <LinearLayout
android:id="@+id/seekbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@@ -130,9 +130,14 @@
android:layout_toEndOf="@id/minus"
android:layout_toStartOf="@id/plus"
android:layout_centerVertical="true" />
-
</RelativeLayout>
-
</RelativeLayout>
+ <!-- Preference should place its actual preference widget here. -->
+ <LinearLayout android:id="@android:id/widget_frame"
+ android:layout_width="wrap_content"
+ android:layout_height="match_parent"
+ android:gravity="end|center_vertical"
+ android:paddingStart="16dp"
+ android:orientation="vertical" />
</LinearLayout>
diff --git a/res/values/attrs.xml b/res/values/attrs.xml
index c8d3895..fb90900 100644
--- a/res/values/attrs.xml
+++ b/res/values/attrs.xml
@@ -21,7 +21,6 @@
<attr name="showSign" format="boolean" />
<attr name="units" format="string|reference" />
<attr name="continuousUpdates" format="boolean" />
- <attr name="defaultText" format="string|reference" />
</declare-styleable>
<!-- Value to pass to callback when restore button is pressed -->
diff --git a/src/com/bliss/support/preferences/CustomSeekBarPreference.java b/src/com/bliss/support/preferences/CustomSeekBarPreference.java
index 97c8a51..ed7ed2b 100644
--- a/src/com/bliss/support/preferences/CustomSeekBarPreference.java
+++ b/src/com/bliss/support/preferences/CustomSeekBarPreference.java
@@ -24,6 +24,8 @@
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
+import android.view.ViewGroup;
+import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
@@ -63,16 +65,20 @@
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomSeekBarPreference);
try {
- mInterval = a.getInt(R.styleable.CustomSeekBarPreference_interval, mInterval);
mShowSign = a.getBoolean(R.styleable.CustomSeekBarPreference_showSign, mShowSign);
- String units = a.getString(R.styleable.CustomSeekBarPreference_units);
- if (units != null)
- mUnits = units;
mContinuousUpdates = a.getBoolean(R.styleable.CustomSeekBarPreference_continuousUpdates, mContinuousUpdates);
} finally {
a.recycle();
}
+ try {
+ String newInterval = attrs.getAttributeValue(SETTINGS_NS, "interval");
+ if (newInterval != null)
+ mInterval = Integer.parseInt(newInterval);
+ } catch (Exception e) {
+ Log.e(TAG, "Invalid interval value", e);
+ }
+ mUnits = getAttributeStringValue(attrs, SETTINGS_NS, "units", "");
mMinValue = attrs.getAttributeIntValue(SETTINGS_NS, "min", mMinValue);
mMaxValue = attrs.getAttributeIntValue(ANDROIDNS, "max", mMaxValue);
if (mMaxValue < mMinValue)
@@ -86,6 +92,7 @@
mValue = mMinValue;
}
+ mSeekBar = new SeekBar(context, attrs);
setLayoutResource(R.layout.preference_custom_seekbar);
}
@@ -106,21 +113,37 @@
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
+ try
+ {
+ // move our seekbar to the new view we've been given
+ ViewParent oldContainer = mSeekBar.getParent();
+ ViewGroup newContainer = (ViewGroup) holder.findViewById(R.id.seekbar);
+ if (oldContainer != newContainer) {
+ // remove the seekbar from the old view
+ if (oldContainer != null) {
+ ((ViewGroup) oldContainer).removeView(mSeekBar);
+ }
+ // remove the existing seekbar (there may not be one) and add ours
+ newContainer.removeAllViews();
+ newContainer.addView(mSeekBar, ViewGroup.LayoutParams.FILL_PARENT,
+ ViewGroup.LayoutParams.WRAP_CONTENT);
+ }
+ } catch (Exception ex) {
+ Log.e(TAG, "Error binding view: " + ex.toString());
+ }
+
+ mSeekBar.setMax(getSeekValue(mMaxValue));
+ mSeekBar.setProgress(getSeekValue(mValue));
+ mSeekBar.setEnabled(isEnabled());
mValueTextView = (TextView) holder.findViewById(R.id.value);
mResetImageView = (ImageView) holder.findViewById(R.id.reset);
mMinusImageView = (ImageView) holder.findViewById(R.id.minus);
mPlusImageView = (ImageView) holder.findViewById(R.id.plus);
- mSeekBar = (SeekBar) holder.findViewById(R.id.seekbar);
-
- mSeekBar.setMax(getSeekValue(mMaxValue));
- mSeekBar.setProgress(getSeekValue(mValue));
-
updateValueViews();
mSeekBar.setOnSeekBarChangeListener(this);
-
mResetImageView.setOnClickListener(this);
mMinusImageView.setOnClickListener(this);
mPlusImageView.setOnClickListener(this);
@@ -129,6 +152,16 @@
mPlusImageView.setOnLongClickListener(this);
}
+ private String getAttributeStringValue(AttributeSet attrs, String namespace, String name,
+ String defaultValue) {
+ String value = attrs.getAttributeValue(namespace, name);
+
+ if (value == null)
+ value = defaultValue;
+
+ return value;
+ }
+
protected int getLimitedValue(int v) {
return v < mMinValue ? mMinValue : (v > mMaxValue ? mMaxValue : v);
}
@@ -142,26 +175,37 @@
}
protected void updateValueViews() {
- mValueTextView.setText(getContext().getString(R.string.custom_seekbar_value,
- (!mTrackingTouch || mContinuousUpdates ? getTextValue(mValue) + (mDefaultValueExists && mValue == mDefaultValue ? " (" + getContext().getString(R.string.custom_seekbar_default_value) + ")" : "")
+ if (mValueTextView != null) {
+ mValueTextView.setText(getContext().getString(R.string.custom_seekbar_value,
+ (!mTrackingTouch || mContinuousUpdates ? getTextValue(mValue) +
+ (mDefaultValueExists && mValue == mDefaultValue ? " (" +
+ getContext().getString(R.string.custom_seekbar_default_value) + ")" : "")
: "[" + getTextValue(mTrackingValue) + "]")));
- if (!mDefaultValueExists || mValue == mDefaultValue || mTrackingTouch)
- mResetImageView.setVisibility(View.INVISIBLE);
- else
- mResetImageView.setVisibility(View.VISIBLE);
- if (mValue == mMinValue || mTrackingTouch) {
- mMinusImageView.setClickable(false);
- mMinusImageView.setColorFilter(getContext().getColor(R.color.disabled_text_color), PorterDuff.Mode.MULTIPLY);
- } else {
- mMinusImageView.setClickable(true);
- mMinusImageView.clearColorFilter();
}
- if (mValue == mMaxValue || mTrackingTouch) {
- mPlusImageView.setClickable(false);
- mPlusImageView.setColorFilter(getContext().getColor(R.color.disabled_text_color), PorterDuff.Mode.MULTIPLY);
- } else {
- mPlusImageView.setClickable(true);
- mPlusImageView.clearColorFilter();
+ if (mResetImageView != null) {
+ if (!mDefaultValueExists || mValue == mDefaultValue || mTrackingTouch)
+ mResetImageView.setVisibility(View.INVISIBLE);
+ else
+ mResetImageView.setVisibility(View.VISIBLE);
+ }
+ if (mMinusImageView != null) {
+ if (mValue == mMinValue || mTrackingTouch) {
+ mMinusImageView.setClickable(false);
+ mMinusImageView.setColorFilter(getContext().getColor(R.color.disabled_text_color),
+ PorterDuff.Mode.MULTIPLY);
+ } else {
+ mMinusImageView.setClickable(true);
+ mMinusImageView.clearColorFilter();
+ }
+ }
+ if (mPlusImageView != null) {
+ if (mValue == mMaxValue || mTrackingTouch) {
+ mPlusImageView.setClickable(false);
+ mPlusImageView.setColorFilter(getContext().getColor(R.color.disabled_text_color), PorterDuff.Mode.MULTIPLY);
+ } else {
+ mPlusImageView.setClickable(true);
+ mPlusImageView.clearColorFilter();
+ }
}
}
@@ -187,8 +231,6 @@
mValue = newValue;
updateValueViews();
-
- notifyChanged();
}
}
@@ -203,6 +245,7 @@
mTrackingTouch = false;
if (!mContinuousUpdates)
onProgressChanged(mSeekBar, getSeekValue(mTrackingValue), false);
+ notifyChanged();
}
@Override