Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change format of chronometer?

Tags:

One of my problem is of changing the format of chronometer in android. i have the problem that chronometer shows its time in 00:00 format and i want it to come in 00:00:00 format.does anyone knows the answer?

like image 656
Nikki Avatar asked Nov 11 '10 08:11

Nikki


People also ask

How do I turn off chronometer on Android?

To stop the chronometer, use the stop method. The start and stop methods do not change the base, they simply start and stop the display of the count in the text view. That is, once started, the Chronometer never stops counting. However, you can reset the base by invoking setBase with a new starting point.

How do you find the time from a chronometer?

getText(). toString() gives you the total elapsed time in seconds (my application is some kind of stop watch, so you can pause it and resume it). Hope it helps.

How do you use the chronometer on Kotlin?

Create ChronoMeter in MainActivity. First, we declare a variable meter to create the Chronometer in Kotlin file. then, we access the button from the xml file and set setOnClickListener to start and stop the timer. val btn = findViewById<Button>(R. id.


2 Answers

I was dealing with the same issue. The easiest way to achieve this goal would be overriding updateText method of the Chronometer, but unfortunately it is a private method so I have done this in this way :

    mChronometer.setText("00:00:00");
    mChronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {

        @Override
        public void onChronometerTick(Chronometer chronometer) {
            CharSequence text = chronometer.getText();
            if (text.length()  == 5) {
                chronometer.setText("00:"+text);
            } else if (text.length() == 7) {
                chronometer.setText("0"+text);
            }
        }
    });

I know that it would be better to write custom widget but for small project it can be suitable.

Setting format on chronometer allows formatting text surrounding actual time and time is formatted using DateUtils.formatElapsedTime .

Hope that helps.

like image 45
Marcin Misiewicz Avatar answered Sep 20 '22 18:09

Marcin Misiewicz


Here is an easy and smart solution for time format 00:00:00 in chronometer in android

Chronometer timeElapsed  = (Chronometer) findViewById(R.id.chronomete);
timeElapsed.setOnChronometerTickListener(new OnChronometerTickListener(){
        @Override
            public void onChronometerTick(Chronometer cArg) {
            long time = SystemClock.elapsedRealtime() - cArg.getBase();
            int h   = (int)(time /3600000);
            int m = (int)(time - h*3600000)/60000;
            int s= (int)(time - h*3600000- m*60000)/1000 ;
            String hh = h < 10 ? "0"+h: h+"";
            String mm = m < 10 ? "0"+m: m+"";
            String ss = s < 10 ? "0"+s: s+"";
            cArg.setText(hh+":"+mm+":"+ss);
        }
    });
    timeElapsed.setBase(SystemClock.elapsedRealtime());
    timeElapsed.start();
like image 147
Md. Naushad Alam Avatar answered Sep 20 '22 18:09

Md. Naushad Alam