0

I want to make a date of birth date picker using the material date picker component. My main target is when the user presses the edit text view, it will show the date picker dialog. Then the user selects a date and will replace it with the selected date in the edit text view. I'm using the material component for my UI.

here is the visual representation of my working target. my target work

Here is the Input XML code:

<?xml version="1.0" encoding="utf-8"?>
<layout 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"
    tools:context=".entry.StudentEntryFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        android:fillViewport="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
   <!-- Date of birth -->
            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/dateOfBirth"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/date_of_birth"
                android:orientation="vertical"
                android:layout_marginTop="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/department_list_dropdown">


                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/roboto"
                    android:inputType="date"
                    android:onClick="dateOfBirth"
                    android:textAppearance="@style/TextAppearance.MdcTypographyStyles.Subtitle1"
                    android:typeface="normal" />

            </com.google.android.material.textfield.TextInputLayout>
            

            <!--Constraint layout finish-->
        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

</layout>

Here is the InsertActivity.kt code:

package com.example.studentinfodb

import android.media.tv.TvContract
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import androidx.databinding.DataBindingUtil
import com.example.studentinfodb.databinding.ActivityInsertBinding
import com.google.android.material.datepicker.MaterialDatePicker

class InsertActivity : AppCompatActivity() {

    private lateinit var binding: ActivityInsertBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_insert)


        // academic school list items
        val schoolNames = listOf(
            "School of Business & Economics",
            "School of Humanities & Social " +
                    "Sciences",
            "School of Engineering & Physical Sciences",
            "School of Health & Life Sciences"
        )

        val schoolAdapter = ArrayAdapter(this, R.layout.school_list, schoolNames)
        (binding.schoolListDropdown.editText as? AutoCompleteTextView)?.setAdapter(schoolAdapter)

        // department name list
        val departmentNames = listOf(
            "Accounting & Finance",
            "Economics",
            "Management",
            "Marketing & International Business",
            "MBA & EMBA Programs"
        )

        val departmentAdapter = ArrayAdapter(this, R.layout.school_list, departmentNames)
        (binding.departmentListDropdown.editText as? AutoCompleteTextView)?.setAdapter(
            departmentAdapter
        )


    }

// date picker dialog for edit text view
    fun dateOfBirth(view: View) {
        val datePicker = MaterialDatePicker.Builder.datePicker()
            .setSelection(MaterialDatePicker.todayInUtcMilliseconds())
            .setTitleText("Add date of birth").build()

        datePicker.show(supportFragmentManager, datePicker.toString())


    }
}

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
kmhmubin
  • 13
  • 1
  • 5

1 Answers1

0

You can use addOnPositiveButtonClickListener listener called when the user confirms a valid selection:

picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
  @Override public void onPositiveButtonClick(Long selection) {
    // parse the value from millis to formatted date, you can use ZonedDateTime
  }
});