Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to update a ViewText every 0.1 sec?

Dear community,

this is my first post, I hope I'm right here.

Aim: A ViewText which displays an int variable (ofc I converted it at first) and update every 1/10 second. So I have a timer which shows every 0.1 second.

Problem: The text gets updated very often, but not properly. Its jumps 9 - 49 - 58 -73 - 120... (maybe a bit more steps, but that's how it seems to me).
The code (I am not proud of it, but I am new to this stuff. I'm thankful for comprehension and advices):

public string seconds = "";
public int time = 0;

t1 = new System.Timers.Timer();
            t1.Interval = 100;
            t1.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
            seconds= Convert.ToString(count);

protected void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
            this.time = time+ 1;
            seconds= Convert.ToString (time);
            TextView tv1 = FindViewById<TextView> (Resource.Id.textView1);
            tv1.Text = string.Format (sekunden);
        }

(this is ofc not the entire code, just the part which is related to the timer thing..)

Best Answer

Answers

  • I meant "TextView" ofc. Sorry, I was really tired atm I wrote this. ^^

  • CortezCortez USMember ✭✭✭

    [Activity (Label = "TextViewUpdate", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { System.Timers.Timer timer; int time = 0; bool started = false; protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Main); timer = new System.Timers.Timer (1000); timer.Elapsed += (sender, e) => { time++; RunOnUiThread(() => { FindViewById<TextView>(Resource.Id.textView1).Text = time.ToString(); }); }; Button btn1 = FindViewById<Button> (Resource.Id.btnStart); btn1.Click += (object sender, EventArgs e) => { if(!started) { this.timer.Start(); btn1.Text = "Stop"; this.started = true; } else { this.timer.Stop(); btn1.Text = "Start"; this.started = false; } }; } }

  • XamarinDoctusXamarinDoctus COMember ✭✭
    edited May 2016
    using System.Timers;
    
    
    
    private int _countSeconds;
    
    
    //This code into OnCreate method
    Timer _timer  = new Timer ();
    _timer.Interval = 1000;
    _timer.Elapsed += OnTimedEvent;
    _countSeconds = 5;
    _timer.Enabled = true;  
    
    
    
    private void OnTimedEvent (object sender, ElapsedEventArgs e)
    {
        _countSeconds--;
        if (_countSeconds < 0) {
            _timer.Stop (); 
        } else {
            _context.RunOnUiThread(() => 
                {
                    FindViewById<TextView>(Resource.Id.textView).Text = _countSeconds.ToString();
                });         
        }
    }
    
    
    
Sign In or Register to comment.