How to create digital clock with textview in android?

This example demonstrates How to create digital clock with textview in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = ""
   xmlns:app = ""
   xmlns:tools = ""
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:orientation = "vertical">
      android:id = "@+id/textview"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent"
      android:gravity = "center"
      android:textStyle = "bold"
      android:textSize = "50sp"
      app:fontFamily = "@font/orbitron" />

In the above code, we have taken textview. It will show clock with blink animation as we see in normal digital clock.

Step 3 − Add the following code to src/

package com.example.andy.myapplication;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
   TextView textview;
   protected void onCreate(Bundle readdInstanceState) {
      textview = findViewById(;
      Date today = new Date();
      SimpleDateFormat formatter = new SimpleDateFormat("HH:MM");
   private void blink() {
      final Handler hander = new Handler();
      new Thread(new Runnable() {
         public void run() {
            try {
            } catch (InterruptedException e) {
   Runnable() {
               public void run() {
                  if(textview.getVisibility() = = View.VISIBLE) {
                  } else {

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –

In the above time textview will blink for forever

Click here to download the project code

Updated on: 30-Jul-2019


Kickstart Your Career

Get certified by completing the course

Get Started