Kotlin Date time picker using JRDateTimePicker

Kotlin Date time picker using JRDateTimePicker

Welcome folks I am back with another blog post. In this blog post I will be implementing a Date Time Picker Dialog in Kotlin using Kotlin using  JRDateTimePicker library.

 

Installation

 

implementation 'com.github.jrizani:JRDateTimePicker:$version'

 

How To

Kotlin

 

val builder = DateTimePicker.Builder(context, themeResId)
        
builder.setInitialDateTime( //you must call this method
    year,
    month_of_year,
    day_of_month,
    hour_of_day,
    minute,
    is24HourView
    )
    
    
builder.setOnDateTimeChangeListener(object : OnDateTimeSelectListener {
    override fun onDateTimeSelected(year: Int, monthOfYear: Int, dayOfMonth: Int, hourOfDay: Int, minute: Int) {
        //do whatever you want with selected date time when it selected
        }
    })

val dialog = builder.create() //method to create DateTimePicker instance

et_date.setOnClickListener {
    //method to show dialog, you can directly call show on builder instance like 'builder.show' that also return DateTimePicker instance
    dialog.show()
    }

Java

 

final DateTimePicker.Builder builder = new DateTimePicker.Builder(context, themeResId);

builder.setInitialDateTime(//you must call this method
        year,
        month_of_year,
        day_of_month,
        hour_of_day,
        minute,
        is24HourView                                                                    
);
builder.setOnDateTimeChangeListener(new DateTimePicker.OnDateTimeSelectListener() {
    @Override
    public void onDateTimeSelected(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minute) {
        //do whatever you want with selected date time when it selected
    }
});

final DateTimePicker dialog = builder.create(); //method to create DateTimePicker instance
                                                                                                              
etDate.setOnClickListener(new View.OnClickListener() {
    @Overrid
    public void onClick(View v) {
       //method to show dialog, you can directly call show on builder instance like 'builder.show' that also return DateTimePicker instance
        dialog.show();
    }
});

 

READ  Kotlin Google Maps AutocompleteTextView Location Example - Coding Shiksha

Simple Example

 

<?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">

    <EditText
        android:id="@+id/et_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none|datetime"
        android:focusable="false"
        android:longClickable="false" />

</LinearLayout>

 

Java

 

package io.github.jrizani.jrdatetimepicker.example;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import io.github.jrizani.jrdatetimepicker.DateTimePicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;


/*=============================*/
/*            AUTHOR           */
/*          JULIAN NR          */
/* juliannoorrizani@gmail.com  */
/*         01 Sep 2019         */
/*=============================*/

public class JavaActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText etDate = findViewById(R.id.et_date);

        final DateTimePicker.Builder builder = new DateTimePicker.Builder(this, R.style.AppTheme_Dialog);
        Calendar date = Calendar.getInstance();
        builder.setInitialDateTime(
                date.get(Calendar.YEAR),
                date.get(Calendar.MONTH),
                date.get(Calendar.DAY_OF_MONTH),
                date.get(Calendar.HOUR_OF_DAY),
                date.get(Calendar.MINUTE),
                true
        );
        builder.setOnDateTimeChangeListener(new DateTimePicker.OnDateTimeSelectListener() {
            @Override
            public void onDateTimeSelected(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minute) {
                Calendar selectedDate = Calendar.getInstance();
                selectedDate.set(year, monthOfYear, dayOfMonth, hourOfDay, minute, 0);
                SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy HH:mm", Locale.getDefault());
                etDate.setText(formatter.format(selectedDate.getTime()));
            }
        });

        final DateTimePicker dialog = builder.create();

        etDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show();
            }
        });
    }
}

 

READ  Kotlin PDF Viewer Library Example - Coding Shiksha

Kotlin

 

package io.github.jrizani.jrdatetimepicker.example

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import io.github.jrizani.jrdatetimepicker.DateTimePicker
import io.github.jrizani.jrdatetimepicker.DateTimePicker.OnDateTimeSelectListener
import kotlinx.android.synthetic.main.activity_main.*
import java.text.SimpleDateFormat
import java.util.*

/*=============================*/
/*            AUTHOR           */
/*          JULIAN NR          */
/* juliannoorrizani@gmail.com  */
/*         01 Sep 2019         */
/*=============================*/

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val builder = DateTimePicker.Builder(
            this,
            R.style.AppTheme_Dialog
        )
        val date = Calendar.getInstance()
        builder.setInitialDateTime(
            date.get(Calendar.YEAR),
            date.get(Calendar.MONTH),
            date.get(Calendar.DAY_OF_MONTH),
            date.get(Calendar.HOUR_OF_DAY),
            date.get(Calendar.MINUTE),
            true
        )
        builder.setOnDateTimeChangeListener(object : OnDateTimeSelectListener {
            override fun onDateTimeSelected(year: Int, monthOfYear: Int, dayOfMonth: Int, hourOfDay: Int, minute: Int) {
                val selectedDate = Calendar.getInstance()
                selectedDate.set(year, monthOfYear, dayOfMonth, hourOfDay, minute, 0)
                val formatter = SimpleDateFormat("dd MMMM yyyy HH:mm", Locale.getDefault())
                et_date.setText(formatter.format(selectedDate.time))
            }
        })

        val dialog = builder.create()

        et_date.setOnClickListener {
            dialog.show()
        }
    }
}

 

Leave a Reply

Close Menu