Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use Chronometer in Android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 524
    Comment on it

    Below example code will helps you to create Chronometer. For this firstly, In activity_main.xml layout I have added buttons and Chronometer layout. Now See MainActivity, here I have used start(), stop(), SystemClock.elapsedRealtime()), setFormat() methods when you clicks SetFormat button, Time View format will changed dynamically. You can see below example code it clearly describes How to use Chronometer in android

     

    Step(1)activity_main.xml layout-

    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/btn_start"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Start"
            android:layout_toStartOf="@+id/chronometer" />
    
        <Button
            android:id="@+id/btn_clear"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn_setFormat"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:text="clearFormat" />
        <!-- chronometer with black background and red text color -->
        <Chronometer
            android:id="@+id/chronometer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn_clear"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:background="#000"
            android:gravity="center"
            android:padding="10dp"
            android:textColor="#f00"
            android:textStyle="bold" />
    
        <Button
            android:id="@+id/btn_stop"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Stop"
            android:layout_alignBaseline="@+id/btn_start"
            android:layout_alignBottom="@+id/btn_start"
            android:layout_toEndOf="@+id/btn_start"
            android:layout_marginStart="21dp" />
    
        <Button
            android:id="@+id/btn_restart"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Restart"
            android:layout_below="@+id/btn_start"
            android:layout_alignParentStart="true"
            android:layout_alignEnd="@+id/btn_start" />
    
        <Button
            android:id="@+id/btn_setFormat"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="setFormat"
            android:layout_below="@+id/btn_stop"
            android:layout_toEndOf="@+id/btn_restart"
            android:layout_alignStart="@+id/btn_stop" />
    
    </RelativeLayout>


     

    Step(2)-MainActivity-
     

     protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                // initiate views
                simpleChronometer = (Chronometer) findViewById(R.id.chronometer);
                start = (Button) findViewById(R.id.btn_start);
                stop = (Button) findViewById(R.id.btn_stop);
                restart = (Button) findViewById(R.id.btn_restart);
                setFormat = (Button) findViewById(R.id.btn_setFormat);
                clearFormat = (Button) findViewById(R.id.btn_clear);
                // perform click  event on start button to start a chronometer
                start.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        simpleChronometer.start();
                    }
                });
    
                // Click on stop button to stop the chronometer
                stop.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        simpleChronometer.stop();
                    }
                });
    
                //Click on on restart button to set the base time on chronometer
                restart.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        simpleChronometer.setBase(SystemClock.elapsedRealtime());
                    }
                });
    
                // Click on on set Format button to set the format of chronometer
                setFormat.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        simpleChronometer.setFormat("Time running (%s)");
                    }
                });
    
                //Click on clear format to rest the view format of chronometer
                clearFormat.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        simpleChronometer.setFormat(null);
                    }
                });
            }
    
    
        }
    
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: