Custom SeekBarPreference control (developed by foreigners and used directly in the preferences file without additional code)

Source: Internet
Author: User

The following describes the SeekBarPreference control. The saved Value is a numeric Value. You can directly use the preferences. xml file without additional code.

The following figure shows the standard Android attributes used by the control.

  • Android: dialogMessageShow text in the dialog box
  • Android: textText information displayed after the progress bar is selected
  • Android: maxMaximum Value of SeekBar
/* The following code was written by Matthew Wiggins 
* and is released under the APACHE 2.0 license
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package com.hlidskialf.android.preference;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.preference.DialogPreference;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.LinearLayout;


public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener
{
private static final String androidns="http://schemas.android.com/apk/res/android";

private SeekBar mSeekBar;
private TextView mSplashText,mValueText;
private Context mContext;

private String mDialogMessage, mSuffix;
private int mDefault, mMax, mValue = 0;

public SeekBarPreference(Context context, AttributeSet attrs) {
super(context,attrs);
mContext = context;

mDialogMessage = attrs.getAttributeValue(androidns,"dialogMessage");
mSuffix = attrs.getAttributeValue(androidns,"text");
mDefault = attrs.getAttributeIntValue(androidns,"defaultValue", 0);
mMax = attrs.getAttributeIntValue(androidns,"max", 100);

}
@Override
protected View onCreateDialogView() {
LinearLayout.LayoutParams params;
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6,6,6,6);

mSplashText = new TextView(mContext);
if (mDialogMessage != null)
mSplashText.setText(mDialogMessage);
layout.addView(mSplashText);

mValueText = new TextView(mContext);
mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
mValueText.setTextSize(32);
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(mValueText, params);

mSeekBar = new SeekBar(mContext);
mSeekBar.setOnSeekBarChangeListener(this);
layout.addView(mSeekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

if (shouldPersist())
mValue = getPersistedInt(mDefault);

mSeekBar.setMax(mMax);
mSeekBar.setProgress(mValue);
return layout;
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
mSeekBar.setMax(mMax);
mSeekBar.setProgress(mValue);
}
@Override
protected void onSetInitialValue(boolean restore, Object defaultValue)
{
super.onSetInitialValue(restore, defaultValue);
if (restore)
mValue = shouldPersist() ? getPersistedInt(mDefault) : 0;
else
mValue = (Integer)defaultValue;
}

public void onProgressChanged(SeekBar seek, int value, boolean fromTouch)
{
String t = String.valueOf(value);
mValueText.setText(mSuffix == null ? t : t.concat(mSuffix));
if (shouldPersist())
persistInt(value);
callChangeListener(new Integer(value));
}
public void onStartTrackingTouch(SeekBar seek) {}
public void onStopTrackingTouch(SeekBar seek) {}

public void setMax(int max) { mMax = max; }
public int getMax() { return mMax; }

public void setProgress(int progress) {
mValue = progress;
if (mSeekBar != null)
mSeekBar.setProgress(progress);
}
public int getProgress() { return mValue; }
}

The following is an example of XML layout.

 <com.hlidskialf.android.preference.SeekBarPreference android:key="duration"
android:title="Duration of something"
android:summary="How long something will last"
android:dialogMessage="Something duration"
android:defaultValue="5"
android:text=" minutes"
android:max="60"
/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.