Android: DatePicker and DatePicker Dialog

58,050

Solution 1

Here I Provide Some Code Hope It Helps You..

public class NewReminder extends Activity {
private static final int DATE_DIALOG_ID = 1;
private int year;
private int month;
private int day;
EditText editTextDate;
private String currentDate;
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addnewreminder);
    initialize();
    context = getApplicationContext();
    OnClickListener listenerDate = new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            final Calendar c = Calendar.getInstance();
            year = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH);
            day = c.get(Calendar.DAY_OF_MONTH);
            showDialog(DATE_DIALOG_ID);
        }
    };
    editTextDate.setOnClickListener(listenerDate);

}

private void initialize() {
    // TODO Auto-generated method stub

    editTextDate = (EditText) findViewById(R.id.editTextDate);

}


private void updateDisplay() {
    currentDate = new StringBuilder().append(day).append(".")
            .append(month + 1).append(".").append(year).toString();

    Log.i("DATE", currentDate);
}

OnDateSetListener myDateSetListener = new OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker datePicker, int i, int j, int k) {

        year = i;
        month = j;
        day = k;
        updateDisplay();
        editTextDate.setText(currentDate);
    }
};



@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, myDateSetListener, year, month,
                day);
    }
    return null;
}
}

Solution 2

Old dialogs are deprecated. Implements fragments:

Migrate your Activity to Fragment Activity:

public class YourActivity extends FragmentActivity 

Create Fragment Dialog:

public class TimePickerFragment extends DialogFragment {

    private OnDateSetListener listener;

    public TimePickerFragment(OnDateSetListener listener) {
        this.listener=listener;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        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);

        // Create a new instance of TimePickerDialog and return it
        return new DatePickerDialog(getActivity(), listener, year,month,day);
    }

}

Implements interface (package: import android.app.DatePickerDialog.OnDateSetListener):

public class YourActivity extends FragmentActivity implements OnDateSetListener{

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

    }

}

Add showDialog function:

public class YourActivity extends FragmentActivity implements OnDateSetListener{

    showDateDialog(){

        FragmentManager fm = getSupportFragmentManager();
        TimePickerFragment newFragment = new TimePickerFragment(this);
        newFragment.show(fm, "date_picker");

    }

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

    }

}

Solution 3

In XML add a TextView and a Button

<TextView
    android:id="@+id/searchText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<Button
    android:id="@+id/search"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Search" />

Add following code in java file

public class DatePickerDialogExample extends Activity {

    TextView txtDate;
    private int mYear, mMonth, mDay, mHour, mMinute;

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


        txtDate = (TextView) findViewById(R.id.searchText);
        Button search = (Button) findViewById(R.id.search);
        search.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Process to get Current Date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);

            // Launch Date Picker Dialog
            DatePickerDialog dpd = new DatePickerDialog(PrayTimeActivity.this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                int monthOfYear, int dayOfMonth) {
                            // Display Selected date in textbox
                            txtDate.setText(dayOfMonth + "-"
                                    + (monthOfYear + 1) + "-" + year);

                        }
                    }, mYear, mMonth, mDay);
                dpd.show();
            }
        });


    }

}

Solution 4

You can try this code.. it will surely helpful to u.. No Doubt!

    protected Dialog onCreateDialog(int id) {

    Calendar c=Calendar.getInstance();
    int Sysday=c.get(Calendar.DAY_OF_MONTH);
    int Sysmonth=c.get(Calendar.MONTH);
    int Sysyear=c.get(Calendar.YEAR);

    int Sysmin=c.get(Calendar.MINUTE);
    int Syshour=c.get(Calendar.HOUR);


    switch (id) {
    case TIME_DIALOG:

        return new TimePickerDialog(this, myTimePicker , Syshour, Sysmin, false);

    case DATE_DIALOG:
        return new DatePickerDialog(this, myDatePicker, Sysyear, Sysmonth, Sysday);
    }
    return null;
}

Solution 5

Try this code it is definately work:

            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                    new DatePickerDialog.OnDateSetListener() {

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

                            birth_Date.setText(dayOfMonth + "-"
                                    + (monthOfYear + 1) + "-" + year);
                        }
                    }, mYear, mMonth, mDay);
            dpd.show();
Share:
58,050
lemoncodes
Author by

lemoncodes

Updated on December 11, 2020

Comments

  • lemoncodes
    lemoncodes over 3 years

    I have this code here on the options menu

    Dialog dialog = new Dialog(ScheduleActivity.this);
    dialog.setTitle("Add Event");
    dialog.setContentView(R.layout.add_even_on);
    Button datePicker = (Button) dialog.findViewById(R.id.datePicker);
    final DialogFragment dateFrag = new MyDatePicker();
        datePicker.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    dateFrag.show(getSupportFragmentManager(), "datePicker");               
                }
            });
    dialog.show();
    

    when, say "Add Event" on the option menu is clicked, a Dialog appears, with a button that shows a DatePickerDialog and beside it is a TextView that reflects the date chosen in the DatePickerDialog, here is the class I got from Androids Developer on how to use the DatePickerDialog.

    class MyDatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
     int pYear;
     int pDay;
     int pMonth;
    
    @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        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);
    
        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
      }
    
      public void onDateSet(DatePicker view, int year, int month, int day) {
        pYear = year;
        pDay = day;
        pMonth = month;
      }
    }
    

    So my problem is that how do i get the Value in milleseconds when i click "Set" on the DatePickerDialog which in turn automatically closes it, and returns to my Dialog which contains the button which open the DatePickerDialog and a Textview which reflects watever date is chosen the DatePickerDialog... i wont show the one i chose inside the DatePickerDialog...

    Here is a picture of what i mean,

    So when i click on the Pick Date Button a DatePickerDialog box appears as seen on the next picture enter image description here

    enter image description here

    and when i click set, i wanted to take into account the value in millseconds from that DatePickerDialog

  • lemoncodes
    lemoncodes almost 12 years
    this is is closet to what i did, tough i made some modifications and i didn't completely used all your methods, but its close enough to mine.. tnx
  • Shirish Herwade
    Shirish Herwade about 11 years
    showDialog(DATE_DIALOG_ID); is deprecated. Use dialog fragment instead stackoverflow.com/questions/11220820/… stackoverflow.com/questions/12710092/…
  • J Blaz
    J Blaz over 8 years
    What if the next time someone clicks the button to get the fragment to show up I want the date to be the last date selected by the user?
  • Hpsaturn
    Hpsaturn over 8 years
    you send params or initial params with newInstance squema: developer.android.com/reference/android/app/DialogFragment.h‌​tml