kotozna
kotozna

Reputation: 331

how to repeatedly update android TextView to simulate animation

I would like the text in a TextView to constantly change once a button in the menu is pressed. Here is my menu's onOptionsItemSelected:

public boolean onOptionsItemSelected(MenuItem item) {


    switch(item.getItemId()){
    case R.id.menu_animate:
        for(int i=0;i<100;i++){
            MessageIn.setText("here"+i);
        }
        break;

    }

  return super.onOptionsItemSelected(item);
}

As you can see, if onOptionsItemSelected thinks the "menu_animate" button was pressed in the menu, it goes on to modify the text in my TextView called "MessageIn".

I've tried to get it to repeatedly display some text with a number counting up from 0 to 99. However when I run the app and press the button, I only see "here99" displayed in my TextView. So it looks like all of the code is executed before the final result is displayed.

How can I get it to refresh the display in the TextView each time the number i is updated?

Thanks.

Upvotes: 0

Views: 1383

Answers (2)

Devrim
Devrim

Reputation: 15533

You can use a Handler and a Runnable for your purpose. I'm sharing an edited code which I currently use just like a purpose of yours.

You can directly copy the class and layout, then try it.

Main idea is using a Handler with its post and postDelayed methods with a Runnable. When you pust the button, it start to change text on every 3 seconds. If you push the button again it resets itself.

Try this code, try to understand it. Handlers can be used for many purpose to change UI. Read more about it.

MainActivity.java:

public class MainActivity extends Activity {

    private Handler handler;
    private TextView textView;

    private TextUpdateRunnable textUpdateRunnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        handler = new Handler();

        textView = (TextView)findViewById(R.id.textView);

        Button startButton = (Button)findViewById(R.id.button);
        startButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(textUpdateRunnable != null) {
                 handler.removeCallbacks(textUpdateRunnable);
                }
                TextUpdateRunnable newTextUpdateRunnable = new TextUpdateRunnable(handler, textView); 
                textUpdateRunnable = newTextUpdateRunnable;
                handler.post(textUpdateRunnable);
            }
        });
    }

    private static class TextUpdateRunnable implements Runnable {
        private static final int LIMIT = 5;

        private int count = 0;
        private Handler handler;
        private TextView textView;

        public TextUpdateRunnable(Handler handler, TextView textView) {
            this.handler = handler;
            this.textView = textView;
        }

        public void run() {
            if(textView != null) {
                textView.setText("here" + count);
                count++;

                if(handler != null && count < LIMIT) {
                    handler.postDelayed(this, 3000);
                }
            }
        }
    };
}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="BUTTON" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Upvotes: 1

Paulo Roberto Rosa
Paulo Roberto Rosa

Reputation: 3285

See on this answer https://stackoverflow.com/a/3039718/2319589 how to use the Sleep with the Runnable() and implement your code this way.

Should work.

Good luck :)

Upvotes: 0

Related Questions