DatePicker in Android

Many Android applications where we must choose a date use the DatePicker dialogue. The majority of the time, this widget is found in travel booking software that reserve hotels. We can easily select the date from the DatePicker dialogue with this widget.

In this article, you will learn how to create your DatePicker Example of DatePicker​ in Android. So let’s take example of DatePicker  activity codes  in Android of Mobile application.  

Example of DatePicker 

                                   

Step-by-Step Im​plementation

​Step 1: Create a New Project in Android ​studio (File >new  project).  


Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

Implement the same in​voke the following code inside activity_main.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
tools:ignore="HardcodedText">

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnopen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginTop="84dp"
android:visibility="visible"
android:background="@drawable/round_corner_border"
android:text="Click open Calendar"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtdataandtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:textSize="50sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnopen" />

</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Create an MainActivity.kt file in your package folder .

Implement the same invoke the fol​low​ing code inside MainActivity.kt file.

 import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatTextView
import java.util.Calendar

class MainActivity : AppCompatActivity() {
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnopen = findViewById<View>(R.id.btnopen) as AppCompatButton
val txtdataandtime = findViewById<View>(R.id.txtdataandtime) as AppCompatTextView

btnopen.setOnClickListener {
opendatatimepicker(txtdataandtime)
}

}

fun opendatatimepicker(txtcalendar: AppCompatTextView) {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val datePickerDialog = DatePickerDialog(
this,
{ view, year, monthOfYear, dayOfMonth ->

txtcalendar.text =
(dayOfMonth.toString() + "-" + (monthOfYear + 1) + "-" + year)
},

year,
month,
day
)

datePickerDialog.show()
}
}

Step 4 : Output of above example.

                                                                                              

Happy coding! 

365Bloggy July 23, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive