Search code examples
androidandroid-layouttextcountdownandroid-cardview

How to change text based on time and date?


So I need one of two options but don't know where to start with either.

  • I have a list card views in a recyclerview. Each card view needs to have countdown timer implemented and able to run simultaneously. (Seems like the harder option) OR

  • Update the text in a card view based on if a specified time has elapsed or system time reaches a specified minute and hour.


Solution

  • To make the cards with different timers you will need threads. And the easiest way in Android is AsyncTask. So you create a bunch of AsyncTasks (probably you'd want an ArrayList to which you will add the AsyncTasks) and then with a cycle simply .execute() them.

    Making an AsyncTask is very easy. You simply make a new class that is derived from AsyncTask and implement the doInBackground() method.

    P.S. If you're developing for Android 3.0 and higher you will need to launch AsyncTasks this way:

    .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    

    This will make them run simultaneously