Skip to content

Commit 98c03a8

Browse files
committed
project #7: add a calendar date picker and show the selected date
1 parent 9d8dc01 commit 98c03a8

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

AgeInMinutes/.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
11
package com.example.ageinminutes
22

3-
import androidx.appcompat.app.AppCompatActivity
3+
import android.app.DatePickerDialog
44
import android.os.Bundle
5+
import android.view.View
6+
import android.widget.Button
7+
import android.widget.TextView
8+
import android.widget.Toast
9+
import androidx.appcompat.app.AppCompatActivity
10+
import java.util.*
511

612
class MainActivity : AppCompatActivity() {
713
override fun onCreate(savedInstanceState: Bundle?) {
814
super.onCreate(savedInstanceState)
915
setContentView(R.layout.activity_main)
16+
// get reference to button
17+
val btnDatePicker = findViewById<Button>(R.id.btnDatePicker)
18+
btnDatePicker.setOnClickListener { view ->
19+
clickDatePicker(view)
20+
Toast.makeText(this, "button works", Toast.LENGTH_LONG).show()
21+
}
22+
23+
}
24+
25+
// calender Date picker function
26+
27+
fun clickDatePicker(view: View) {
28+
val myCalendar = Calendar.getInstance()
29+
val year = myCalendar.get(Calendar.YEAR)
30+
val month = myCalendar.get(Calendar.MONTH)
31+
val day = myCalendar.get(Calendar.DAY_OF_MONTH)
32+
33+
//open date picker popup
34+
DatePickerDialog(
35+
this,
36+
DatePickerDialog.OnDateSetListener { view, selectedYear, selectedMonth, selectedDayOfMonth ->
37+
// assign the user input in the variable and format the view
38+
// date will show -> 10/2/2021
39+
val selectedDate = "$selectedDayOfMonth/ ${selectedMonth + 1}/ $selectedYear"
40+
41+
// get the reference of the text view
42+
val showSelectedDate = findViewById<TextView>(R.id.tvSelectedDate)
43+
showSelectedDate.setText(selectedDate).toString()
44+
},
45+
year,
46+
month,
47+
day
48+
).show()
1049
}
1150
}

AgeInMinutes/app/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
43
xmlns:tools="http://schemas.android.com/tools"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent"
@@ -41,7 +40,7 @@
4140
android:textStyle="bold" />
4241

4342
<Button
44-
android:id="@+id/button"
43+
android:id="@+id/btnDatePicker"
4544
android:layout_width="match_parent"
4645
android:layout_height="wrap_content"
4746
android:layout_marginTop="15dp"
@@ -62,7 +61,7 @@
6261
<TextView
6362
android:layout_width="wrap_content"
6463
android:layout_height="wrap_content"
65-
android:text="Selected Date"
64+
android:text="@string/selected_date"
6665
android:textColor="#B7B7B7"
6766
android:textSize="18sp" />
6867

@@ -78,7 +77,7 @@
7877
<TextView
7978
android:layout_width="wrap_content"
8079
android:layout_height="wrap_content"
81-
android:text="Age in minutes"
80+
android:text="@string/age_in_minutes"
8281
android:textColor="#B7B7B7"
8382
android:textSize="18sp" />
8483

AgeInMinutes/app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
<string name="age">AGE</string>
55
<string name="in_minutes">IN MINUTES</string>
66
<string name="select_date">SELECT DATE</string>
7+
<string name="selected_date">Selected Date</string>
8+
<string name="age_in_minutes">Age in minutes</string>
79
</resources>

0 commit comments

Comments
 (0)