1
The value in Textview is a string that represents minutes and seconds.
To use it, as the starting value of Chonometer, you have to convert it to milliseconds.
Create a method for conversion:
private long minutesAndSecondsToMilliseconds(String minutesAndSeconds){
String[] timeParts = minutesAndSeconds.split(":");
int minutes = Integer.parseInt(timeParts[0]);
int seconds = Integer.parseInt(timeParts[1]);
return (minutes * 60 + seconds) * 1000;
}
Note: The method assumes that the value in the string minutesAndSeconds
is in format mm:ss
. mm
may be greater than 59.
Use later like this:
TextView textView = (TextView)findViewById(R.id.textView);
Chronometer chronometer = (Chronometer)findViewById(R.id.chronometer);
...
...
long milliseconds = minutesAndSecondsToMilliseconds(textView.getText().toString());
chronometer.setBase(SystemClock.elapsedRealtime() - milliseconds);
Works perfectly!! Thank you!!
– Lázaro Rodrigues
Is it possible for Chronometer to exceed 60 minutes? As the textView is a time of a football match, it goes up to 90 minutes, but the Chronometer when it passes 60 he "Zera".
– Lázaro Rodrigues
No, directly. If 60 minutes pass he will show
1:00:00
. Indirectly you can use an Onchronometerticklistener and in the methodonChronometerTick()
take the current value of the text and change it to the format you want.– ramaral
I changed the conversion method to allow values greater than 59 minutes.
– ramaral
@Lázarorodrigues, has the answer solved your question? Do you think you can accept it or do you need something else?
– ramaral
Yes, thank you very much!! There is only one problem, the timer "starts" when the screen item disappears through the recyclerview scroll, there is a way not to do that?
– Lázaro Rodrigues
There is, but for that you need to see the code. Ask another question.
– ramaral
Code of Adapter or recyclerview?
– Lázaro Rodrigues
I can’t be sure but the Adapter code should be sufficient.
– ramaral