Good evening. Please tell me how to write such a counter, which increases the day of the month every second, independently of the moment the onCreate method is created.

private TextView timeDate; java.util.Date noteTS = Calendar.getInstance().getTime(); String date = "dd MMMM yyyy"; // 01 January 2013 timeDate.setText(DateFormat.format(date, noteTS)); 

    1 answer 1

    The following code will execute the body of the run() method every second. In it, instead of the current time, as in the example, you can display the date in the format you need. You can also create a variable that you increment every second, in your case, you can change the month value.

     android.os.Handler handler = new android.os.Handler(); int i = 0; TextView textView = new TextView(this); int refreshTime = 1000; // 1 second textView.post(new Runnable() { @Override public void run() { i++; //for example you can increment value there textView.setText((int) System.currentTimeMillis()+""); handler.postDelayed(this,refreshTime); } }); 
    • one
      Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky
    • one
      Thanks for the help - Androtrad