0

I know, I know, there are tons of post on here about this particular problem, however, most of them are very outdated, and none of them comply with the way I wrote this app... I'm fairly new to coding, and I'm doing this on my own, so i need help to show me how to use what I have and get the chronometer to display milliseconds!

Here's my Java:

package com.jackson.eason.stopwatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;

public class MainActivity extends Activity {

    Chronometer mChronometer;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get references to views
        mChronometer = (Chronometer) findViewById(R.id.chronometer);
        Button button1 = (Button) findViewById(R.id.start);
        Button button2 = (Button) findViewById(R.id.stop);
        Button button3 = (Button) findViewById(R.id.reset);
        Button button4 = (Button) findViewById(R.id.set_format);
        Button button5 = (Button) findViewById(R.id.clear_format);



        // Declare the listeners
        View.OnClickListener mStartListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.start();
            }
        };

        View.OnClickListener mStopListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.stop();
            }
        };

        View.OnClickListener mResetListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.setBase
                        (SystemClock.elapsedRealtime());
            }
        };

        // You forgot to declare a listener for set format in your updated code
        View.OnClickListener mSetFormatListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.setFormat("Formatted time (%s)");
            }
        };

        View.OnClickListener mClearFormatListener = new OnClickListener() {
            public void onClick(View v) {
                mChronometer.setFormat(null);
            }
        };
        //Assign listeners to your buttons
        button1.setOnClickListener(mStartListener);
        button2.setOnClickListener(mStopListener);
        button3.setOnClickListener(mResetListener);
        button4.setOnClickListener(mSetFormatListener);
        button5.setOnClickListener(mClearFormatListener);
    }




}

Also, if needed, Here's my Main XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:clickable="true"
    android:background="#ff4f4a53">

    <Chronometer android:id="@+id/chronometer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="30dip"
        android:paddingTop="30dip"
        android:focusableInTouchMode="true"
        android:textSize="55sp"
        android:textIsSelectable="false"
        android:typeface="serif"
        android:textColor="#fffcfffa" />

    <Button android:id="@+id/start"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="start"
        android:background="#ff5effab"
        android:elegantTextHeight="false"
        android:layout_gravity="center_horizontal">
        <requestFocus />
    </Button>

    <Button android:id="@+id/stop"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="stop"
        android:background="#fffe7c5b">
    </Button>

    <Button android:id="@+id/reset"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="reset"
        android:layout_gravity="center_horizontal"
        android:textColor="#fffffcfe">
        </Button>

    <Button android:id="@+id/clear_format"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="clear"
        android:layout_gravity="center_horizontal"
        android:textColor="#fffefdff">
    </Button>

    <Button android:id="@+id/set_format"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="format"
        android:layout_gravity="center_horizontal"
        android:textColor="#ffffffff">
    </Button>


</LinearLayout>

Any help would be greatly appreciated!

  • I dont know how to use this chronometer properly, but I do know you are not setting the format properly. That is done through a %s according to the documentation: http://developer.android.com/reference/android/widget/Chronometer.html#setFormat(java.lang.String) – engineercoding Feb 12 '15 at 15:48
  • No problem. Always look for the documentation of objects which you havent seen before. It really helped me at android programming (Im still a noob at it though :p) – engineercoding Feb 12 '15 at 15:57

0 Answers0