Ovais Khan
Ovais Khan

Reputation: 205

Update Textview text multiple times with few seconds delay

I am trying to update textview text several times but only last text is displayed on screen. I basically want to delay text appearance so that user gets time to read the message. Below is the code snippet that i have tried. Kindly guide. I am using xamarin.

class SplashScreen : Activity
{

    public SplashScreen() 
    {

    } 

    protected override void OnCreate(Bundle bundle)
    {

        base.OnCreate(bundle);
        RequestWindowFeature (Android.Views.WindowFeatures.NoTitle);
        SetContentView(Resource.Layout.splash_screen);


        MyTextView tv0 = FindViewById<MyTextView> (Resource.Id.BookIntro);
        tv0.Text = "START";

        ThreadPool.QueueUserWorkItem(d =>
        {

        //some code here 


        RunOnUiThread (() => {

            tv0.Text = "HI";
            System.Threading.Thread.Sleep(2000);
            tv0.Text = "HELLO";
            System.Threading.Thread.Sleep(2000);
            tv0.Text = "HOW ARE YOU";
            System.Threading.Thread.Sleep(2000);
            tv0.Text = "WELCOME TO ANDROID";
            System.Threading.Thread.Sleep(2000);
            tv0.Text = "BYE BYE";
            });
        });
    }
}

The above code displayed text "START" and then sleeps for (2+2+2+2 = 8) seconds and then displays only last text (BYE BYE). Kindly guide.

Upvotes: 1

Views: 1278

Answers (4)

mgabz
mgabz

Reputation: 733

I tried @Sven-Michael Stübe answer as follows:

EditText QtyChange = FindViewById<EditText>(Resource.Id.txt_qty_change);
this.QtyChange.TextChanged += QtyChange_TextChanged;

private async void QtyChange_TextChanged(object sender, TextChangedEventArgs e)
            {
                await System.Threading.Tasks.Task.Delay(600);

                int newQuant;
                if (Int32.TryParse(e.Text.ToString(), out newQuant))
                {
                    // Update text here:
                    something.Text = newQuant
                    adapter.NotifyItemChanged(AdapterPosition);
                }
            }

Upvotes: 0

Sven-Michael St&#252;be
Sven-Michael St&#252;be

Reputation: 14750

Use the power of asnyc/await :) It keeps your UI responsive and you don't have to dispatch stuff back to the UI thread manually, because it saves the context.

protected override void OnCreate(Bundle bundle)
{

    base.OnCreate(bundle);
    RequestWindowFeature (Android.Views.WindowFeatures.NoTitle);
    SetContentView(Resource.Layout.splash_screen);

    UpdateTextAsync();
}

private async void UpdateTextAsync() 
{
    MyTextView tv0 = FindViewById<MyTextView> (Resource.Id.BookIntro);
    tv0.Text = "HI";
    await Task.Delay(2000);
    tv0.Text = "HELLO";
    await Task.Delay(2000);
    tv0.Text = "HOW ARE YOU";
    await Task.Delay(2000);
    tv0.Text = "WELCOME TO ANDROID";
    await Task.Delay(2000);
    tv0.Text = "BYE BYE";
}

Upvotes: 1

Dudi Keleti
Dudi Keleti

Reputation: 2984

You run the update text and the Thread.Sleep on the UI thread, so the UI not receive the change itself because it immediately go to sleep, after that last text update the UI receive control and update itself.

To view the updating text you need to update your text with Timer (you have several options, all of them very simple to use. just google it) or to do the updating and the waiting (Thread.Sleep) in background (via Task for example), and send the update request to UI.

Just remember that if you updaeting the UI from non UI thread, you need yo do it with Send\Post and not directly. (check Dispatcher.Invoke\BeginInvoke for WPF or other synchronization context for other platforms).

(I don't give specific implementation because you can choose form variant possibilities, if you want any specific example, please comment)

Upvotes: 0

Shree Krishna
Shree Krishna

Reputation: 8562

Do like this,

new CountDownTimer(8000, 1000) {

 public void onTick(long millisUntilFinished) {
     setText("step"+millisUntilFinished);
 }

 public void onFinish() {
     //Do something
 }
}.start();

in setText() method

public void setText(String value){
   tv0.Text = value; //Set value to TextView here
}

Upvotes: 0

Related Questions