Wednesday 17 August 2016

Chronometer

Chronometer  -code done, tutorial need to format
It implement a timer to application. You can Start, Stop,  reset timer and format timer as well as.
Code -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
tools:context=".MainActivity">
 <
Chronometer
    
android:id="@+id/chronometer1"
    
android:layout_width="wrap_content"
    
android:layout_height="wrap_content" />
</
LinearLayout>

Start - Use to start the chronometer.
Chronometer chronometer=(Chronometer)findViewById(R.id.chronometer1);

chronometer.start();

2. stop(): stop function of chronometer is used to stop the counting up.
chronometer.stop();

3. setFormat(String format): set format function of chronometer is used to set the format string used to display. In other words we can say it is used to display text, numbers etc along-with chronometer.
chronometer.setFormat("Formated Time - %s");
 
4. ClearFormat
 
chronometer.setFormat(null);
 
4. getformat(): This function of chronometer is used for getting the current format string. This methods returns a string type value.
String getformat= chronometer.getFormat().toString();

 textView.setText(getformat);
 
5. Reset -
chronometer.setBase(SystemClock.elapsedRealtime());
 
6. 6. setBase(long base): set base function of chronometer is used to set the time that count up time is in reference to. You can give it a start time in the elapsedRealtime() timebase, and it counts up from that, or if you don’t give it a base time, it will use the time at which you call start().
SystemClock.elapsedRealtime() is the number of milliseconds since the device was turned on.
Below we set the base time for a chronometer.
Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); // initiate a chronometer
 
simpleChronometer.setBase(SystemClock.elapsedRealtime()); // set base time for a chronometer
7. getBase(): get base function is used to get the base time from a chronometer. This method returns a base time as set through the setBase() function. This method return long value.
Below we get the base time from a chronometer.
Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); // initiate a chronometer
 
long base=simpleChronometer.getBase(); // get base time from a chronometer
 
 
5. setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener): This is a listener event which is automatically called when the chronometer changes.
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {

    @Override

    public void onChronometerTick(Chronometer chronometer) {

        Toast.makeText(getApplicationContext(),"Chronometer is ticked",Toast.LENGTH_LONG).show();

    }

});
 

































22

No comments:

Post a Comment