disable dates before today date in DatePickerDialog Android?
You can use this line of code
datePicker.setMinDate(System.currentTimeMillis() - 1000);
You can use it like this. I tested it and it works
import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton = (Button)findViewById(R.id.m_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog mDate = new DatePickerDialog(MainActivity.this, date, 2016, 2, 24);
mDate.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
mDate.show();
}
});
}
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
view.setMinDate(System.currentTimeMillis() - 1000);
}
};
}
How to disable dates after today date in DatePickerDialog Android?
Just pass current date in setMaxDate()
of DatePickerDialog
.
DatePickerDialog dpDialog = new DatePickerDialog(getContext(), myDateListener, mYear, mMonth, mDay);
dpDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
or
dpDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
How to disable dates before today date in DatePickerDialog Android?
See this example..!
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class MyAndroidAppActivity extends Activity {
private TextView tvDisplayDate;
private Button btnChangeDate;
private int myear;
private int mmonth;
private int mday;
static final int DATE_DIALOG_ID = 999;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setCurrentDateOnView();
addListenerOnButton();
}
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
final Calendar c = Calendar.getInstance();
myear = c.get(Calendar.YEAR);
mmonth = c.get(Calendar.MONTH);
mday = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(mmonth + 1).append("-").append(mday).append("-")
.append(myear).append(" "));
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
DatePickerDialog _date = new DatePickerDialog(this, datePickerListener, myear,mmonth,
mday){
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
if (year < myear)
view.updateDate(myear, mmonth, mday);
if (monthOfYear < mmonth && year == myear)
view.updateDate(myear, mmonth, mday);
if (dayOfMonth < mday && year == myear && monthOfYear == mmonth)
view.updateDate(myear, mmonth, mday);
}
};
return _date;
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
myear = selectedYear;
mmonth = selectedMonth;
mday = selectedDay;
// set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(mmonth + 1)
.append("-").append(mday).append("-").append(myear)
.append(" "));
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:id="@+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (M-D-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
how to disable dates till current date?
Try this.
DatePickerDialog datePickerDialog = new DatePickerDialog(Create_slots.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//editText_Date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); //'dd-mm-yyy'
editText_Date.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth); //'yyy-mm-dd'
date1 = editText_Date.getText().toString();
System.out.println("date1 = "+date1);
}
}, mYear, mMonth, mDay);
Calendar mcurrentDate = Calendar.getInstance();
mcurrentDate.add(Calendar.DATE, 1);
datePickerDialog.getDatePicker().setMinDate(mcurrentDate.getTimeInMillis());
datePickerDialog.show();
How to disable past dates in Android date picker?
You can do
datePicker.setMinDate(System.currentTimeMillis() - 1000);
which sets today's date as minimum date and all the past dates are disabled.
datePicker
is an object of DatePicker
if you are using an object of DatePickerDialog
you can do
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
Note: setMinDate
was introduced in API 11
Disable past date in DatePickerDialog
First of all declare as following:
private Calendar myCalendar = Calendar.getInstance();
then like your code:-
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String selectedDate = dateFormat.format(calendar.getTime());
btnDate.setText(selectedDate);
btnDate.setTextColor(getResources().getColor(colorPrimaryDark));
}
};
make this change in your code instead of your last two lines:
`Date newDate = calendar.getTime();
new DatePickerDialog(TutorAddNewTimeSlotActivity.this, dateSetListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();`
change it and make it like following:
datePickerDialog.getDatePicker().setMinDate(myCalendar.getTimeInMillis());
datePickerDialog.show();
which will disable past date.
How to Disable future dates in Android date picker
Get the DatePicker
from DatePickerDialog
with getDatePicker()
. Set the max date to current date with setMaxDate()
:
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
Requires API level 11.
Related Topics
Taking a "Screenshot" of a Specific Layout in Android
Changing Locale: Force Activity to Reload Resources
Difference Between Addvalueeventlistener() and Addlistenerforsinglevalueevent() of Firebase
Onkeylistener Not Working with Soft Keyboard (Android)
How to Get Chrome Browsing History/Bookmarks in Our Android App
How to Draw Dashed Polyline with Android Google Map Sdk V2
How to Implement Rounded Corners to a Mapfragment
Detecting Gps On/Off Switch in Android Phones
Sending Pictures to a Web Server
How to Reverse the Direction of Marquee of a Textview
How to Pass Arraylist<Hashmap<String, String>>From One Activity to Another
E/Surface﹕ Getslotfrombufferlocked: Unknown Buffer: 0Xab7519C0
Gesture Detection and Scrollview Issue
Setting Android Theme Background Color