Seekbar setting TextView text

Hello,

I am building my first android app, and want to use a seekbar to change a textview value e.g. seekbar at far left shows a big 0, and as you slide right that increases to 12.

I have set the event handler correctly for it (I believe) by creating a new class for it, so in my OnCreate event

SeekBar sb = FindViewById(Resource.Id.seekBarNoEggs);
sb.Progress = 5;
sb.Max = 12;
sb.SetOnSeekBarChangeListener(new SeekBarListener());

and my event class

public class SeekBarListener : Java.Lang.Object, SeekBar.IOnSeekBarChangeListener
{
public void OnProgressChanged(SeekBar seekBar, int progress, bool fromUser)
{
var tv = FindViewById(Resource.Id.textViewNoEggs);
}

public void OnStartTrackingTouch(SeekBar seekBar)
{
}

public void OnStopTrackingTouch(SeekBar seekBar)
{
}

}

Now the obvious problem is that my SeekBarListener class doesn't inherit from Activity, so has no idea what "FindViewById" is.

How do I reference my TextView from within the OnProgressChanged event please? Or have I gone about this totally the wrong way?

Thank you

Russ

Posts

  • TeHaTeHa DEMember ✭✭✭

    first possibility:
    also derived your activity-class from IOnSeekBarChangeListener, like that:
    [Activity]
    public class MyActivity : Activity, SeekBar.IOnSeekBarChangeListener
    { ... }

    or the a second (.net/c# typical) possibility:
    Use a global TextView an register an event on you seekbar, like that:
    [Activity]
    public class MyActivity : Activity
    {
    TextView tv;
    SeekBar sb;

     protected override void OnCreate(Bundle bundle)
     {
       SetContentFiew(123);
       tv = (TextView)FindViewById(Resource.Id.tv);
       sb = (SeekBar)FindViewById(Resource.Id.sb);
       sb.ProgressChanged += delegate(object sender, SeekBar.ProgressChanedEventArgs e) { /*Do Event Handling*/ };
       //or
       sb.ProgressChanged += new EventHandler<SeekBar.ProgressChangedEventArgs>>(sb_ProgressChanged);
     }
    
     void sb_ProgressChanged(object sender, SeekBar.ProgressChanedEventArgs e)
     {
       //Do your event-handling here
     }  
    }
    

    You can also register delegates for the events 'StartTrackingTouch' and 'StopTrackingTouch'

  • StevaSteva RSMember ✭✭✭

    If you implement that interface in your activity instead of new class your problem will be solved.
    It should be possible to do it your way if you pass the context from activity but I'm not really sure. Anyway implementing it inside the activity is the right way to do it.

  • RussRuss GBMember

    Thank you both!

    Firstly, I didn't realise I had created a second class, rather than a subclass :)

    I ended up creating a global reference to the text view like this, and passing the context around,

    public class Activity1 : Activity
    {
        TextView textViewNumberOfEggs;
        SeekBar seekBar;
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
            textViewNumberOfEggs = FindViewById<TextView>(Resource.Id.textViewNumberOfEggs);
    

    Then in my listener class

        public class SeekBarListener : Java.Lang.Object, SeekBar.IOnSeekBarChangeListener
        {
            Activity1 context;
    
            public SeekBarListener(Activity1 context)
            {
                this.context = context;
            }
    
            public void OnProgressChanged(SeekBar seekBar, int progress, bool fromUser)
            {
                context.textViewNumberOfEggs.Text = progress.ToString();
            }
    

    However to me (maybe naive) creating a subclass seems wrong, so I'll try to follow your advice and do it again correctly. I'll let you know how that goes, thanks.

  • RussRuss GBMember

    B e a utiful, thank you

    public class Activity1 : Activity
    {
        TextView textViewNumberOfEggs;
        SeekBar seekBar;
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
            textViewNumberOfEggs = FindViewById<TextView>(Resource.Id.textViewNumberOfEggs);
            seekBar = FindViewById<SeekBar>(Resource.Id.seekBarNumberOfEggs);
    
            seekBar.Progress = 0;
            seekBar.Max = 12;
    
            seekBar.ProgressChanged += new EventHandler<SeekBar.ProgressChangedEventArgs>(seekBarProgressChanged);
        }
    
        void seekBarProgressChanged(object sender, SeekBar.ProgressChangedEventArgs e)
        {
            textViewNumberOfEggs.Text = e.Progress.ToString();
        }
    
  • pratik90pratik90 INMember ✭✭

    @RussellTaylor Please let me know how can i use in Xamarin forms?

Sign In or Register to comment.