Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to continuously switch textswitcher between two values every 2 seconds

I've looked at the code here as well as the code here but I still can't seem to get my code to work right. With the 2nd link, I can get a "timer" that counts up on the page, but with the first, my UI locks up. What I'm trying to do is have a seperate thread that continually flips the text in a textswitcher every 3 seconds as long as the app is open. I need it to switch between two values, and have tried something like the following:

private Runnable mUpdateTimeTask = new Runnable() {


       public void run() {
           while(true){                                                             
                try {
                    mHandler.post(new Runnable(){
                       @Override
                       public void run() {

                          try {
                            mSwitcher.setText("ON");  
                            Thread.sleep(1000);
                            mSwitcher.setText("OFF");
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                       }
                    }
                    ); 

                }catch (Exception e) {
                    //tv1.setText(e.toString());
                }

             } 
                 }
             };

Where it will flip "on" or "off" every 2 seconds. I also need to be able to update the text switcher content from the main UI, but haven't gotten to the point i can try and test that. In addition to the above, I have also tried to use an Async Task:

      new AsyncTask<Void, Double, Void>() {
      @Override
      protected Void doInBackground(Void... params) {
          while (true) {
              mSwitcher.setText("ON");
                SystemClock.sleep(2000);
                mSwitcher.setText("OFF");
                SystemClock.sleep(2000);
          } 
      }     
  }.execute();

but this did not work either.

like image 261
Evan R. Avatar asked Feb 23 '23 13:02

Evan R.


2 Answers

An alternative easier method of achieving this is to use a viewFlipper rather than a viewSwitcher. This allows you to do everything that you want using the XML layout alone:

<ViewFlipper
    android:id="@+id/viewFlipper1"
    android:autoStart="true"
    android:flipInterval="3000">
        <TextView
            android:id="@+id/on_textview"
            android:text="ON" />
        <TextView
            android:id="@+id/off_textview"
            android:text="OFF" />
</ViewFlipper>

To make it look slightly prettier, you can also define the animations to be used in the viewFlipper:

<ViewFlipper
    android:id="@+id/viewFlipper1"
    android:autoStart="true"
    android:flipInterval="3000"
    android:inAnimation="@android:anim/fade_in"
    android:outAnimation="@android:anim/fade_out">    

AdapterViewFlipper Documentation.

like image 129
Rory Avatar answered Feb 25 '23 02:02

Rory


try using timer:

Timer timer = new Timer("desired_name");
timer.scheduleAtFixedRate(
    new TimerTask() {
        public void run() {
            //switch your text using either runOnUiThread() or sending alarm and receiving it in your gui thread
        }
    }, 0, 2000);

Reference for runOnUiThread. You cannot update gui elements from non-ui threads, so trying to update it from doInBackground() method of AsyncTask will lead to error.

like image 21
Vladimir Avatar answered Feb 25 '23 02:02

Vladimir