Output :
Create DateTime.Class File
package com.datetimedialog.datetimedialog;
/**
* Created by JAINISH on 1/31/2018.
*/
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTime {
public static final
String DATE_FORMAT
= "yyyy-MM-dd HH:mm:ss";
private Date
mDate;
private Calendar
mCalendar;
/**
* Constructor with no
parameters which will create DateTime
* Object with the current date
and time
*/
public DateTime() {
this(new Date());
}
/**
* Constructor with Date
parameter which will initialize the
* object to the date specified
* @param date
*/
public DateTime(Date date) {
mDate =
date;
mCalendar =
Calendar.getInstance();
mCalendar.setTime(mDate);
}
/**
* Constructor with DateFormat
and DateString which will be
* used to initialize the
object to the date string specified
* @param dateFormat String DateFormat
* @param dateString Date in String
*/
public DateTime(String dateFormat, String
dateString) {
mCalendar =
Calendar.getInstance();
SimpleDateFormat mFormat = new SimpleDateFormat(dateFormat);
try {
mDate =
mFormat.parse(dateString);
mCalendar.setTime(mDate);
} catch (ParseException
e) {
e.printStackTrace();
}
}
/**
* Constructor with DateString
formatted as default DateFormat
* defined in DATE_FORMAT
variable
* @param dateString
*/
public DateTime(String dateString) {
this(DATE_FORMAT,
dateString);
}
/**
* Constructor with Year,
Month, Day, Hour, Minute, and Second
* which will be use to set the
date to
* @param year Year
* @param monthOfYear Month of Year
* @param dayOfMonth Day of Month
* @param hourOfDay Hour of Day
* @param minuteOfHour Minute
* @param secondOfMinute Second
*/
public DateTime(int year,
int monthOfYear, int dayOfMonth,
int
hourOfDay, int minuteOfHour, int secondOfMinute) {
mCalendar =
Calendar.getInstance();
mCalendar.set(year,
monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute);
mDate =
mCalendar.getTime();
}
/**
* Constructor with Year, Month
Day, Hour, Minute which
* will be use to set the date
to
* @param year Year
* @param monthOfYear Month of Year
* @param dayOfMonth Day of Month
* @param hourOfDay Hour of Day
* @param minuteOfHour Minute
*/
public DateTime(int year,
int monthOfYear, int dayOfMonth,
int
hourOfDay, int minuteOfHour) {
this(year,
monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, 0);
}
/**
* Constructor with Date only
(no time)
* @param year Year
* @param monthOfYear Month of Year
* @param dayOfMonth Day of Month
*/
public DateTime(int year,
int monthOfYear, int dayOfMonth)
{
this(year,
monthOfYear, dayOfMonth, 0,0,0);
}
public Date
getDate() {
return mDate;
}
public Calendar
getCalendar() {
return mCalendar;
}
public String
getDateString(String dateFormat) {
SimpleDateFormat mFormat = new SimpleDateFormat(dateFormat);
return mFormat.format(mDate);
}
public String
getDateString() {
return getDateString(DATE_FORMAT);
}
public int getYear()
{
return mCalendar.get(Calendar.YEAR);
}
public int getMonthOfYear()
{
return mCalendar.get(Calendar.MONTH);
}
public int getDayOfMonth()
{
return mCalendar.get(Calendar.DAY_OF_MONTH);
}
public int getHourOfDay()
{
return mCalendar.get(Calendar.HOUR_OF_DAY);
}
public int getMinuteOfHour()
{
return mCalendar.get(Calendar.MINUTE);
}
public int getSecondOfMinute()
{
return mCalendar.get(Calendar.SECOND);
}
}
Create DateTimePicker.class File
package com.datetimedialog.datetimedialog;
/**
* Created by JAINISH on 1/31/2018.
*/
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TimePicker;
import java.util.Date;
/**
* Created by Arman.Pagilagan on 15/05/14.
*/
public class DateTimePicker extends
DialogFragment {
public static final
String TAG_FRAG_DATE_TIME
= "fragDateTime";
private static
final String KEY_DIALOG_TITLE = "dialogTitle";
private static
final String KEY_INIT_DATE = "initDate";
private static
final String TAG_DATE = "date";
private static
final String TAG_TIME = "time";
private Context
mContext;
private ButtonClickListener
mButtonClickListener;
private OnDateTimeSetListener
mOnDateTimeSetListener;
private Bundle
mArgument;
private DatePicker
mDatePicker;
private TimePicker
mTimePicker;
// DialogFragment constructor must be empty
private boolean not_future = false;
private String
value_select = null;
private LinearLayout
date_content,time_content;
public DateTimePicker()
{
}
@Override
public void onAttach(Activity
activity) {
super.onAttach(activity);
mContext =
activity;
mButtonClickListener
= new ButtonClickListener();
}
/**
*
* @param dialogTitle Title of the DateTimePicker
DialogFragment
* @param initDate Initial Date and Time set to
the Date and Time Picker
* @return Instance of the
DateTimePicker DialogFragment
*/
public static DateTimePicker newInstance(CharSequence
dialogTitle, Date initDate) {
// Create a new instance of DateTimePicker
DateTimePicker mDateTimePicker = new DateTimePicker();
// Setup the constructor parameters as arguments
Bundle mBundle = new
Bundle();
mBundle.putCharSequence(KEY_DIALOG_TITLE,
dialogTitle);
mBundle.putSerializable(KEY_INIT_DATE,
initDate);
mDateTimePicker.setArguments(mBundle);
// Return instance with arguments
return mDateTimePicker;
}
public static DateTimePicker
newInstance(CharSequence dialogTitle, Date initDate, boolean
not_future, String value_select) {
// Create a new instance of DateTimePicker
DateTimePicker mDateTimePicker = new DateTimePicker();
// Setup the constructor parameters as arguments
Bundle mBundle = new
Bundle();
mBundle.putCharSequence(KEY_DIALOG_TITLE,
dialogTitle);
mBundle.putSerializable(KEY_INIT_DATE,
initDate);
mDateTimePicker.setArguments(mBundle);
mDateTimePicker.not_future
= not_future;
mDateTimePicker.value_select
= value_select;
// Return instance with arguments
return mDateTimePicker;
}
@Override
public Dialog
onCreateDialog(Bundle savedInstanceState) {
// Retrieve Argument passed to the constructor
mArgument = getArguments();
// Use an AlertDialog Builder to initially create the Dialog
AlertDialog.Builder mBuilder = new AlertDialog.Builder(mContext);
// Setup the Dialog
mBuilder.setTitle(mArgument.getCharSequence(KEY_DIALOG_TITLE));
mBuilder.setNegativeButton(android.R.string.no,mButtonClickListener);
mBuilder.setPositiveButton(android.R.string.yes,mButtonClickListener);
// Create the Alert Dialog
AlertDialog mDialog = mBuilder.create();
// Set the View to the Dialog
mDialog.setView(
createDateTimeView(mDialog.getLayoutInflater())
);
// Return the Dialog created
return mDialog;
}
/**
* Inflates the XML Layout and
setups the tabs
* @param layoutInflater Layout inflater from the
Dialog
* @return Returns a view that
will be set to the Dialog
*/
private View createDateTimeView(LayoutInflater
layoutInflater) {
// Inflate the XML Layout using the inflater from the
created Dialog
View mView = layoutInflater.inflate(R.layout.date_time_picker,null);
// Extract the TabHost
TabHost mTabHost = (TabHost)
mView.findViewById(R.id.tab_host);
mTabHost.setup();
// // Create Date Tab and
add to TabHost
// TabHost.TabSpec mDateTab
= mTabHost.newTabSpec(TAG_DATE);
////
mDateTab.setIndicator(getString(R.string.tab_date));
//
mDateTab.setIndicator("Date");
//
mDateTab.setContent(R.id.date_content);
// mTabHost.addTab(mDateTab);
// // Create Time Tab and
add to TabHost
// TabHost.TabSpec mTimeTab
= mTabHost.newTabSpec(TAG_TIME);
////
mTimeTab.setIndicator(getString(R.string.tab_time));
//
mTimeTab.setIndicator("Time");
// mTimeTab.setContent(R.id.time_content);
//
mTabHost.addTab(mTimeTab);
// Retrieve Date from
Arguments sent to the Dialog
DateTime mDateTime = new DateTime((Date) mArgument.getSerializable(KEY_INIT_DATE));
// Initialize Date and Time Pickers
mDatePicker = (DatePicker) mView.findViewById(R.id.date_picker);
if (not_future){
mDatePicker.setMaxDate(System.currentTimeMillis());
}
mTimePicker =
(TimePicker) mView.findViewById(R.id.time_picker);
mDatePicker.init(mDateTime.getYear(),
mDateTime.getMonthOfYear(),
mDateTime.getDayOfMonth(), null);
mTimePicker.setCurrentHour(mDateTime.getHourOfDay());
mTimePicker.setCurrentMinute(mDateTime.getMinuteOfHour());
date_content
= (LinearLayout) mView.findViewById(R.id.date_content);
time_content
= (LinearLayout) mView.findViewById(R.id.time_content);
//value_select start
if (value_select.equalsIgnoreCase(DateFormateclass.str_date_time)){
// Create Date Tab and add to TabHost
TabHost.TabSpec mDateTab =
mTabHost.newTabSpec(TAG_DATE);
//
mDateTab.setIndicator(getString(R.string.tab_date));
mDateTab.setIndicator("Date");
mDateTab.setContent(R.id.date_content);
mTabHost.addTab(mDateTab);
// Create Time Tab and add to TabHost
TabHost.TabSpec mTimeTab =
mTabHost.newTabSpec(TAG_TIME);
//
mTimeTab.setIndicator(getString(R.string.tab_time));
mTimeTab.setIndicator("Time");
mTimeTab.setContent(R.id.time_content);
mTabHost.addTab(mTimeTab);
}else if (value_select.equalsIgnoreCase(DateFormateclass.str_date)){
// Create Date Tab and add to TabHost
TabHost.TabSpec mDateTab =
mTabHost.newTabSpec(TAG_DATE);
//
mDateTab.setIndicator(getString(R.string.tab_date));
mDateTab.setIndicator("Date");
mDateTab.setContent(R.id.date_content);
mTabHost.addTab(mDateTab);
date_content.setVisibility(View.VISIBLE);
time_content.setVisibility(View.GONE);
}else if (value_select.equalsIgnoreCase(DateFormateclass.str_time)){
// Create Time Tab and add to TabHost
TabHost.TabSpec mTimeTab =
mTabHost.newTabSpec(TAG_TIME);
//
mTimeTab.setIndicator(getString(R.string.tab_time));
mTimeTab.setIndicator("Time");
mTimeTab.setContent(R.id.time_content);
mTabHost.addTab(mTimeTab);
date_content.setVisibility(View.GONE);
time_content.setVisibility(View.VISIBLE);
}
//value_select end
// Return created view
return mView;
}
/**
* Sets the
OnDateTimeSetListener interface
* @param onDateTimeSetListener Interface that is used to send
the Date and Time
* to the calling object
*/
public void setOnDateTimeSetListener(OnDateTimeSetListener
onDateTimeSetListener) {
mOnDateTimeSetListener
= onDateTimeSetListener;
}
private class ButtonClickListener
implements DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface
dialogInterface, int result) {
// Determine if the user selected Ok
if(DialogInterface.BUTTON_POSITIVE == result) {
DateTime mDateTime = new DateTime(
mDatePicker.getYear(),
mDatePicker.getMonth(),
mDatePicker.getDayOfMonth(),
mTimePicker.getCurrentHour(),
mTimePicker.getCurrentMinute()
);
mOnDateTimeSetListener.DateTimeSet(mDateTime.getDate());
}
}
}
/**
* Interface for sending the
Date and Time to the calling object
*/
public interface OnDateTimeSetListener {
public void DateTimeSet(Date
date);
}
}
Create Activity MainActivity.class
File
package com.datetimedialog.datetimedialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.text.ParseException;
import java.util.Date;
public class MainActivity extends
AppCompatActivity implements View.OnClickListener,
DateTimePicker.OnDateTimeSetListener {
TextView txt_datetime,txt_date,txt_time;
Button btn_datetime,btn_date,btn_time;
String str;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_datetime
= findViewById(R.id.txt_datetime);
txt_date =
findViewById(R.id.txt_date);
txt_time =
findViewById(R.id.txt_time);
btn_datetime
= findViewById(R.id.btn_datetime);
btn_date =
findViewById(R.id.btn_date);
btn_time =
findViewById(R.id.btn_time);
btn_datetime.setOnClickListener(this);
btn_date.setOnClickListener(this);
btn_time.setOnClickListener(this);
}
@Override
public void onClick(View
view) {
switch (view.getId()){
case R.id.btn_datetime:
str =
DateFormateclass.str_date_time;
getDateTime("");
break;
case R.id.btn_date:
str =
DateFormateclass.str_date;
getDateTime("");
break;
case R.id.btn_time:
str =
DateFormateclass.str_time;
getDateTime("");
break;
}
}
private void getDateTime(String
str_date) {
Date date1 = null;
if (str_date
!= null && !str_date.equalsIgnoreCase("")){
try {
date1 = DateFormateclass.sdf_date.parse(str_date);
} catch (ParseException
e) {
e.printStackTrace();
}
}else {
date1 = new
Date();
}
if (str.equalsIgnoreCase(DateFormateclass.str_date_time)){
SimpleDateTimePicker
simpleDateTimePicker = SimpleDateTimePicker.make(
DateFormateclass.str_date_time,
date1,
this,
getSupportFragmentManager()
);
// Show It baby!
simpleDateTimePicker.show(false, DateFormateclass.str_date_time);
}else if (str.equalsIgnoreCase(DateFormateclass.str_date)){
SimpleDateTimePicker
simpleDateTimePicker = SimpleDateTimePicker.make(
DateFormateclass.str_date,
date1,
this,
getSupportFragmentManager()
);
// Show It baby!
simpleDateTimePicker.show(false, DateFormateclass.str_date);
}else if (str.equalsIgnoreCase(DateFormateclass.str_time)){
SimpleDateTimePicker
simpleDateTimePicker = SimpleDateTimePicker.make(
DateFormateclass.str_time,
date1,
this,
getSupportFragmentManager()
);
// Show It baby!
simpleDateTimePicker.show(false, DateFormateclass.str_time);
}
}
@Override
public void DateTimeSet(Date
date) {
DateTime mDateTime = new DateTime(date);
if (str.equalsIgnoreCase(DateFormateclass.str_date_time)){
txt_datetime.setText(DateFormateclass.sdf_date_time.format(date));
}else if (str.equalsIgnoreCase(DateFormateclass.str_date)){
txt_date.setText(DateFormateclass.sdf_date.format(date));
}else if (str.equalsIgnoreCase(DateFormateclass.str_time)){
txt_time.setText(DateFormateclass.sdf_time.format(date));
}
}
}
Create DateFormateclass.class File
package com.datetimedialog.datetimedialog;
/**
* Created by JAINISH on 1/31/2018.
*/
import java.text.SimpleDateFormat;
public class DateFormateclass {
public final static
SimpleDateFormat sdf_date_time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
public final static
SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy-MM-dd");
public final static
SimpleDateFormat sdf_time = new SimpleDateFormat("HH:mm:ss");
public final static
String str_date_time
="Set Date & Time";
public final static
String str_date
="Set Date";
public final static
String str_time
="Set Time";
}
Create SimpleDateTimePicker.class
File
package com.datetimedialog.datetimedialog;
/**
* Created by JAINISH on 1/31/2018.
*/
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import java.util.Date;
public class SimpleDateTimePicker {
private CharSequence
mDialogTitle;
private Date
mInitDate;
private DateTimePicker.OnDateTimeSetListener
mOnDateTimeSetListener;
private FragmentManager
mFragmentManager;
/**
* Private constructor that can
only be access using the make method
* @param dialogTitle Title of the Date Time Picker
Dialog
* @param initDate Date object to use to set the
initial Date and Time
* @param onDateTimeSetListener OnDateTimeSetListener
interface
* @param fragmentManager Fragment Manager from the
activity
*/
private SimpleDateTimePicker(CharSequence
dialogTitle, Date initDate,
DateTimePicker.OnDateTimeSetListener onDateTimeSetListener,
FragmentManager
fragmentManager) {
// Find if there are any DialogFragments from the
FragmentManager
FragmentTransaction mFragmentTransaction
= fragmentManager.beginTransaction();
Fragment mDateTimeDialogFrag =
fragmentManager.findFragmentByTag(
DateTimePicker.TAG_FRAG_DATE_TIME
);
// Remove it if found
if(mDateTimeDialogFrag != null) {
mFragmentTransaction.remove(mDateTimeDialogFrag);
}
mFragmentTransaction.addToBackStack(null);
mDialogTitle
= dialogTitle;
mInitDate =
initDate;
mOnDateTimeSetListener
= onDateTimeSetListener;
mFragmentManager
= fragmentManager;
}
/**
* Creates a new instance of
the SimpleDateTimePicker
* @param dialogTitle Title of the Date Time Picker
Dialog
* @param initDate Date object to use to set the
initial Date and Time
* @param onDateTimeSetListener OnDateTimeSetListener
interface
* @param fragmentManager Fragment Manager from the
activity
* @return Returns a
SimpleDateTimePicker object
*/
public static SimpleDateTimePicker make(CharSequence
dialogTitle, Date initDate,
DateTimePicker.OnDateTimeSetListeneronDateTimeSetListener, FragmentManager fragmentManager) {
return new SimpleDateTimePicker(dialogTitle,
initDate,
onDateTimeSetListener,
fragmentManager);
}
/**
* Shows the DateTimePicker
Dialog
*/
public void show() {
// Create new DateTimePicker
DateTimePicker mDateTimePicker =
DateTimePicker.newInstance(mDialogTitle,mInitDate);
mDateTimePicker.setOnDateTimeSetListener(mOnDateTimeSetListener);
mDateTimePicker.show(mFragmentManager, DateTimePicker.TAG_FRAG_DATE_TIME);
}
public void show(boolean not_future, String value_select) {
// Create new DateTimePicker
DateTimePicker mDateTimePicker = DateTimePicker.newInstance(mDialogTitle,mInitDate,not_future,value_select);
mDateTimePicker.setOnDateTimeSetListener(mOnDateTimeSetListener);
mDateTimePicker.show(mFragmentManager, DateTimePicker.TAG_FRAG_DATE_TIME);
}
}
Add activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context="com.datetimedialog.datetimedialog.MainActivity"
android:orientation="vertical"
android:padding="5dp"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:text="Date Time
: "
android:textStyle="bold"
android:textSize="20dp"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/txt_datetime"
android:textStyle="bold"
android:textSize="20dp"
android:gravity="center"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_datetime"
android:text="Date
Time"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:text="Date :
"
android:textStyle="bold"
android:textSize="20dp"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/txt_date"
android:textStyle="bold"
android:textSize="20dp"
android:gravity="center"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_date"
android:text="Date"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:text="Time :
"
android:textStyle="bold"
android:textSize="20dp"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/txt_time"
android:textStyle="bold"
android:textSize="20dp"
android:gravity="center"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_time"
android:text="Time"/>
</LinearLayout>
Add date_time_picker.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/date_time"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- TabHost that will hold the Tabs and its content
-->
<TabHost
android:id="@+id/tab_host"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- TabWidget that will hold the Tabs -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Layout that will hold the content of the Tabs -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Layout for the DatePicker -->
<LinearLayout
android:id="@+id/date_content"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:calendarViewShown="false" />
</LinearLayout>
<!-- Layout for the TimePicker -->
<LinearLayout
android:id="@+id/time_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TimePicker
android:id="@+id/time_picker"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
No comments:
Post a Comment