Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom inline SeekBarPreference - how to set SeekBar progress on the 1st run?

I have prepared a simple test project for my question at GitHub.

In my project there is a custom inline SeekBarPreference, which mostly works fine (its summary is updated when seekbar is being dragged and it saves integer value):

app screenshot

However there is a problem:

On the very 1st run of the app (you might need to uninstall my app when you try see the error again) the progress of the SeekBar is not set (but the summaries are set):

app screenshot

My question is: how to fix this issue in my code?

I have tried adding mSeekBar.setProgress(mProgress) in different spots of SeekBarPreference.java, but just can't find the correct place for that code.

Below are excerpts from my source code (in case Stackoverflow ever outlives GitHub) -

MainActivity.java:

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

getFragmentManager().beginTransaction()
    .addToBackStack(null)
    .replace(R.id.root, new PrefFragment(), "prefs")
    .commit();

PrefFragment.java:

@Override
public void onResume() {
    super.onResume();

    SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
    // set the summaries from saved values
    onSharedPreferenceChanged(prefs, SEEK_1);
    onSharedPreferenceChanged(prefs, SEEK_2);
    prefs.registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {

    if (SEEK_1.equals(key)) {
        int i1 = prefs.getInt(key, DEFAULT_1);
        mSeek1.setSummary("$ " + i1);
    } else if (SEEK_2.equals(key)) {
        int i2 = prefs.getInt(key, DEFAULT_2);
        mSeek2.setSummary("$ " + i2);
    }       
}  

SeekBarPreference.java (the complete source code):

public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {

    private SeekBar mSeekBar; // FIXME how to set its progress?
    private int mProgress;

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

    @Override
    protected View onCreateView(ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.preference_seekbar, parent, false);
        // can also be done in onBindView, does not work either
        mSeekBar = (SeekBar) view.findViewById(R.id.seekbar);
        mSeekBar.setProgress(mProgress);
        mSeekBar.setOnSeekBarChangeListener(this);
        return view;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (!fromUser)
            return;

        setValue(progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // not used
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // not used
    }

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        setValue(restoreValue ? getPersistedInt(mProgress) : (Integer) defaultValue);
    }

    public void setValue(int value) {
        if (shouldPersist()) {
            persistInt(value);
        }

        if (value != mProgress) {
            mProgress = value;
            notifyChanged();
        }
    }
}

UPDATE:

What I have unsuccessfully tried sofar -

1) Adding the following lines to the onCreateView() method:

    int progress = getPersistedInt(mProgress); // shows 0 in debugger
    mSeekBar.setProgress(progress);

2) Adding the following lines to the setValue() method:

    if (value != mProgress) {
        mProgress = value;
        mSeekBar.setProgress(mProgress);
        notifyChanged();
    }

Alas this does not work, the progress of the mSeekBar stays at 0.

Also, there is a SeekBarPreference by Google - but I don't understand how it works there (or if it works at all).

like image 856
Alexander Farber Avatar asked May 11 '15 14:05

Alexander Farber


People also ask

How do I change the progress of a SeekBar?

In Order to the seekBar to Change while you are in the Activity you need to Call the method setProgress(), and pass it a boolean animate value.

How do I manage SeekBar on Android?

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.

How do you implement SeekBar?

Step1: Create a new project. After that, you will have java and XML file. Step2: Open your xml file and add a SeekBar and TextView for message as shown below, max attribute in SeekBar define the maximum it can take. Assign ID to SeekBar And TextView.

How do I change my SeekBar color?

If you are using default SeekBar provided by android Sdk then their is a simple way to change the color of that . just go to color. xml inside /res/values/colors. xml and change the colorAccent.


1 Answers

You need to override onGetDefaultValue in SeekBarPreference.

Try this:

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
    return a.getInt(index, 0);
}
like image 157
Mattia Maestrini Avatar answered Oct 04 '22 23:10

Mattia Maestrini