Monday, April 19, 2010

Create a Apps to Show Digital Time in Android

this blog helps to create a simple digital clock in andriod.

Create a Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@+id/countdown" android:layout_width="320dip"
        android:layout_height="120dip" android:text="00:00:00"
        android:textSize="15pt" android:textColor="#00C2FF"
        android:layout_centerInParent="true" />
</RelativeLayout>


Create Activty Class for show the Time

public class MainActivity extends Activity {
    private TextView countdown;
    private Timer timer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView countdown = (TextView) findViewById(R.id.countdown);
    }

    @Override
    protected void onStart() {
        super.onStart();
        timer = new Timer("DigitalClock");
        Calendar calendar = Calendar.getInstance();

        // Get the Current Time
        final Runnable updateTask = new Runnable() {
            public void run() {
                countdown.setText(getCurrentTimeString()); // shows the current time of the day
//                countdown.setText(getReminingTime()); // shows the remaining time of the day
            }
        };

        // update the UI
        int msec = 999 - calendar.get(Calendar.MILLISECOND);
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(updateTask);
            }
        }, msec, 1000);
    }

    @Override
    protected void onStop() {
        super.onStop();
        timer.cancel();
        timer.purge();
        timer = null;
    }
   private String getReminingTime() {
        Calendar calendar = Calendar.getInstance();
        int hour = 23 - calendar.get(Calendar.HOUR_OF_DAY);
        int minute = 59 - calendar.get(Calendar.MINUTE);
        int second = 59 - calendar.get(Calendar.SECOND);
        return String.format("%02d:%02d:%02d", hour, minute, second);
    }



    private String getCurrentTimeString() {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        return String.format("%02d:%02d:%02d %02d", hour, minute, second);
    }
}

8 comments:

  1. Hi Ganesan,
    Thanks for your post. I think you declared TextView object "countdown" twice, which will force application to close.

    ReplyDelete
  2. Nice code but there were 2 errors that i just rectified. There was an extra format specifier:

    private String getCurrentTimeString() {
    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);
    return String.format("%02d:%02d:%02d", hour, minute, second);
    }

    And the TextView has to be public and not private:
    public TextView countdown;

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This code apply my application but arise some error. In this code to set the user permission in the androidManifest.xml give me suggestion for this code.

    ReplyDelete
  5. good code but one exception has occurred during run time...........

    ReplyDelete
  6. It contains 3 errors.
    Make the "countdown" as public.
    Secondly the countdown is declared twice.
    In the String.format("%02d:%02d:%02d %02d") remove one format specifier.
    As it is defined with three variables with 4 format specifier.

    ReplyDelete
  7. @Tapan Rana: Thanks for your advice. I have modified the code and it worked fine. This is entire code for users :)

    import java.util.Calendar;
    import java.util.Timer;
    import java.util.TimerTask;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class MainActivity extends Activity {
    public TextView countdown;
    public Timer timer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo);
    countdown = (TextView) findViewById(R.id.countdown);
    }

    @Override
    protected void onStart() {
    super.onStart();
    timer = new Timer("DigitalClock");
    Calendar calendar = Calendar.getInstance();

    // Get the Current Time
    final Runnable updateTask = new Runnable() {
    public void run() {
    countdown.setText(getCurrentTimeString()); // shows the current
    // time of the day
    // countdown.setText(getReminingTime()); // shows the remaining
    // time of the day
    }
    };

    // update the UI
    int msec = 999 - calendar.get(Calendar.MILLISECOND);
    timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
    runOnUiThread(updateTask);
    }
    }, msec, 1000);
    }

    @Override
    protected void onStop() {
    super.onStop();
    timer.cancel();
    timer.purge();
    timer = null;
    }

    public String getReminingTime() {
    Calendar calendar = Calendar.getInstance();
    int hour = 23 - calendar.get(Calendar.HOUR_OF_DAY);
    int minute = 59 - calendar.get(Calendar.MINUTE);
    int second = 59 - calendar.get(Calendar.SECOND);
    return String.format("%02d:%02d:%02d", hour, minute, second);
    }

    public String getCurrentTimeString() {
    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);
    return String.format("%02d:%02d:%02d", hour, minute, second);
    }
    }

    ReplyDelete