Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 893719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:15:56+00:00 2026-05-15T14:15:56+00:00

So I have a TextSwitcher that I want to update every second with the

  • 0

So I have a TextSwitcher that I want to update every second with the number of seconds it has been since the activity opened. Here is my code

public class SecondActivity extends Activity implements ViewFactory
{   
    private TextSwitcher counter;
    private Timer secondCounter;
    int elapsedTime = 0;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // Create the layout
        super.onCreate(savedInstanceState);

        setContentView(R.layout.event);

        // Timer that keeps track of elapsed time
        counter = (TextSwitcher) findViewById(R.id.timeswitcher);
        Animation in = AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out);
        counter.setFactory(this);
        counter.setInAnimation(in);
        counter.setOutAnimation(out);

        secondCounter = new Timer();
        secondCounter.schedule(new TimerUpdate(), 0, 1000);
    }

    /**
     * Updates the clock timer every second
     */
    public void updateClock()
    {        
        //Update time
        elapsedTime++;
        int hours = elapsedTime/360;
        int minutes = elapsedTime/60;
        int seconds = elapsedTime%60;

        // Format the string based on the number of hours, minutes and seconds
        String time = "";

        if (!hours >= 10)
        {
            time += "0";
        }
        time += hours + ":";

        if (!minutes >= 10)
        {
            time += "0";
        }
        time += minutes + ":";

        if (!seconds >= 10)
        {
            time += "0";
        }
        time += seconds;

        // Set the text to the textview
        counter.setText(time);
    }

    private class TimerUpdate extends TimerTask
    {
        @Override
        public void run()
        {
            updateClock();  
        }
    }

    @Override
    public View makeView() 
    {
        Log.d("MakeView");
        TextView t = new TextView(this);
        t.setTextSize(40);
        return t;
    }
}

So basically, I have a Timer that every second adds another second and them formats the way I want to be displayed and set the text of the TextSwitcher, which I thought called makeView, but makeView only gets called once and the time stays as 00:00:01. Did I miss a step, I dont think this UI object is very well documented.

Thanks, Jake

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:15:56+00:00Added an answer on May 15, 2026 at 2:15 pm

    You can only update the UI in the UI thread. So in your example you could do something like this.

    private Handler mHandler = new Handler() {
         void handleMessage(Message msg) {
              switch(msg.what) {
                   CASE UPDATE_TIME:
                        // set text to whatever, value can be put in the Message
              }
         }
    }
    

    And call

    mHandler.sendMessage(msg);
    

    in the run() method of the TimerTask.

    This is a solution to your current issue but there is probably a better way to do it without using TimerTasks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following app that has a textview and an edittext. When the
I have an editText that brings up the number keyboard. When you enter a
I have a class that extends Activity and implements ViewFactory. I have found some
I have an android application with a gallery that switches between textswitcher views. It
I have an edittext, and a textwatcher that watches if SPACE arrived or not.
Have you determined a maximum number of characters allowed in FCKEditor ? I seem
I am having a problem in searching a ListView. In my activity I have
I have been slowing learning and building my first android app. I'm VERY new
I have an Activity with an AutoComplete box on it. Whenever the text changes,
I have a ListView that I filter with and EditText that I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.