{"id":36987,"date":"2024-11-01T09:53:52","date_gmt":"2024-11-01T09:53:52","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36987"},"modified":"2024-11-01T11:42:42","modified_gmt":"2024-11-01T11:42:42","slug":"kotlin-android-app-development-course-creating-the-stopwatch-feature-of-a-clock-app","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36987\/","title":{"rendered":"kotlin android app development course, creating the stopwatch feature of a clock app"},"content":{"rendered":"<p><body><\/p>\n<p>Implementing a stopwatch feature while creating a clock app in Android development is a very useful exercise. In this article, we will detail how to create a stopwatch feature using Kotlin. We will set up the overall app structure, design the UI of the stopwatch, and implement its basic functionality.<\/p>\n<h2>1. Project Setup<\/h2>\n<p>Open Android Studio and create a new project. Set the project name to &#8220;StopwatchApp&#8221; and choose Kotlin as the language. Set the Minimum SDK to API 21 (Lollipop) or higher.<\/p>\n<h3>1.1. Adding Gradle Dependencies<\/h3>\n<p>No special libraries are needed to implement the UI elements of the stopwatch, but we will use <code>ViewModel<\/code> and <code>LiveData<\/code> to manage the UI state. Therefore, add the following dependencies to the <code>build.gradle<\/code> file.<\/p>\n<pre><code>implementation \"androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0\"\nimplementation \"androidx.lifecycle:lifecycle-livedata-ktx:2.4.0\"<\/code><\/pre>\n<h2>2. UI Design<\/h2>\n<p>The UI of the stopwatch consists mainly of <code>TextView<\/code> and <code>Button<\/code>. Open the <code>activity_main.xml<\/code> file and design the UI as follows.<\/p>\n<pre><code>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\"\n    android:gravity=\"center\"&gt;\n\n    &lt;TextView\n        android:id=\"@+id\/tvStopwatch\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:textSize=\"48sp\"\n        android:text=\"00:00:00\"\n        android:layout_marginBottom=\"20dp\"\/&gt;\n\n    &lt;Button\n        android:id=\"@+id\/btnStart\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Start\"\/&gt;\n\n    &lt;Button\n        android:id=\"@+id\/btnStop\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Stop\"\/&gt;\n\n    &lt;Button\n        android:id=\"@+id\/btnReset\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Reset\"\/&gt;\n\n&lt;\/LinearLayout&gt;<\/code><\/pre>\n<h2>3. Implementing Logic<\/h2>\n<p>Open the MainActivity.kt file and implement the logic of the stopwatch. We will utilize <code>CountDownTimer<\/code> to count the time of the stopwatch.<\/p>\n<h3>3.1. Creating ViewModel<\/h3>\n<p>Create a ViewModel class to manage the state of the stopwatch. This will allow us to store UI-related data.<\/p>\n<pre><code>class StopwatchViewModel : ViewModel() {\n    private var timer: CountDownTimer? = null\n    private var time = 0L\n    private val _formattedTime = MutableLiveData<String>()\n\n    val formattedTime: LiveData<String> get() = _formattedTime\n\n    private fun updateTime() {\n        val hours = (time \/ 3600000) % 24\n        val minutes = (time \/ 60000) % 60\n        val seconds = (time \/ 1000) % 60\n        _formattedTime.value = String.format(\"%02d:%02d:%02d\", hours, minutes, seconds)\n    }\n\n    fun start() {\n        timer = object : CountDownTimer(Long.MAX_VALUE, 1000) {\n            override fun onTick(millisUntilFinished: Long) {\n                time += 1000\n                updateTime()\n            }\n\n            override fun onFinish() {}\n        }.start()\n    }\n\n    fun stop() {\n        timer?.cancel()\n    }\n\n    fun reset() {\n        stop()\n        time = 0\n        updateTime()\n    }\n}<\/code><\/pre>\n<h3>3.2. Implementing MainActivity<\/h3>\n<p>Now, in the MainActivity.kt file, we will handle button click events using the ViewModel.<\/p>\n<pre><code>class MainActivity : AppCompatActivity() {\n    private lateinit var viewModel: StopwatchViewModel\n    private lateinit var tvStopwatch: TextView\n    private lateinit var btnStart: Button\n    private lateinit var btnStop: Button\n    private lateinit var btnReset: Button\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n\n        viewModel = ViewModelProvider(this).get(StopwatchViewModel::class.java)\n        tvStopwatch = findViewById(R.id.tvStopwatch)\n        btnStart = findViewById(R.id.btnStart)\n        btnStop = findViewById(R.id.btnStop)\n        btnReset = findViewById(R.id.btnReset)\n\n        viewModel.formattedTime.observe(this, { time ->\n            tvStopwatch.text = time\n        })\n\n        btnStart.setOnClickListener {\n            viewModel.start()\n        }\n\n        btnStop.setOnClickListener {\n            viewModel.stop()\n        }\n\n        btnReset.setOnClickListener {\n            viewModel.reset()\n        }\n    }\n}<\/code><\/pre>\n<h2>4. Running and Testing the App<\/h2>\n<p>Now let&#8217;s run the app and test the stopwatch feature. Pressing the start button will start the stopwatch, the stop button will halt the time, and the reset button will reset the time.<\/p>\n<h2>5. Improvements<\/h2>\n<p>While the basic stopwatch functionality is implemented, the following additional features can be improved and implemented:<\/p>\n<ul>\n<li>Add a Pause feature<\/li>\n<li>Add a Lap feature<\/li>\n<li>Improve the UI and apply animations<\/li>\n<\/ul>\n<h2>6. Conclusion<\/h2>\n<p>In this article, we explained how to create a simple stopwatch feature in an Android app using Kotlin. Through this basic app, you will familiarize yourself with fundamental concepts of Kotlin and Android. It will serve as a strong foundation for building more complex apps in the future.<\/p>\n<p>Now, as you develop each feature further, enhance your Android app development skills!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Implementing a stopwatch feature while creating a clock app in Android development is a very useful exercise. In this article, we will detail how to create a stopwatch feature using Kotlin. We will set up the overall app structure, design the UI of the stopwatch, and implement its basic functionality. 1. Project Setup Open Android &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36987\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;kotlin android app development course, creating the stopwatch feature of a clock app&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[143],"tags":[],"class_list":["post-36987","post","type-post","status-publish","format-standard","hentry","category-kotlin-android-app-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>kotlin android app development course, creating the stopwatch feature of a clock app - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/36987\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"kotlin android app development course, creating the stopwatch feature of a clock app - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Implementing a stopwatch feature while creating a clock app in Android development is a very useful exercise. In this article, we will detail how to create a stopwatch feature using Kotlin. We will set up the overall app structure, design the UI of the stopwatch, and implement its basic functionality. 1. Project Setup Open Android &hellip; \ub354 \ubcf4\uae30 &quot;kotlin android app development course, creating the stopwatch feature of a clock app&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36987\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:53:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:42:42+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/36987\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36987\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"kotlin android app development course, creating the stopwatch feature of a clock app\",\"datePublished\":\"2024-11-01T09:53:52+00:00\",\"dateModified\":\"2024-11-01T11:42:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36987\/\"},\"wordCount\":342,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin Android app development\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36987\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36987\/\",\"name\":\"kotlin android app development course, creating the stopwatch feature of a clock app - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:53:52+00:00\",\"dateModified\":\"2024-11-01T11:42:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36987\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36987\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36987\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"kotlin android app development course, creating the stopwatch feature of a clock app\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"kotlin android app development course, creating the stopwatch feature of a clock app - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/36987\/","og_locale":"ko_KR","og_type":"article","og_title":"kotlin android app development course, creating the stopwatch feature of a clock app - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Implementing a stopwatch feature while creating a clock app in Android development is a very useful exercise. In this article, we will detail how to create a stopwatch feature using Kotlin. We will set up the overall app structure, design the UI of the stopwatch, and implement its basic functionality. 1. Project Setup Open Android &hellip; \ub354 \ubcf4\uae30 \"kotlin android app development course, creating the stopwatch feature of a clock app\"","og_url":"https:\/\/atmokpo.com\/w\/36987\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:53:52+00:00","article_modified_time":"2024-11-01T11:42:42+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/36987\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36987\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"kotlin android app development course, creating the stopwatch feature of a clock app","datePublished":"2024-11-01T09:53:52+00:00","dateModified":"2024-11-01T11:42:42+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36987\/"},"wordCount":342,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin Android app development"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36987\/","url":"https:\/\/atmokpo.com\/w\/36987\/","name":"kotlin android app development course, creating the stopwatch feature of a clock app - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:53:52+00:00","dateModified":"2024-11-01T11:42:42+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36987\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36987\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36987\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"kotlin android app development course, creating the stopwatch feature of a clock app"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=36987"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36987\/revisions"}],"predecessor-version":[{"id":36988,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36987\/revisions\/36988"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}