Monday, February 13, 2012

SeekBarPreference Example for Android


import android.app.AlertDialog.Builder;
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;

public class SeekBarPreference extends DialogPreference {
private final String tag = "SeekBarPreference";
private final String SENSITIVITY_LEVEL_PREF = "sensitivity";
private final String VOLUME_LEVEL_PREF = "volume";

    private Context context;
    private SeekBar sensitivityLevel = null;
    private LinearLayout layout = null;
    public SeekBarPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        persistInt(10);
    }
   
    protected void onPrepareDialogBuilder(Builder builder) {
        layout = new LinearLayout(context);
        layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        layout.setMinimumWidth(400);
        layout.setPadding(20, 20, 20, 20);
        sensitivityLevel = new SeekBar(context);
        if (this.getKey().equalsIgnoreCase(SENSITIVITY_LEVEL_PREF)) {
        sensitivityLevel.setMax(100);
        }
        else {
        sensitivityLevel.setMax(10);
        }
        sensitivityLevel.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    sensitivityLevel.setProgress(getPersistedInt(10));
        layout.addView(sensitivityLevel);
        builder.setView(layout);
        //super.onPrepareDialogBuilder(builder);
    }
   
    protected void onDialogClosed(boolean positiveResult) {
        if(positiveResult){
            persistInt(sensitivityLevel.getProgress());
        }
        super.onDialogClosed(positiveResult);
    }
}

No comments:

Post a Comment