I want to fix the month and year in datePickerDialog but want the user to pick date on his own. Is it possible? ex: DatePickerDialog will be fixed with month May and year 2015, user can now only change the dates between 1 to 31
Asked
Active
Viewed 71 times
-4
-
refer this link http://stackoverflow.com/questions/21321789/android-datepicker-change-to-only-month-and-year – Anjali Tripathi May 30 '15 at 08:31
-
@Priya please find my below solution for that – Ravindra Kushwaha May 30 '15 at 08:31
-
If you just cant answer then its ohk , but why are you downvoting the question??Its a valid question and its according to my project need. – Anupriya May 30 '15 at 08:42
-
I did not down vote you..Even i tried to help you to short out from these problem.Check the below answer – Ravindra Kushwaha May 30 '15 at 08:44
-
@Ravindra I am not saying to you yar, am just saying to those who dont have guts to answer the question but just know only how to downvote the answer – Anupriya May 30 '15 at 08:47
-
@Priya...I have updated the code...Please check below – Ravindra Kushwaha May 30 '15 at 10:53
-
@Priya ...Did yours problem resolve?? – Ravindra Kushwaha Jun 01 '15 at 08:31
-
Finally I solved my problem,I have mentioned my answer below,,you can see there for any help,,, – Anupriya Jun 01 '15 at 10:43
3 Answers
1
I solved my problem in the following way:
Calendar cal = Calendar.getInstance();
DatePickerDialog dialog = new DatePickerDialog(this, myDateSetListener, year, month,
day);
cal.set(Calendar.MONTH, userMonthValue);
cal.set(Calendar.YEAR, userYearValue);
cal.set(Calendar.DAY_OF_MONTH, 1);
dialog.getDatePicker().setMinDate(cal.getTimeInMillis() - 1000);
// code to set max date
cal = Calendar.getInstance();
cal.set(Calendar.MONTH, userMonthValue);
cal.set(Calendar.YEAR, userYearValue);
cal.set(Calendar.DAY_OF_MONTH, cal.getMaximum(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMaxDate(cal.getTimeInMillis() - 1000);
Now my date picker dialog is fixed for month and year,,,and user is able to change only the dates between 1 and 30 or 31

Anupriya
- 1,663
- 3
- 17
- 28
0
You can use below lines of code
public class First extends Activity {
DatePicker _date;
Date date_current;
Date date_future;
int days_count=0;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Calendar ci = Calendar.getInstance();
String CiDateTime = ci.get(Calendar.YEAR) + "-" +
(ci.get(Calendar.MONTH) + 1) + "-" +
ci.get(Calendar.DAY_OF_MONTH);
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
try {
date_current = f.parse(CiDateTime);
long milliseconds = date_current.getTime();
_date.setMinDate(milliseconds);//here is _date is DatePicker Object
int monthMaxDays = ci.getActualMaximum(Calendar.DAY_OF_MONTH);
if(monthMaxDays>ci.get(Calendar.DAY_OF_MONTH))
{
days_count = monthMaxDays-ci.get(Calendar.DAY_OF_MONTH);
String CiDateMax = ci.get(Calendar.YEAR) + "-" +
(ci.get(Calendar.MONTH) + 1) + "-" +
ci.get(Calendar.DAY_OF_MONTH)+days_count;
date_future = f.parse(CiDateMax);
long milliseconds_MAx = date_future.getTime();
_date.setMaxDate(milliseconds_MAx);
}
else
{
_date.setMaxDate(milliseconds);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Ravindra Kushwaha
- 7,846
- 14
- 53
- 103
-
@priya..Is it workable for you..If there is any concern than plz let me know – Ravindra Kushwaha May 30 '15 at 09:07
-
using ur code today date will be set as the mindate, but i want user to set date himself while month and year is fixed. – Anupriya May 30 '15 at 10:02
0
You can get the underlying DatePicker from a DatePickerDialog (by simply calling getDatePicker()) and set its bounds using :