Last active
May 20, 2019 06:52
-
-
Save rahulpandey/f19eaccffc4b4934e40e64a735eff29f to your computer and use it in GitHub Desktop.
Date time picker android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<DatePicker | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:calendarViewShown="false" | |
android:clipToPadding="true" | |
android:spinnersShown="true" | |
android:datePickerMode="spinner" | |
/> | |
<TimePicker | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:clipToPadding="true" | |
android:timePickerMode="spinner"/> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.myapplication | |
import android.app.AlertDialog | |
import android.app.Dialog | |
import android.graphics.drawable.ColorDrawable | |
import android.os.Bundle | |
import android.view.LayoutInflater | |
import androidx.fragment.app.DialogFragment | |
class MyDateTimePickerDialogFragment : DialogFragment() { | |
companion object { | |
fun newInstance(): MyDateTimePickerDialogFragment = MyDateTimePickerDialogFragment() | |
} | |
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | |
if (context == null) return super.onCreateDialog(savedInstanceState) | |
val view = LayoutInflater.from(context).inflate(R.layout.fragment_date_time_dialog, null) | |
val timePicker = view.findViewById<TimePicker>(R.id.time_pick) | |
timePicker.setIs24HourView(true) | |
val datePicker = view.findViewById<DatePicker>(R.id.date_pick) | |
datePicker.maxDate = Calendar.getInstance().time.time | |
val dialog = AlertDialog.Builder(context!!, android.R.style.Theme_Holo_Dialog) | |
.setTitle("Select Date Time") | |
.setView(view) | |
.setCancelable(true) | |
.setPositiveButton(android.R.string.ok) { dialog, which -> | |
} | |
.setNegativeButton(android.R.string.cancel) { | |
_, _ -> [email protected]() | |
}.create() | |
dialog.window?.setBackgroundDrawable(ColorDrawable(android.graphics.Color.TRANSPARENT)) | |
return dialog | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment