DateTimePicker run different depends on api level in Android - android

I want to use Date Time picker in my project .
http://www.parallelcodes.com/android-datetime-picker-example/
This tutorial works perfectly. When I run this datetimepicker in api 19 showing like this web site and I want to do this.
When I run api 22 datetimepicker appear like this.
So, Any suggestions?

I used this code to solve this issue,
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
private Calendar calendar;
private Calendar maxCalendar;
private Calendar minCalendar;
private DatePickerDialog.OnDateSetListener onDateSetListener;
private static final String TAG = DatePickerFragment.class.getSimpleName();
public void setup(Calendar calendar, DatePickerDialog.OnDateSetListener onDateSetListener){
this.calendar = calendar;
this.onDateSetListener = onDateSetListener;
}
public void setup(Calendar calendar, Calendar maxCalendar, DatePickerDialog.OnDateSetListener onDateSetListener){
this.calendar = calendar;
this.maxCalendar = maxCalendar;
this.onDateSetListener = onDateSetListener;
}
public void setup(Calendar calendar, Calendar maxCalendar, Calendar minCalendar){
this.calendar = calendar;
this.maxCalendar = maxCalendar;
this.minCalendar = minCalendar;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Light_Dialog_MinWidth, this, year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
if(minCalendar!=null){
dialog.getDatePicker().setMinDate(minCalendar.getTimeInMillis());
}
if(maxCalendar != null){
dialog.getDatePicker().setMaxDate(maxCalendar.getTimeInMillis());
}
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
if(onDateSetListener != null){
onDateSetListener.onDateSet(view, year, month, day);
}else{
Log.w(TAG, "onDateSetListener callback is not initialized.");
}
}
}
and call like this ,
public void showDatePicker() {
Calendar calendar = Calendar.getInstance();
DatePickerFragment fragment = new DatePickerFragment();
fragment.setup(calendar, new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
}
});
fragment.show(getSupportFragmentManager(), null);
}

This is android own calendar which design changes for different API. If you want to use same calendar for all API version then you should try with a custom calendar rather then default calendar
If still have any confusion then please let me know

Related

How do I subtract two date pickers in Android?

I have two date pickers in my app (fromDate and toDate). When the user selects and sets both dates from these two date pickers, I want my app to automatically show the number of days between them in another edit text (days) box.
How can I do it?
First add OnDateChangedListener for both of the date pickers as
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
fromDate.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
UpdateText();
}
});
toDate.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
UpdateText();
}
});
Create a function UpdateText to update the EditText days
private void UpdateText() {
Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
localCalendar.set(fromDate.getYear(), fromDate.getMonth(), fromDate.getDayOfMonth());
int day1 = localCalendar.get(Calendar.DAY_OF_YEAR);
localCalendar.set(toDate.getYear(), toDate.getMonth(), toDate.getDayOfMonth());
int day2 = localCalendar.get(Calendar.DAY_OF_YEAR);
int day = day2-day1;
days.setText(""+day);
}
Note: Make sure you are imported the right files
import java.util.Calendar;
import java.util.TimeZone;
Complete program
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;
import java.util.TimeZone;
public class MainActivity extends AppCompatActivity {
DatePicker fromDate, toDate;
EditText days;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fromDate = (DatePicker) findViewById(R.id.datePicker1);
toDate = (DatePicker) findViewById(R.id.datePicker2);
days = (EditText)findViewById(R.id.Days);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
fromDate.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
UpdateText();
}
});
toDate.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
UpdateText();
}
});
}
private void UpdateText() {
Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
localCalendar.set(fromDate.getYear(), fromDate.getMonth(), fromDate.getDayOfMonth());
int day1 = localCalendar.get(Calendar.DAY_OF_YEAR);
localCalendar.set(toDate.getYear(), toDate.getMonth(), toDate.getDayOfMonth());
int day2 = localCalendar.get(Calendar.DAY_OF_YEAR);
int day = day2-day1;
days.setText(""+day);
}
}

Android Datepicker - Always show date month year spinner layout instead of calendar view

While using android datepicker, I want to always show the old date month year spinner instead of new calendar view ? How can I do that?
I want this for all device running any android version:
and dont want following ui:
I used this answer https://stackoverflow.com/a/31610267/5700434 to completely solve the problem.
Mainly use this :
DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Light_Dialog_MinWidth, this, year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Full code :
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
private Calendar calendar;
private Calendar maxCalendar;
private Calendar minCalendar;
private DatePickerDialog.OnDateSetListener onDateSetListener;
private static final String TAG = DatePickerFragment.class.getSimpleName();
public void setup(Calendar calendar, DatePickerDialog.OnDateSetListener onDateSetListener){
this.calendar = calendar;
this.onDateSetListener = onDateSetListener;
}
public void setup(Calendar calendar, Calendar maxCalendar, DatePickerDialog.OnDateSetListener onDateSetListener){
this.calendar = calendar;
this.maxCalendar = maxCalendar;
this.onDateSetListener = onDateSetListener;
}
public void setup(Calendar calendar, Calendar maxCalendar, Calendar minCalendar){
this.calendar = calendar;
this.maxCalendar = maxCalendar;
this.minCalendar = minCalendar;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Light_Dialog_MinWidth, this, year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
if(minCalendar!=null){
dialog.getDatePicker().setMinDate(minCalendar.getTimeInMillis());
}
if(maxCalendar != null){
dialog.getDatePicker().setMaxDate(maxCalendar.getTimeInMillis());
}
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
if(onDateSetListener != null){
onDateSetListener.onDateSet(view, year, month, day);
}else{
LogUtils.w(TAG, "onDateSetListener callback is not initialized.");
}
}
}
There are many libraries available for android date picker( eg: exmple1 example2 example3 . or you can use default date picker. This date picker may be the one you are looking for
DatePickerDialog dialog = new
DatePickerDialog(getActivity(),AlertDialog.THEME_HOLO_LIGHT, this, year, month, day);
This worked perfectly in all the version of android. from 4.4 to 6.0
tested although it is deprecated but it supported very well.
Use the DatePicker instead of the Calendar View, and add the following xml attributes:
android:datePickerMode="spinner"
android:calendarViewShown="false"
Giving the following result:

How to get date "value" from DatePicker?

I don't know how to get the date value from Datepicker.
PickerDialog.java
package com.example.alecksjohanssen.newyorktimes;
/**
* Created by AlecksJohanssen on 3/19/2016.
*/
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import java.util.Calendar;
public class PickerDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{ DateSettings dateSettings = new DateSettings(getActivity());
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayofmonth = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog;
dialog = new DatePickerDialog(getActivity(),dateSettings,year,month,dayofmonth);
return dialog;
}
}
DateSetting.java
package com.example.alecksjohanssen.newyorktimes;
import android.app.DatePickerDialog;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.Toast;
/**
* Created by My name Is Hardline on 2/26/2016.
*/
public class DateSettings implements DatePickerDialog.OnDateSetListener {
Context context;
public DateSettings(Context context) {
this.context = context;
}
#Override
public void onDateSet(DatePicker view, int year, int monthofyear,int dayofmonth) {
Toast.makeText(context, "Selected date" + monthofyear + "/" + dayofmonth + "/" + year, Toast.LENGTH_LONG).show();
}
}
public void setDate() {
PickerDialog pickerDialogs = new PickerDialog();
pickerDialogs.show(getFragmentManager(), "date_picker");
}
It's basically it, it's only shows that Selected date is . Still I'm looking for how to extract the date data so that I can put it on my Begin Date :P.
You can try like this
private void datePicker(){
Calendar calendar = Calendar.getInstance();
final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
DatePickerDialog datePickerDialog = new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
System.out.print(dateFormatter.format(newDate.getTime()));
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
Say you have Calendar mCalendar that you want to init the date picker with it.
dialog.getDatePicker().init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker datePicker, int i, int i2, int i3) {
mCalendar.set(i, i2, i3);
}
});
Then to get the picked date
mCalendar.getTime()

Android: DatePicker value saved in a variable doesn't go in onCreate

I have a rather simple issue but this is bugging me out!
I am trying to build an app in which I have 2 DatePickers, periodFrom and periodTo.
Basically what I'm trying to do is let the user pick dates on both cases and then calculate the difference between dates in days. I already know how to get the difference, how to get the date from the DatePicker. The problem is I don't know where I should get the value from the DatePicker, as my onCreate intializes the listener for the DatePickers but it doesn't store the set dates in the global variables, only the current date which it gets from Calendar.getInstance();
Here is my code for the MainActivity, maybe you can enlighten me on this issue I'm having. Thank you!
MainActivity.class
package com.endtech.utilitycalculator;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private EditText periodFrom, periodTo;
private Calendar mCalendarFrom = Calendar.getInstance();
private Calendar mCalendarTo = Calendar.getInstance();
private long from, to;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
periodFrom = (EditText) findViewById(R.id.periodFrom);
periodTo = (EditText) findViewById(R.id.periodTo);
initListeners();
from = mCalendarFrom.getTimeInMillis(); //This is current time, not the set time
to = mCalendarTo.getTimeInMillis(); //This is current time, not the set time
}
private void initListeners() {
setDateFrom();
setDateTo();
}
private void setDateFrom() {
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendarFrom.set(Calendar.YEAR, year);
mCalendarFrom.set(Calendar.MONTH, monthOfYear);
mCalendarFrom.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabelFrom();
}
};
periodFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(MainActivity.this, date, mCalendarFrom.get(Calendar.YEAR),
mCalendarFrom.get(Calendar.MONTH), mCalendarFrom.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void updateLabelFrom() {
String mFormat = "dd/MM/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(mFormat, Locale.GERMANY);
periodFrom.setText(sdf.format(mCalendarFrom.getTime()));
}
private void setDateTo() {
final DatePickerDialog.OnDateSetListener date2 = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendarTo.set(Calendar.YEAR, year);
mCalendarTo.set(Calendar.MONTH, monthOfYear);
mCalendarTo.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabelTo();
}
};
periodTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(MainActivity.this, date2, mCalendarTo.get(Calendar.YEAR),
mCalendarTo.get(Calendar.MONTH), mCalendarTo.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void updateLabelTo() {
String mFormat = "dd/MM/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(mFormat, Locale.GERMANY);
periodTo.setText(sdf.format(mCalendarTo.getTime()));
}
}
The problem is you are getting the dates before selecting them.
Simply Create a button (when you click on the button then the process of converting difference into days get done). then on button click get the values of dates and then convert it into days.
Let me know if you find some problem by commenting below.

Datepicker created with DialopFragment showing Calender also

I am new to Android programming and I am using a basic Datepicker created using a DialogFragment. My DatePicker shows up fine on click etc., but the problem is that it is displaying the Calendar view as well. I do not want this. I have been searching for a solution for a day now, and I am unwilling to use custom Datepickers right now, as I want to get familiar with the basics first.
I have also read suggestions such as
yourDatepicker.setCalendarViewShown(false);
or set it to false in the XML. But my DatePicker comes from a DialogFragment, so how do I access this datepicker? How do I set its final view? I do not wish to make changes in the source code as I am still learning the ropes.
My datepicker code:
public class DueDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day){
// Code to set Due Date TextField to the selected date.
TextView reminderText = (TextView)getActivity().findViewById(R.id.addTaskFormDueDateHolder);
String dueDateStr = year + "-" + month + "-" + day;
reminderText.setText(dueDateStr);
}
}
I have used Datepicker Dialog Fragment, to show the Date picker in dialog.
Here is my Complete Code
MainActivity.java
package com.example.testmydrag;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.DatePicker;
import android.support.v4.app.FragmentActivity;
import android.app.Dialog;
import android.app.DatePickerDialog;
import android.support.v4.app.DialogFragment;
import java.util.Calendar;
public class MainActivity extends FragmentActivity {
EditText mEdit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void selectDate(View view) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getSupportFragmentManager(), "DatePicker");
}
public void setTheDate(int year, int month, int day) {
mEdit = (EditText)findViewById(R.id.Text);
mEdit.setText(month+"/"+day+"/"+year);
}
/*
* Date Picker Dialog
*/
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int yy = c.get(Calendar.YEAR);
int mm = c.get(Calendar.MONTH);
int dd = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(),this,c.YEAR, c.MONTH, c.DATE);
/*Calendar View if you want to Remove Set it to False*/
dialog.getDatePicker().setCalendarViewShown(true);
/*Spinner View if you want to Show Set it to True*/
dialog.getDatePicker().setSpinnersShown(false);
dialog.setTitle("Pick a date");
return dialog;
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
setTheDate(yy, mm+1, dd);
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:text="#+string/date_text"
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+string/pick_date"
android:onClick="selectDate" />
</LinearLayout>
I found a temporary solution, though I am not entirely happy with it. You need minimum SDK version 11 for this.
Basically I added a call to getDatePicker() in my onAcreateDialog function, mentioned in the DueDatePickerFragment, and then changed its settings. I don't understand why I need to do this, and why it is appearing so incorrectly in my emulator. No one else seems to be having this problem. Any explanations would be highly appreciated. Here is the code:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
Log.i("DUEDATEPICKERDIALOG", "Inside onCreateDIALOG");
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialogObj = new DatePickerDialog(getActivity(), this, year, month, day);
DatePicker dueDatePicker = datePickerDialogObj.getDatePicker();
float scaleVal = (float) 0.6;
dueDatePicker.setScaleX(scaleVal);
dueDatePicker.setScaleY(scaleVal);
dueDatePicker.setCalendarViewShown(false);
return datePickerDialogObj;
}
Hope this helps someone.

Categories

Resources