-1

Following Button is part of a fragment

butndob.setOnClickListener(new View.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                    DatePickerDialog.OnDateSetListener mDateSetListen =  new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                            int dayOfMonth) {
                        dob.setText(dayOfMonth + "/"+monthOfYear+"/"+year);  
               //dob is edittext on which date picked will be displayed         
                    }
                };

I think the above code is incomplete to dislay the dialog to pick a date, what should be done to make the DatePickerDialog to appear on screen.

I don't want to use a DatePicker in my layout I have used a butndob whose click will display a DatePickerDialog .

Spring Breaker
  • 8,233
  • 3
  • 36
  • 60
Deepti
  • 119
  • 4
  • 12
  • Please have a look at here [Android Date Picker Example](http://www.mkyong.com/android/android-date-picker-example/) –  Apr 29 '14 at 05:58
  • @user3110424 i am not using datepicker in layout its simply a button and on its click even a date picker dialog should be shown – Deepti Apr 29 '14 at 06:13
  • If you have gone through the above link you can see how he has done it. You can see the full example. And about `showDialog` method. Its an android method when you called `showDialog` actually `onCreateDialog` method is being called and rest you can see in the link if you have any problem you can ask here. –  Apr 29 '14 at 06:17
  • @Deepti Check [this](http://www.javabeat.net/datepickerdialog-android/) it may help u – W I Z A R D Apr 29 '14 at 06:59

2 Answers2

0

Use

   static final int ID_DATEPICKER = 0;

      @Override
           public void onClick(View v) {
            // TODO Auto-generated method stub

            showDialog(ID_DATEPICKER);
           }
Santosh Kathait
  • 1,444
  • 1
  • 11
  • 22
0

Deepti use this code

dateBtn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            new DatePickerDialog(CurrentClass.this, d, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();


        }
    });

DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {

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

        monthOfYear = monthOfYear+1;

        String date="";
        String month="";
        if(dayOfMonth<10)
        {
            date="0"+dayOfMonth;
        }
        else{

            date=""+dayOfMonth;
        }

        if(monthOfYear<10)
        {
            month="0"+monthOfYear;

        }
        else
        {
            month=""+monthOfYear;
        }


        String  DateNow= ""+date+"/"+month+"/"+year;

            dob.setText(DateNow);


    }
};
Rohit Goswami
  • 617
  • 5
  • 17
  • this code i felt helpfull but there is an exception .. i have added the code above .. plz check if you can. – Deepti Apr 29 '14 at 07:09