Forum Xamarin.Android

How to update a Button Text using a timer

Hi,

I need help as mentioned in the title.
I have a timer routine that will update the text of button. But it doesn't seem to update then the timer routine is executed.

private void tmrRead_Tick(System.Object sender, System.EventArgs e)
{
bnt.Text = "Go";
}

I am using C#. Please help me with this problem.

Thanks

Posts

  • StevenTheEvenStevenTheEven SEMember ✭✭✭

    RunOnUiThread (() => textview.Text = "Method Complete");

  • AlvinKwekAlvinKwek USMember ✭✭

    May I know how do I implement this statement in C#?

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    That is in C#.

  • CortezCortez USMember ✭✭✭

    Tutorial: docs.xamarin.com/guides/android/advanced_topics/writing_responsive_applications

    You have default a UI thread. You create in this thread a Timer. When the timer fired it creates a new background thread. Now you have two thread. Conventionally from the background thread you cannot update the UI. In C# Visual Studio you can do it with MethodInvoker or with delegate. But here in Xamarin Studio or in Android you can do it just with RunOnUiThread. Very simple solution and i like it. In C# VS it's a bit difficult.

  • AlvinKwekAlvinKwek USMember ✭✭

    Button[] arrBn = new Button[17];

    private void tmrRead_Tick(System.Object sender, System.EventArgs e)
    {

    for (i = 1; i <= 16; i++)
    {
    RunOnUiThread(() => arrBn[i].Text = "*" );

    }
    }

    Above is a portion of my code.
    I declare an array of buttons and i am supposed to initial the button text in a timer routine.

    But I was thrown with a exception "Array index is out of range." where value of "i" is 17..

    I do not know why it happens. Please help me

  • CheesebaronCheesebaron DKInsider, University mod

    arrays are 0 indexed. So the array arrBn goes from 0 to 16. This means 17 and above is out of bounds the same with -1 and below.

  • AlvinKwekAlvinKwek USMember ✭✭

    Yes.. I know but did you notice that it is in a for loop "for (i = 1; i <= 16; i++)"??

    Logically, value of "i" should not reach 17

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    How are you declaring "i"?

  • AlvinKwekAlvinKwek USMember ✭✭

    "i" is declared in the timer routine...

    private void tmrRead_Tick(System.Object sender, System.EventArgs e) {

    byte i = 0;

    for (i = 1; i <= 16; i++) {
    RunOnUiThread(() => arrBn[i].Text = "*" );

    }

    }

  • StevenTheEvenStevenTheEven SEMember ✭✭✭

    Why are you initiating the buttons text in a timer?

  • AlvinKwekAlvinKwek USMember ✭✭

    The reason to do so is because the application is connected to a server where the button text will be updated base on the server's instruction. When the application first start up, this timer routine will continuously poll the server for instruction on updating the button text.
    Once its is updated, this routine will stop.

    Anyway, to work around the problem, I have tentatively solve it by below codes.

    RunOnUiThread(() => arrBn[1].Text = arrTextfromServer[1]);
    RunOnUiThread(() => arrBn[2.Text = arrTextfromServer[2]);
    RunOnUiThread(() => arrBn[3.Text = arrTextfromServer[3]);
    RunOnUiThread(() => arrBn[4.Text = arrTextfromServer[4]);
    |
    |
    |

    RunOnUiThread(() => arrBn[16.Text = arrTextfromServer[16)

    But I still hope to know what happen to this strange result when using it in a for-next loop.

    Please help me.

  • CortezCortez USMember ✭✭✭

    This is nice and beautiful, but as Cheesebaron said the arrays are indexed from 0. You have a static datastructure(array). If you want to do that you can place the buttons in it from 1. And in the iterations you can handle it from 1, but the first index 0 is unused. Your iteration works well. Start from 1 to the end of the array. I place a value at the 0 index and it wasn't processed by the iteration. In this situation you waste your resource. There is a place for one more button. Fine. But it is very rare when you use the exact value, now for example 17. You should use the array.Length, or list.Count property. You have 3 possibilities: the arrTextfromServer and the arrBn element number is the same. You have fewer element in the arrBn or more element in the arrBn like the arrTextfromServer. To not to receive array is out of the range exception try to use your array Length property

    for (int i = 0; i < arrBn.Length; i++) { RunOnUiThread(() => arrBn[i].Text = "*" ); }

  • AlvinKwekAlvinKwek USMember ✭✭

    Thanks Cortez, I will try out your code as below
    for (int i = 0; i < arrBn.Length; i++)
    {
    RunOnUiThread(() => arrBn[i].Text = "*" );
    }

    but from a normal programming logic, if i code as below, value of i in "arrBn[i].Text" should not have reached the value of "17". this is what I have not encountered before and puzzled me..
    In fact, this source code is originally implemented in Visual Studio Windows Application. There isn't any problem.

    for (int i = 0; i <= 16; i++)
    {
    RunOnUiThread(() => arrBn[i].Text = "*" );
    }

  • CheesebaronCheesebaron DKInsider, University mod

    @Cortez seriously, please use the enter button to make line breaks!

  • AlvinKwekAlvinKwek USMember ✭✭

    I got it with below statements.... thanks for all the advice....

    for (int i = 0; i < 16; ++i)
    {
    RunOnUiThread(() => arrBn[i].Text = "*" );
    }

  • foxsefoxse USMember

    Please, help!
    how to update UI multiply times?

    imageView = FindViewById(Resource.Id.imageView1);
    imageView2 = FindViewById(Resource.Id.imageView2);

                    _timer = new System.Threading.Timer((o) =>
                    {
                        RunOnUiThread(() =>
                        {
                            var now = DateTime.Now;
                            float mAngl = now.Second * 6;
                            float  hAgl = now.Hour * 30;
    
                            imageView.SetScaleType(Android.Widget.ImageView.ScaleType.Matrix);   //required
                            imageView2.SetScaleType(Android.Widget.ImageView.ScaleType.Matrix);   //required
                            imageView.ImageMatrix.PostRotate(mAngl, imageView.Width / 2, imageView.Height / 2);
                            imageView2.ImageMatrix.PostRotate(hAgl, imageView.Width / 2, imageView.Height / 2);
    
                        });
                    }
                    , null, 0, 1000);
    
Sign In or Register to comment.