12

In my app I have one datepicker, I am able to select and set the selected date in textview, but the issue is if again I click on textview to open datepicker dialog, it always shows current date instead of last selected date..so what is the issue?

  public class MainActivity extends Activity {

private TextView date_dropdown;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    date_dropdown=(TextView)findViewById(R.id.shows_dt);
    
    
    Calendar calendar = Calendar.getInstance();

    date_dropdown.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-"
            + (calendar.get(Calendar.MONTH) + 1) + "-"
            + calendar.get(Calendar.YEAR));

    date_dropdown.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            showDatePickerDialog();
        }
    });
}

public void showDatePickerDialog() {

    System.out.println("show date picke dilg ");
    System.out.println("show date picke dilg");

    DialogFragment newFragment1 = new SelectDateFragment();
    newFragment1.show(getFragmentManager(), "DatePicker");
}

public class SelectDateFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        populateSetDate(yy, mm + 1, dd);
    }

    public void populateSetDate(int year, int month, int day) {
        date_dropdown.setText(day + "-" + month + "-" + year);
    }

}
secantsquared
  • 19
  • 1
  • 5
Aditya
  • 1,508
  • 1
  • 19
  • 37

7 Answers7

2

Below Line You Have Given get only Current date, so You will Get current date only.

 date_dropdown.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-"
        + (calendar.get(Calendar.MONTH) + 1) + "-"
        + calendar.get(Calendar.YEAR));

save your Selected date in Shared Preference then Show in your TextView

Android Dev
  • 421
  • 8
  • 26
2

You should define calender instance in onCreate() method and then all year month and date value update onDateSet(), So when you again open date picker dialog it will surly display previously selected date.

Do just like this

  public class MainActivity extends Activity {

private TextView date_dropdown;

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


       final Calendar calendar = Calendar.getInstance();
        int yy1 = calendar.get(Calendar.YEAR);
        int mm1 = calendar.get(Calendar.MONTH);
        int dd1 = calendar.get(Calendar.DAY_OF_MONTH);

    date_dropdown=(TextView)findViewById(R.id.shows_dt);


    Calendar calendar = Calendar.getInstance();

    date_dropdown.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-"
            + (calendar.get(Calendar.MONTH) + 1) + "-"
            + calendar.get(Calendar.YEAR));

    date_dropdown.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            showDatePickerDialog();
        }
    });
}

public void showDatePickerDialog() {

    System.out.println("show date picke dilg ");
    System.out.println("show date picke dilg");

    DialogFragment newFragment1 = new SelectDateFragment();
    newFragment1.show(getFragmentManager(), "DatePicker");
}

public class SelectDateFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        return new DatePickerDialog(getActivity(), this, yy1, mm1, dd1);
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        populateSetDate(yy, mm + 1, dd);

         yy1 = yy ;
         mm1 = mm;
         dd1 = dd ;
    }

    public void populateSetDate(int year, int month, int day) {
        date_dropdown.setText(day + "-" + month + "-" + year);
    }

}
0

It looks like you're creating a new dialog every time you click, which means onCreateDialog is setting the date to the current time again. You want to have a class variable DialogFragment df, and in your showDatePickerDialog do something like this:

public void showDatePickerDialog() {
    if (df == null){
        // Only create a new fragment if one does not exist
        DialogFragment df = new SelectDateFragment();
    }
    df.show(getFragmentManager(), "DatePicker");
}

You will also want to store the value the user selects in your onPause method and set the DialogFragment to that value in onResume, in case your user navigates away from your app while inputting information.

TBridges42
  • 1,849
  • 1
  • 19
  • 29
  • public void showDatePickerDialog() { DialogFragment df = null ; if (df == null){ // Only create a new fragment if one does not exist df = new SelectDateFragment(); } df.show(getFragmentManager(), "DatePicker"); /*DialogFragment newFragment1 = new SelectDateFragment(); newFragment1.show(getFragmentManager(), "DatePicker");*/ } – Aditya Jun 04 '15 at 13:39
  • i did some thing like that..but issue is still same..first i open datepicker it shows current date.then i change it to 2nd june..and close datepicker dialog..it sets 2nd june in textview..till here its fine..but again i click and open dialog it still shows current date – Aditya Jun 04 '15 at 13:41
  • In the code in your comment you assign the dp to null right before the if statement. That if statement will always be true and you will always create a new df. Remove that line, and declare your df outside the method. – TBridges42 Jun 04 '15 at 16:34
0

How to disable future date picker in android

 mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());


ed_date.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                final DatePickerDialog   mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
                {
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
                    {
                              ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                            int month_k=selectedmonth+1;

                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");
                // TODO Hide Future Date Here
                mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

                // TODO Hide Past Date Here
                //  mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
                mDatePicker.show();
            }
        }); 
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
0
 mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());


ed_date.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                final DatePickerDialog   mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
                {
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
                    {
                              ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                            int month_k=selectedmonth+1;

                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");
                // TODO Hide Future Date Here
                mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

                // TODO Hide Past Date Here
                //  mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
                mDatePicker.show();
            }
        }); 
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
0

updateDate through set previous selected date in date picker

mDatePicker.updateDate(1994, 6, 12);


mDatePicker.setTitle("Please select date");
mDatePicker.updateDate(1994, 6, 12);
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
0

A simple way to print selected date is:

picker.addOnPositiveButtonClickListener {
    tv_date.text = "picker.headerText"
}

picker is an object of DatePicker class.

Ryan M
  • 18,333
  • 31
  • 67
  • 74