Easy runtime meter
In this tutorial I'll show you a simple time meter example. It can be used for checking the running time of your algorithms. It's useful when you need to decide wich solution is faster/better than an other. It just a simple helper code, but if you made some changes on it, can be a part of any final project, like benchmarking or testing workarounds.
First of all, create a new android project in Eclipse. Then make the basic layout (main.xml) looks like this:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.c
om/apk/res/android" - android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:layout_gravity="center_horizontal"
- android:id="@+id/StartButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/startbutton"
; - android:layout_marginBottom="20dp"
- android:layout_margin="5dp"
- android:textSize="20sp"
- android:padding="20dp" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text=""
- android:textSize="20sp"
- android:gravity="center_horizontal"
- android:id="@+id/TimeTextView" />
- </LinearLayout>
The strings.xml looks like this:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, TimerTestActivity!</string>
- <string name="app_name">TimerTest</string>
- <string name="startbutton">Start</string>
- <string name="TimerText">Time meter</string>
- <string name="TimerPlaseWaitText"
>Please wait…</string> - <string name="TestRunningTime">Test running time: </string>
- </resources>
Now the java coding:
The application using a simple thread and a handler to show the running time.
When pressing the Start button the thread starts and and do the stuff which is in the run block. In the first line of onclick, the current time will be saved. After that the handler get an empty message which means the running is finished.
The handler object calculates the time and show it on the ui.
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mTimeText = (TextView) findViewById(R.id.TimeTextView);
- mStartButton.setOnClickListener(new OnClickListener() {
- @Override
- final ProgressDialog progDialog;
- progDialog = ProgressDialog.show(TimerTestActivity.this,
- getText(R.string.TimerText),
- getText(R.string.TimerPlaseWaitText), true);
- public void run() {
- try {
- // Place your algorithm here
- //
- for (int i = 1; i < 1500; i++) {
- for (int j = 1; j < 1500; j++) {
- }
- }
- //
- //
- mHandler.sendEmptyMessage(0);
- Log.d("TimerTestException:"
;, e.getMessage()); - }
- progDialog.dismiss();
- }
- }.start();
- }
- });
- }
- private Handler mHandler = new Handler() {
- public void handleMessage(Message msg) {
- double formatedTime = ((double) mDifference) / 1000;
- mTimeText.setText(getText(R.string.TestRunningTime) + " "
- + formatedTime + " sec");
- mTimeText.setVisibility(TextView.VISIBLE);
- }
- };



You can download the full project from there: TimerTest.zip
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
7 years 40 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
7 years 40 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
7 years 40 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
7 years 40 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
7 years 40 weeks ago
Poll
Useful resources
Android Development Projects
- i want to create a well-designed live streaming TV app android with cpanel
- I want to build Mobile Application
- Hybrid (Ionic) Mobile App Developer
- activity redemition app&web
- App like DigiLocker
- App chat irc estilo Whatsapp
- Marketing app
- build a mobile application
- Mobile data collection and field representative management software
- bot szybkie odpowiedzi na zlecenia