0

I am using kitkat to build a stopwatch, and want it to display the millisecond and remove the minute part as well, Here is my code:

public class MainActivity extends AppCompatActivity implements OnClickListener {
private Chronometer chronometer;

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

    chronometer = (Chronometer) findViewById(R.id.chronometer);
    (findViewById(R.id.start_button)).setOnClickListener(this);
    (findViewById(R.id.stop_button)).setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.start_button:
            chronometer.setBase(SystemClock.elapsedRealtime());
            chronometer.start();
            break;
        case R.id.stop_button:
            chronometer.stop();
            break;}
    }
}

and Xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Chronometer Demo"
    android:textSize="20sp"/>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">
<Button
    android:text="START"
    android:id="@+id/start_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<Button
    android:text="STOP"
    android:id="@+id/stop_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

<Chronometer
    android:id="@+id/chronometer"
    android:format=""
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="40sp"/>
</LinearLayout>

I think is the format, but I am not sure how to do the setting, Any help will be appreciated thanks.

Mark L
  • 1
  • 1
  • 3

1 Answers1

0

android's chronograph updates every second, so it's impossible.

There's a lib on github with overrided chronograph that can show millis:

https://github.com/antoniom/Millisecond-Chronometer

I hope it helps you

  • sorry, but I don't know how to use it(the github lib), can you show me a little bit? thanks – Mark L Jul 31 '15 at 10:40
  • To use it just clone the repository. If you use git, type: 'cd ', and then 'git clone https://github.com/antoniom/Millisecond-Chronometer.git' In case you can't use git, download zip from github, unzip it and import as project in your Android Studio. It has an example activity that shows usage of chronometer – Денис Клещев Jul 31 '15 at 11:22