Android: Display a Toast in OnSeekBarChangeListener?

Go To StackoverFlow.com

2

I'd like to display the value of the seekbar when the user moves it, similar to the way the Contacts ListView displays the letter of the section you're in when you scroll it (Android 2.3.x).

Anyone have a suggestion of how to implement it? I was thinking a Toast in onProgressChanged() method of OnSeekBarChangeListener but having trouble getting the context to show() the Toast.

EDIT: Also, the Seekbar is in a row of a ListView so OnSeekBarChangeListener is in a class extendending ArrayAdapter and not the usual "Activity" so things like getBaseContext() are not available to the method.

2012-04-04 21:25
by wufoo


1

you can use :

Toast.makeText(Main.this, "Message", Toast.LENGTH_LONG).show();

or

Toast.makeText(getBaseContext(), "Message", Toast.LENGTH_LONG).show();

First way:use Application class globally

public class MyApp extends Application {
    private static MyApp instance;

    public static MyApp getInstance() {
        return instance;
    }

    public static Context getContext(){
        return instance.getApplicationContext();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }
}

Second way: Pass Context to your class constructor or to a method in class

2012-04-04 21:29
by ρяσѕρєя K
Forgot to mention, I have the Seekbar in a listview, so the listener is in a class extending ArrayAdapter. I don't have getBaseContext() available - wufoo 2012-04-04 21:33


1

Thanks for the replies. I used a little from both suggestions and came up with this, but even at 500ms the Toast doesn't update fast enough to keep up with the seekBar changes. hm...

public class adapListControl extends ArrayAdapter <String>
{
   final Context mCtx;
   ...

   public adapListControl (Context context, int textViewResourceId)
   {
      super (context, textViewResourceId);
      this.mCtx = context;
   }
   ...

   OnSeekBarChangeListener osbl = new OnSeekBarChangeListener ()
   {
      @Override
      public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser)
      { 
         Log.d (TAG, " prog change:" + progress);
         Toast.makeText(mCtx, "" + progress, 500).show();
      }
      ...
   }
2012-04-05 13:47
by wufoo
Ads