1

I have a date in my button (5-12-2031) when i click the button i want to show date picker with date what i have in my button. In Stackoverflow i found to set the time pickerAndroid: Setting time in time picker with the time shown in text view i tried the same but the date picker not shown.

My code is:

Calendar mDateCalender = Calendar.getInstance();
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_details_main_layout);


    date.setOnClickListener(new onClickListener) {
    public void onClick(){

    new DatePickerDialog(AddDetailsActivity.this, onDateListener,
                        mDateCalender.get(Calendar.YEAR), mDateCalender.get(Calendar.MONTH),
                        mDateCalender.get(Calendar.DAY_OF_MONTH)).show();
    }
    }
}


 DatePickerDialog.OnDateSetListener onDateListener = new OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

        String dateFormat = mOfflineDatas.get(0).getOnsket_utfort();
        String[] getDates = dateFormat.split("-");

        Toast.makeText(getApplicationContext(), "" + dateFormat, Toast.LENGTH_LONG).show();

        mDateCalender.set(Calendar.DAY_OF_MONTH, Integer.valueOf(getDates[0]));
        mDateCalender.set(Calendar.MONTH, Integer.valueOf(getDates[1]));
        mDateCalender.set(Calendar.YEAR, Integer.valueOf(getDates[2]));

    }
};

my dateFromat = 5-12-2013 it splits correctly.

What change i need to change in this anybody suggest some ideas..

Community
  • 1
  • 1
Vini
  • 967
  • 1
  • 15
  • 33
  • You're providing a date in the `DatePickerDialog` constructor call. If you want to provide some other date, just change whatever you're passing to the constructor. – laalto Dec 05 '13 at 10:53
  • @laalto please provide some example or reference... – Vini Dec 05 '13 at 11:07

4 Answers4

2

Try this..

Global :

static final int DATE_DIALOG_ID = 999;

private int myear;
private int mmonth;
private int mday;

OnCreate :

    final Calendar c = Calendar.getInstance();
    myear = c.get(Calendar.YEAR);
    mmonth = c.get(Calendar.MONTH);
    mday = c.get(Calendar.DAY_OF_MONTH);

       btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            String[] str = btn.getText().toString().trim().split("-");

            mday = Integer.parseInt(str[0]);
            mmonth = Integer.parseInt(str[1]);
            myear = Integer.parseInt(str[2]);

                showDialog(DATE_DIALOG_ID);

            }

        });

Method outside OnCreate() :

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            DatePickerDialog _date =   new DatePickerDialog(this, datePickerListener, myear,mmonth,
                    mday){

            };
            return _date;
        }
        return null;
    }

EDIT 1:

date.setOnClickListener(new onClickListener) {
public void onClick(){


            String[] str = date.getText().toString().trim().split("-");

            mday = Integer.parseInt(str[0]);
            mmonth = Integer.parseInt(str[1]);
            myear = Integer.parseInt(str[2]);
new DatePickerDialog(AddDetailsActivity.this, onDateListener,
                    myear, mmonth,
                    mday).show();
}
}
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • Thank you for your answer @Gopal provide this one same like yours i will try that one.... – Vini Dec 05 '13 at 11:28
1

this may help you...

yourButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        showDatePickerDialog(yourButton.getText().toString());
    }
});

private void showDatePickerDialog(String date) {
    // here date is 5-12-2013
    String[] split = date.split("-");
    int day = Integer.valueOf(split[0]);
    int month = Integer.valueOf(split[1]);
    int year = Integer.valueOf(split[2]);
    OnDateSetListener dateSetListener = new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            // TODO Auto-generated method stub

        }
    };

    DatePickerDialog datePickerDialog = new DatePickerDialog(this,
            dateSetListener, year, month, day);
    datePickerDialog.show();
}
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • @Ela sure. you are parsing Date String from text of Button and assigning date to Calender in onDateSetListner. Listener will be called after dialog is opened and user has selected date. prior, Calender will have default date. so by the time, dialog is shown, Calender has default date as Listener will be called later... first get the date and pass it to the Constructor of DatePickerDialog... – Gopal Gopi Dec 05 '13 at 12:48
0

There's the DatePicker.init where you can set it...

Or to set the date you can use the calendar control as shown in the android developer sample. See Steps 4-6.

Here's another sample blog post on this.

or you can use following on button click event

Calendar cal=Calendar.getInstance(Locale.ENGLISH);
dp.init(cal.getTime().getYear()+1900, cal.getTime().getMonth(), cal.getTime().getDay(), this);
Sanket Shah
  • 4,352
  • 3
  • 21
  • 41
0

If Your Using Fragments this code may help you .

mLesson_Date = (TextView) rootView.findViewById(R.id.Lesson_Date);

--------------------------------------------------------------------
mLesson_DateBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // showDialog(DATE_DIALOG_ID);
                DialogFragment newFragment1 = new DatePickerFragment(
                        mLesson_Date);// Here you are passing the TextView Object to DatePickerFragment by calling the constructor 
                newFragment1.show(getFragmentManager(), "datePicker");
            }
        });
--------------------------------------------------------------------

public static class DatePickerFragment extends DialogFragment implements
            DatePickerDialog.OnDateSetListener {

        public EditText editText;
        DatePicker dpResult;
        TextView sample;

        public DatePickerFragment() {
            // TODO Auto-generated constructor stub.
        }

        public DatePickerFragment(TextView setTimeView) {
            // TODO Auto-generated constructor stub.
            this.sample = setTimeView;
        }

        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);
            // return new DatePickerDialog(getActivity(),
            // (EditSessionActivity)getActivity(), year, month, day);

            // 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) {

            sample.setText(String.valueOf(day) + "/"
                    + String.valueOf(month + 1) + "/" + String.valueOf(year));
            // set selected date into datepicker also

        }
    }
Nagaraja
  • 581
  • 1
  • 4
  • 12
  • That's nice but I think the author was looking how to pre-populate the DatePicker with a specified date. This example doesn't do that and just records the selected date in a TextView (it just pre-populates the date in the DatePicker with the *current* date and not some other specified date). – clamum Apr 22 '17 at 05:53