0

I am using a tutorial i found on the net for datepicker.But i want to set the maximum and minimum date for the datepicker.Also i have heard that there is an issue for API level less than 11.

My code:

package com.androidexample.datepicker;

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 DatePickerExample extends Activity {

    private TextView Output;
    private Button changeDate;

    private int year;
    private int month;
    private int day;

    static final int DATE_PICKER_ID = 1111; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Output = (TextView) findViewById(R.id.Output);
        changeDate = (Button) findViewById(R.id.changeDate);

        // Get current date by calender

        final Calendar c = Calendar.getInstance();
        year  = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day   = c.get(Calendar.DAY_OF_MONTH);

        // Show current date

        Output.setText(new StringBuilder()
                // Month is 0 based, just add 1
                .append(month + 1).append("-").append(day).append("-")
                .append(year).append(" "));

        // Button listener to show date picker dialog

        changeDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // On button click show datepicker dialog 
                showDialog(DATE_PICKER_ID);

            }

        });
   }


    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_PICKER_ID:

            // open datepicker dialog. 
            // set date picker for current date 
            // add pickerListener listner to date picker
            return new DatePickerDialog(this, pickerListener, year, month,day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {

            year  = selectedYear;
            month = selectedMonth;
            day   = selectedDay;

            // Show selected date 
            Output.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));

           }
        };

}

The xml code:

<?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/changeDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click To Change Date" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current/Selected Date (M-D-YYYY): "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/Output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />



</LinearLayout>

The problem with the above code is that there is no upper limit on the year part.I also need a lower limit on the year part.Can someone tell me as how to modify the code to set the upper and lower limit on the year part?

user3852672
  • 285
  • 1
  • 8
  • 17
  • 2
    `DatePicker` has `setMinDate()` and `setMaxDate()` methods. – RP- Sep 15 '14 at 04:57
  • possible duplicate of [Android datepicker min max date before api level 11](http://stackoverflow.com/questions/10836679/android-datepicker-min-max-date-before-api-level-11) – Andrew T. Sep 15 '14 at 04:58
  • But i am not using datepicker control in the xml file.Can someone tell me as how to modify my code to set maximum and minimum dates? – user3852672 Sep 15 '14 at 04:59
  • @Andrew , in the example of the link the datepicker is defined in the xml file.But in my example datepicker is not defined in xml file. – user3852672 Sep 15 '14 at 05:03
  • @user3852672 have you tried at least 1 of the answers? Some of the answers are related to `DatePickerDialog`. – Andrew T. Sep 15 '14 at 05:11

3 Answers3

0

In DatePickerDialog.OnDateSetListener() you can compare the month, year , date with your specified min and max dates.

DjP
  • 4,537
  • 2
  • 25
  • 34
0

You can obtain DatePicker using DatePickerDialog.

DatePickerDialog dialog = new DatePickerDialog(this, pickerListener, year, month,day);
//calculate min and max dates (you can use, Calender API or Joda Time, just search for it.
dialog.getDatePicker().setMaxDate(maxDate.getTime());
dialog.getDatePicker().setMinDate(minDate.getTime());
return dialog;
RP-
  • 5,827
  • 2
  • 27
  • 46
0

You can get the underlying DatePicker from a DatePickerDialog by simply calling getDatePicker() and set its bounds using:

setMinDate(long minDate)

setMaxDate(long maxDate)

This works only for API level 11 and above.

Aniruddha
  • 4,477
  • 2
  • 21
  • 39