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.
Give a name to the project and click "Next".
Select the "Phone and Tablet" option and click "Next".
Select an empty activity and click "Next".
At last, give the activity name and click on "Finish".
Step 2
Locate the Gradle Scripts>>Build. Gradle.
And, type the following dependency in your app's build.gradle.
Code copy is here:
- compile 'com.tomerrosenfeld.customanalogclockview:custom-analog-clock-view:1.1'
Step 3
Next, go to app >> res >> layout >> activity_main.xml. Select activity page.
Just type the code as follows.
The code copy is here.
- <com.tomerrosenfeld.customanalogclockview.CustomAnalogClock
- android:id="@+id/analog_clock"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:default_watchface="true"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"
- />
Step 4
Next, go to app >> res >>Styles and,
Just type the code as follows.
Code copy is here.
- <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
- <!-- Customize your theme here. -->
- <item name="colorPrimary">@color/colorPrimary</item>
- <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
- <item name="colorAccent">@color/colorAccent</item>
- </style>
Step 5
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page.
Just type the code as follows.
The code copy is here.
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- CustomAnalogClock customAnalogClock = (CustomAnalogClock) findViewById(R.id.analog_clock);
- customAnalogClock.setAutoUpdate(true);
Step 6
Verify the preview.
After the code is applied, the preview will appear like this.
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.
Run the application in your desired emulator (Shift + F10).
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*