Night Clock In Android Using Android Studio

Introduction

 
In this article, we are going to learn about Night Clock in Android using Android Studio. It is a type of library that performs all clock operations. In this, we have used Analog Clock in night mode.
 
Step 1
 
Create a new project in Android Studio.
 
Android
 
Give a name to the project and click "Next".
 
Android
 
Select the "Phone and Tablet" option and click "Next".
 
Android
 
Select an empty activity and click "Next".
 
Android
 
At last, give the activity name and click on "Finish".
 
Android
 
Step 2
 
Locate the Gradle Scripts>>Build. Gradle.
 
Android
 
And, type the following dependency in your app's build.gradle.
 
Android
 
Code copy is here:
  1. compile 'com.tomerrosenfeld.customanalogclockview:custom-analog-clock-view:1.1'  
Step 3
 
Next, go to app >> res >> layout >> activity_main.xml. Select activity page.
 
Android
 
Just type the code as follows.
 
Android
 
The code copy is here.
  1. <com.tomerrosenfeld.customanalogclockview.CustomAnalogClock  
  2.         android:id="@+id/analog_clock"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="match_parent"  
  5.         app:default_watchface="true"  
  6.         android:layout_centerVertical="true"  
  7.         android:layout_centerHorizontal="true"  
  8.         />  
Step 4
 
Next, go to app >> res >>Styles and,
 
Android
 
Just type the code as follows.
 
Android
 
Code copy is here.
  1. <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">  
  2.         <!-- Customize your theme here. -->  
  3.         <item name="colorPrimary">@color/colorPrimary</item>  
  4.         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>  
  5.         <item name="colorAccent">@color/colorAccent</item>  
  6.     </style>  
Step 5
 
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page.
 
Android
 
Just type the code as follows.
 
Android
 
The code copy is here.
  1. @Override  
  2.    protected void onCreate(Bundle savedInstanceState) {  
  3.        super.onCreate(savedInstanceState);  
  4.        setContentView(R.layout.activity_main);  
  5.   
  6.        CustomAnalogClock customAnalogClock = (CustomAnalogClock) findViewById(R.id.analog_clock);  
  7.        customAnalogClock.setAutoUpdate(true);  
Step 6
 
Verify the preview.
After the code is applied, the preview will appear like this.
 
Android
 
Step 7
 
Next, go to Android Studio and deploy the application. Select an Emulator or your mobile with USB debugging enabled. Give it a few seconds to make installations and set permissions.
 
Android
 
Run the application in your desired emulator (Shift + F10).
 
Android
 
Explanation of source code
 
The source code used in the activitymain.xml file will implement an Analog Clock in the app and Mainactivity.java will render the current time to the clock.
 

Summary

 
We have just created the app to see the Analog Clock in the night mode and learned how to use it.
 
*Support and Share, Thank you*


Similar Articles