Search code examples
javaandroidandroid-widgetdelay

How to change a textview text with a delay without using threads in android?


I need to sequentially show a changing text with 1 second or half a second between changes. How can i achieve this without using threads in android?


Solution

  • I would just use a handler and a runnable in your activity, fairly simple

        Handler textNextHandler = new Handler(); 
    
        final Runnable textTimer = new Runnable() 
        {
            public void run() 
            {
               // ROTATE YOUR TEXT HERE THEN TELL IT HOW LONG TO DELAY UNTIL NEXT
               textNextHandler.postDelayed(this, 500);  
            }
        };
    
        @Override
        public void onResume() {
            super.onResume();
            textNextHandler.postDelayed(textTimer,500); 
        }       
    
        @Override
        public void onPause() {
            super.onPause();
            textNextHandler.removeCallbacks(textTimer);
        }