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
-
@KellyG41791 (Kelly Greatrex)
I've just received an achievement: Saver http://t.co/zFzNteNg #Android #Androidgames #Gameinsight
12 min 40 sec ago -
@corey_latislaw (Corey Leigh Latislaw)RT @androidphilly: Want to get started with #Android development? Join us this Wednesday for a beginner class: http://t.co/bdUJZiju
12 min 43 sec ago -
@androidphilly (Android Alliance)Want to get started with #Android development? Join us this Wednesday for a beginner class: http://t.co/bdUJZiju
12 min 48 sec ago -
@XxDiatabssxX (mark hughes)Hurray! I've completed a "Holiday decorations" task! 39 http://t.co/JrBGWxX0 #android #gameinsight #androidgames
12 min 49 sec ago -
12 min 49 sec ago
Poll
Useful resources
Android Development Projects
- Private project for Petterpp1 [WRD] by geomaster909
- Modifications on an Android Game by tymex
- app dev for techcity by dany2g
- Android App by dvlinh
- Iphone app by nabzyd
- Betfair Mobile App by Daaniel
- Need App Developer/Designer for iPhone, iPad by lplco
- Android Maps - Saving GeoLocations to MySql database. by adelshehri
- Prototype Android Apps display Information from database by getitonline
- Metal Weigh Calculator by mpmakwana



Comments
Great ArticlE >> ThX AloT
Great ArticlE >> ThX AloT ..
I ReallY EnjoyeD ReadinG it
It is very useful .. and u have great style
I hope u can visit my site below
And .. I,m Waiting for more
شات مصرى ,
شات مصرى ,
شات مصرى ,
شات مصرية ,
شات مصريه ,
دردشة مصرية ,
دردشة بنت مصرية ,
شات بنت مصرية ,
شات بنت مصر ,
دردشة بنت مصر ,
شات ,
دردشه مصريه ,
شات بنات مصر ,
دردشة بنات مصر ,
شات مصرى بنات ,
شات مصر ,
دردشة ,
شات اسكندرية ,
شات القاهرة ,
شات المنصورة ,
شات طنطا ,
شات السويس ,
شات الشرقية ,
شات المنوفية ,
شات الفيوم ,
شات بنى سويف ,
شات سوهاج ,
شات الصعيد ,
شات الزقازيق ,
شات جامعة القاهرة ,
شات جامعة اسكندرية ,
شات بنت السعودية ,
شات بنت البحرين ,
شات بنت الكويت ,
شات بنت فلسطين ,
شات بنت المغرب ,
شات بنت الامارات ,
شات بنت اليمن ,
شات بنت العراق ,
شات مصراوى ,
شات بنات ,
روتانا سينما ,
روتانا سينما مشاهدة مباشرة ,
روتانا سينما بث مباشر ,
روتانا سينما اونلاين ,
I hope u can find something useful About it .. Enjoy
Thanks .. lol i had to read it twice