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.