3
public void onClick(View view) {
    Calendar c = Calendar.getInstance();

    int cyear = c.get(Calendar.YEAR);
    int cmonth = c.get(Calendar.MONTH);
    int cday = c.get(Calendar.DAY_OF_MONTH);        
    DatePickerDialog dialog = new DatePickerDialog(Students.this,new OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            String date_selected = String.valueOf(dayOfMonth)+" /"+String.valueOf(monthOfYear+1)+" /"+String.valueOf(year);
            dateFieldTextBox.setText(date_selected);
            //                              CampusNewsISActivity.showLog("dateFieldTextBox selected");
        }
    },cyear,cmonth,cday);
    //          dialog.setCancelable(false);
    dialog.show();
}

want to validate datepickerdialof for bday ie. it should not accept birthdate before 1st jan 1985

Iswanto San
  • 18,263
  • 13
  • 58
  • 79
Dhruvil
  • 49
  • 2
  • 8
  • 1
    http://stackoverflow.com/questions/6421874/how-to-get-the-date-set-in-the-datepicker-widget-in-android/7961268#7961268 – Raghunandan Apr 19 '13 at 11:46

1 Answers1

1
package com.example.pickerdate;

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.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private TextView mDateDisplay;
    private int mYear;
    private int year1;
    private int mMonth;
    private int mDay;
    private int month;
    private int day;
    static final int DATE_DIALOG_ID = 1;
    Button pickDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        pickDate = (Button) findViewById(R.id.pickDate);
        pickDate.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                System.out.println("hello3");
                showDialog(DATE_DIALOG_ID);

            }
        });

        System.out.println("hello1");
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        updateDisplay();
        year1 = mYear;
        month = mMonth;
        day = mDay;
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        DatePickerDialog _date = null;
        switch (id) {

        case DATE_DIALOG_ID:
            _date = new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                    mDay) {
                @Override
                public void onDateChanged(DatePicker view, int year,
                        int monthOfYear, int dayOfMonth) {
                    System.out.println("----------onDateChanged()-----------"
                            + mYear + "--" + year);
                    System.out.println("----------onDateChanged()-----------"
                            + mMonth + "--" + monthOfYear);
                    System.out.println("----------onDateChanged()-----------"
                            + mDay + "--" + dayOfMonth);

                    /*
                     * These lines of commented code used for only setting the
                     * maximum date on Date Picker..
                     * 
                     * if (year > mYear && year) 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);
                     */

                    // these below lines of code used for setting the maximum as
                    // well as minimum dates on Date Picker Dialog..

                    if ((mYear > year)
                            || ((mMonth > monthOfYear) && (mYear == year))
                            || ((mDay > dayOfMonth) && (mYear == year) && (mMonth == monthOfYear))) {
                        view.updateDate(year1, month, day);

                    }

                }
            };

        }
        System.out.println("hello5");
        return _date;
    }

    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {

        case DATE_DIALOG_ID:
            System.out.println("hello6");

            ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
            break;
        }
    }

    private void updateDisplay() {
        System.out.println("hello2");
        mDateDisplay.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-").append(mDay).append("-")
                .append(mYear).append(" "));
    }

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

        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            System.out.println("hello7");

            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            System.out.`enter code here`println("year=" + year);
            System.out.println("month=" + monthOfYear);
            System.out.println("day=" + dayOfMonth);
            updateDisplay();
        }
    };
}
Harsh Parikh
  • 3,845
  • 3
  • 14
  • 14
  • please don't copy the exact same answer to several questions - chances are that they either don't really fit or the questions are duplicates (which should be flagged as such, once you'll have enough rep) – kleopatra Jul 26 '13 at 06:18