TextView doesn't update properly (a bit late)

I'm trying to update the text of a TextView when I click a ImageView.
Every time you click on the ImageView, an integer is being incremented and that value is being set to the TextView.

Now, when I first click on the ImageView, the default text is gone (text = "") in the app, but if I debug the application, TextView.Text = 1.
On my second click the value gets set to 2, but the text shows 1. The text is always 1 update late, which is weird because in debug the values are correct.

Here's my code:

private int count= 0;

    private ImageView image;
    private TextView tv;

    protected override void OnCreate(Bundle bundle)

        // Set our view from the "main" layout resource

        image= FindViewById<ImageView>(Resource.Id.image1);
        tv = FindViewById<TextView>(Resource.Id.textView1);
        tv.Text = "0";

        image.Click += delegate
            this.RunOnUiThread(() => tv.Text = Convert.ToString(count));
            this.RunOnUiThread(() => tv.PostInvalidate());

Best Answer


  • Rakete1111Rakete1111 USMember

    Thanks for the answer!
    But it doesn't work, it shows the same output.
    But thanks @rzee7

  • Rakete1111Rakete1111 USMember

    Thanks @rzee7
    But I don't see the difference it would make as opposed to what I already have, but, I still tried it of course, and nope, it doesn't work.
    Maybe as extra info:
    If I don't call PostInvalidate, then the TextView will skip 3 updates, instead of 1. Weird,

    Thanks for your help!

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    Better yet, post up an example project that re-produces the issue. Also are you running this on an emulator or an actual device? Post up more info about which emulator/device you're using.

  • Rakete1111Rakete1111 USMember

    It's weird, but I can reproduce this issue in the default Blank App from Android from Xamarin :)
    If I click the click me button, first the text disappears for 2 clicks, then it starts to increment.

    I'm using the VS Emulator, both on the 5" Lollipop 5.1.1 and the 4.7" KitKat 4.4 emulator.

    In the attached file is the whole project solution (it's the unmodified Blank App).
    Thanks for the info!

  • Rakete1111Rakete1111 USMember

    That could explain the issue. Thanks a lot!
    No, I haven't tried any of those, but I will try.

    Thank you!

  • Rakete1111Rakete1111 USMember

    Yup, it's an emulator problem, thanks guys!

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    Thanks for the update!

Sign In or Register to comment.