Display Current Day, Date and Time In CardView - Kotlin Version
In this tutorial, you will learn how to develop an Android application using Kotlin that displays the current day, date, and time within a 'CardView'. You'll implement this by writing Kotlin code in the MainActivity to fetch the current system time, format it according to your requirements, and display it in a CardView UI component. Additionally, you'll configure necessary dependencies in the 'build.gradle' file to ensure smooth integration of required libraries or components for the project. This tutorial will focus on handling date and time formatting, UI layout design with CardView, and integrating dependencies for a Kotlin-based Android application. Submitted on July 07, 2024
To create an Android app in Kotlin that displays the current day, date, and time in a ‘CardView’ with specified formatting, you'll need to follow these steps. Here the necessary Kotlin code for MainActivity and also mention the dependencies you might need in your ‘build.gradle’ file.
Step-by-Step Implementation
1. Update Dependencies
Ensure you have the necessary dependencies in your ‘build.gradle’ (Module: app) file:
Copy Code
implementation 'androidx.cardview:cardview:1.0.0' // for CardViewimplementation 'androidx.core:core-ktx:1.7.0' // for Kotlin extensions
Make sure your ‘build.gradle’ is synced after adding these dependencies.
2. Create Layout (activity_main.xml)
Here's how your activity_main.xml layout file should look:
Now, in your ‘MainActivity.kt’ file, update it as follows:
Copy Code
importandroid.os.Bundleimportandroid.widget.TextViewimportandroidx.appcompat.app.AppCompatActivityimportjava.text.SimpleDateFormatimportjava.util.*classMainActivity : AppCompatActivity() {
private lateinit var textViewDay: TextViewprivate lateinit var textViewDate: TextViewprivate lateinit var textViewTime: TextViewoverride funonCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textViewDay = findViewById(R.id.textViewDay)
textViewDate = findViewById(R.id.textViewDate)
textViewTime = findViewById(R.id.textViewTime)
// Get current date and timeval calendar = Calendar.getInstance()
val dateFormat = SimpleDateFormat("EEEE", Locale.getDefault())
val date = SimpleDateFormat("MMMM dd, yyyy", Locale.getDefault())
val time = SimpleDateFormat("hh:mm a", Locale.getDefault())
val currentDate = date.format(calendar.time)
val currentTime = time.format(calendar.time)
val currentDay = dateFormat.format(calendar.time)
// Set text to TextViews
textViewDay.text = currentDay
textViewDate.text = currentDate
textViewTime.text = currentTime
}
}
Explanation of Android CardView and TextViews - Kotlin
Explanation:
Dependencies: We added androidx.cardview:cardview for the CardView and androidx.core:core-ktx for Kotlin extensions.
Layout (activity_main.xml):
Uses a CardView with nested LinearLayout containing three TextViews for day, date, and time.
Retrieves current date and time using Calendar and formats it using SimpleDateFormat.
Sets formatted date, day, and time to respective TextViews.
Additional Notes:
Customization: You can further customize the CardView and TextViews with different attributes like text size, font, margins, etc., to fit your design requirements.
Time Formatting: Adjust SimpleDateFormat patterns ("EEEE", "MMMM dd, yyyy", "hh:mm a") as per your desired format for day, date, and time.
By following these steps, you should have an Android app in Kotlin that displays the current day, date, and time within a CardView, meeting your requirements. Adjustments can be made based on additional features or styling preferences for your application.