Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8399451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:18:29+00:00 2026-06-09T21:18:29+00:00

I’m using a custom dialog preference to generate a seekbar in my preferences menu.

  • 0

I’m using a custom dialog preference to generate a seekbar in my preferences menu.
After researching seekbar implementation, to obtain the float value I need from the seekbar I have written the following code:

public void onProgressChanged(SeekBar seek, int newValue,
        boolean fromTouch) {
    // Round the value to the closest integer value.
    if (stepSize >= 1) {
        value = Math.round(newValue/stepSize)*stepSize;

    }
    else {
        value = newValue;
    }

    // Set the valueText text.
    float sValue = newValue*10;
    sValue = sValue/100;

    valueText.setText(Float.toString(sValue));

Which produces the float that I want. However, I want to be able to use this float in my main activity. I have attempted to store it using SharedPreferences using:

userPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Editor editor = userPrefs.edit()
editor.putFloat("mpm", sValue);
editor.commit();

Which is how I’ve learned to use SharedPreferences in a class extending Activity.

However as this seekbar extends Dialogue Preference I cannot use

getBaseContext()

As I get the error that the method getBaseContext is undefined for this type.

I have tried changing getBaseContext() to getContext() but this has been unsuccessful although that may be because I am unfamiliar with this implementation.

How can I save this float from the dialogue preference and use the value in a different class?

The code I am using to retrieve SharedPreferences:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.logbook);
    initialise();
    userPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    list = userPrefs.getString("list", "10");
    userswallstring = userPrefs.getString("height", "10.0");


    try {
        usersWall = Float.valueOf(userswallstring.trim());
    } catch (Exception e) {
        // TODO: handle exception
    }
    mpm = userPrefs.getFloat("mpm", 2);

Mpm.class:
package com.gbclimber.ep;

 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.SharedPreferences;

 import android.content.SharedPreferences.Editor;
 import android.content.res.TypedArray;
 import android.preference.DialogPreference;
 import android.preference.PreferenceManager;

 import android.util.AttributeSet;

 import android.view.LayoutInflater;
 import android.view.View;

 import android.widget.SeekBar;
 import android.widget.TextView;


 public class Mpm extends
    DialogPreference implements SeekBar.OnSeekBarChangeListener {
// Layout widgets.
private SeekBar seekBar = null;
private TextView valueText = null;

// Custom xml attributes.
private int maximumValue = 0;
private int minimumValue = 0;
private int stepSize = 0;
private String units = null;

private int value = 0;
SharedPreferences userPrefs;
/**
 * The SeekBarDialogPreference constructor.
 * @param context of this preference.
 * @param attrs custom xml attributes.
 */
public Mpm(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray typedArray = context.obtainStyledAttributes(attrs,
        R.styleable.Mpm);

    maximumValue = typedArray.getInteger(
        R.styleable.Mpm_maximumValue, 0);
    minimumValue = typedArray.getInteger(
        R.styleable.Mpm_minimumValue, 0);
    stepSize = typedArray.getInteger(
        R.styleable.Mpm_stepSize, 1);
    units = typedArray.getString(
        R.styleable.Mpm_units);

    typedArray.recycle();
}
/**
 * {@inheritDoc}
 */
protected View onCreateDialogView() {

    LayoutInflater layoutInflater = LayoutInflater.from(getContext());

    View view = layoutInflater.inflate(
        R.layout.mpmdp, null);

    seekBar = (SeekBar)view.findViewById(R.id.seekbar);
    valueText = (TextView)view.findViewById(R.id.valueText);

    // Get the persistent value and correct it for the minimum value.
    value = getPersistedInt(minimumValue) - minimumValue;

    // You're never know...
    if (value < 0) {
        value = 0;
    }

    seekBar.setOnSeekBarChangeListener(this);
    seekBar.setKeyProgressIncrement(stepSize);
    seekBar.setMax(maximumValue - minimumValue);
    seekBar.setProgress(value);

    return view;
}
/**
 * {@inheritDoc}
 */
public void onProgressChanged(SeekBar seek, int newValue,
        boolean fromTouch) {
    // Round the value to the closest integer value.
    if (stepSize >= 1) {
        value = Math.round(newValue/stepSize)*stepSize;

    }
    else {
        value = newValue;
    }

    // Set the valueText text.
    float sValue = newValue*10;
    sValue = sValue/100;
    userPrefs = PreferenceManager
            .getDefaultSharedPreferences(getContext());
    Editor editor = userPrefs.edit();
    editor.putFloat("mpm", sValue);
    editor.commit();
    valueText.setText(Float.toString(sValue));

    callChangeListener(value);
}
/**
 * {@inheritDoc}
 */
public void onStartTrackingTouch(SeekBar seek) {
}
/**
 * {@inheritDoc}
 */
public void onStopTrackingTouch(SeekBar seek) {
}
/**
 * {@inheritDoc}
 */
public void onClick(DialogInterface dialog, int which) {
    // if the positive button is clicked, we persist the value.
    if (which == DialogInterface.BUTTON_POSITIVE) {
        if (shouldPersist()) {
            persistInt(value + minimumValue);
        }
    }

    super.onClick(dialog, which);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
    // TODO Auto-generated method stub
    super.onDialogClosed(positiveResult);
    persistInt(value + minimumValue);
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:18:31+00:00Added an answer on June 9, 2026 at 9:18 pm

    Just did a quick search but for your context try

    this.getDialog().getContext()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.