Monday, 13 August 2012

Seekbar (Android)

package com.test.seekbar;

import com.test.seekbar.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.preference.DialogPreference;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public final class SeekBarPreference extends DialogPreference implements OnSeekBarChangeListener {

    // Default values
    private static final int DEFAULT_CURRENT_VALUE = 50;
    private static final int DEFAULT_MIN_VALUE = 0;
    private static final int DEFAULT_MAX_VALUE = 100;

    private final int DefaultValue;
    private final int MaxValue;
    private final int MinValue;
   
    private int mCurrentValue;
   
    private SeekBar SeekBar;
    private TextView ValueText;

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

    MinValue = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/com.mnm.seekbarpreference", "minValue", DEFAULT_MIN_VALUE);
    MaxValue = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/com.mnm.seekbarpreference", "maxValue", DEFAULT_MAX_VALUE);
    DefaultValue = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "defaultValue", DEFAULT_CURRENT_VALUE);
    }

    @Override
    protected View onCreateDialogView() {
    mCurrentValue = getPersistedInt(DefaultValue);

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.dialog_slider, null);

    ((TextView) view.findViewById(R.id.min)).setText(Integer.toString(MinValue));
    ((TextView) view.findViewById(R.id.max)).setText(Integer.toString(MaxValue));

    SeekBar = (SeekBar) view.findViewById(R.id.seek_bar);
    SeekBar.setMax(MaxValue - MinValue);
    SeekBar.setProgress(mCurrentValue - MinValue);
    SeekBar.setOnSeekBarChangeListener(this);

    ValueText = (TextView) view.findViewById(R.id.current_value);
    ValueText.setText(Integer.toString(mCurrentValue));

    return view;
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

        if (!positiveResult) {
            return;
        }
   
        if (shouldPersist()) {
            persistInt(mCurrentValue);
        }

        notifyChanged();
    }

    @Override
    public CharSequence getSummary() {
        String summary = super.getSummary().toString();
        int value = getPersistedInt(DefaultValue);
        return String.format(summary, value);
    }
   
    public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
        mCurrentValue = value + MinValue;
        ValueText.setText(Integer.toString(mCurrentValue));
    }

    public void onStartTrackingTouch(SeekBar seek) {
        //ValueText.setText(getString(R.string.seekbar_on));
    }

    public void onStopTrackingTouch(SeekBar seek) {
        // ValueText.setText(getString(R.string.seekbar_off)); 
}

No comments:

Post a Comment