Skip to content
Advertisement

SeekBar – Can’t set starting point

When I first start my app, before the Shared Preferences are created, I want my seekbar to be set on 300px (max 600). It seems simple, as all I need to do is use setProgress and give it a value of 300. But for some reason this is not working in my code, and whatever I do, the starting point remains at 0. I was wondering if you had any idea what I am doing wrong. Many thanks.

    public void sizeButton(View v) {

    if (v.getId() == R.id.size_btn) {
        seekDialog = new Dialog(this);
        seekDialog.setContentView(R.layout.size_chooser);
        seekTxt = (TextView) seekDialog.findViewById(R.id.spq_txt);
        seekSpq = (SeekBar) seekDialog.findViewById(R.id.size_seek);
        seekSpq.setMax(600);

        SharedPreferences prefs = getSharedPreferences(LauncherPage.PREFS, MODE_PRIVATE);
        if (prefs != null) {
            int currSize = prefs.getInt("size", size);
            seekSpq.setProgress(currSize);
            seekTxt.setText(Integer.toString(currSize) + " pixels");
        } else {
            seekSpq.setProgress(300);
        }
    }

To be clear “seekSpq.setProgress(currSize);” works just fine. The one that doesn’t work is “seekSpq.setProgress(300);”.

Answer

SharedPreferences prefs = getSharedPreferences(LauncherPage.PREFS, MODE_PRIVATE);
int currSize = prefs.getInt("size", size);
if (currSize !=0) {
    seekSpq.setProgress(currSize);
    seekTxt.setText(Integer.toString(currSize) + " pixels");
}else{
    seekSpq.setProgress(300);
}
Advertisement