0% found this document useful (0 votes)
11 views

datepicker

The document contains an XML layout for an Android activity that includes a TextView for displaying a selected date and a Button to trigger a date picker. The MainAct.kt file implements the functionality to show a DatePickerDialog and update the TextView with the selected date. Additionally, there is a note in the Gradle script to change a version number and specify the use of Nougat 7.0 version.

Uploaded by

logeshwari18a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

datepicker

The document contains an XML layout for an Android activity that includes a TextView for displaying a selected date and a Button to trigger a date picker. The MainAct.kt file implements the functionality to show a DatePickerDialog and update the TextView with the selected date. Additionally, there is a note in the Gradle script to change a version number and specify the use of Nougat 7.0 version.

Uploaded by

logeshwari18a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1) Activity.

xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center">

<TextView
android:id="@+id/tvSelectedDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Date: "
android:textSize="18sp"
android:paddingBottom="20dp"/>

<Button
android:id="@+id/btnSelectDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick a Date"/>

</LinearLayout>

2) MainAct.kt

package com.example.datepicker

import android.app.DatePickerDialog
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.*
class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val tvSelectedDate = findViewById<TextView>(R.id.tvSelectedDate)


val btnSelectDate = findViewById<Button>(R.id.btnSelectDate)

btnSelectDate.setOnClickListener {
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val day = calendar.get(Calendar.DAY_OF_MONTH)

val datePickerDialog = DatePickerDialog(


this,
{ _, selectedYear, selectedMonth, selectedDay ->
val selectedDate = "$selectedDay/${selectedMonth + 1}/$selectedYear"
tvSelectedDate.text = "Selected Date: $selectedDate"
},
year, month, day
)
datePickerDialog.show()
}
}
}

3) Gradle script

Change 34 to 35

Use naugat 7.0 version

You might also like