0

I'm using two text views in a dialog, one is for from date and another is for to date. Now when i click on from date text view the date picker dialog opens and when i select the date it is not updated in the text view, if i open the date picker again and select the date for the second time the date is updated in the text view. can any one figure out why it is not updated the first time.

 public static void datePickerDialog(final Context context) {

            dialog = new Dialog(context);
            dialog.setContentView(R.layout.date_picker_dialog);

            fromDateText = dialog.findViewById(R.id.from_date);
            toDateText = dialog.findViewById(R.id.to_date);

            fromDateText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    datePicker(context);
                }
            });

            toDateText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    datePicker(context);
                }
            });
            dialog.show();

            fromDateText.setText(fromDate);
            toDateText.setText(toDate);
        }


        public static void datePicker(Context context){

            fromDatePicker = new DatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog_MinWidth
                    ,fromDateListner, fromDay, fromMonth, fromYear);
            simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
            fromDatePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            fromDatePicker.show();

            fromDateListner = new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    month+=1;
                    fromDate = dayOfMonth+"/"+month+"/"+year;
                    setDate();
                }
            };

        }

        private static void setDate() {

            try {
                dateFrom = simpleDateFormat.parse(fromDate);
                dateTo = simpleDateFormat.parse(toDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            fromDateText.setText(dateFrom.toString());
            toDateText.setText(dateTo.toString());
        }
Dhanasekar
  • 179
  • 12
  • is the same thing happening with the `dateTo` textview? – meditat Jul 06 '18 at 07:44
  • https://stackoverflow.com/questions/14933330/datepicker-how-to-popup-datepicker-when-click-on-edittext Try this – Ankita Jul 06 '18 at 07:50
  • Why are you setting text twice? You will get fromDate and toDate first time empty in datePickerdialog method –  Jul 06 '18 at 08:56
  • @meditat I havent implemented dateTo yet.. – Dhanasekar Jul 06 '18 at 10:25
  • @AndroidTeam I have set a default date, so there will not be any problem in that. – Dhanasekar Jul 06 '18 at 10:26
  • In `setDate()` comment out the lines: `dateTo = simpleDateFormat.parse(toDate);` and `toDateText.setText(dateTo.toString());` and run the project. –  Jul 06 '18 at 10:41

3 Answers3

0

Can you try like this?

    private static void setDate() {

        try {
            dateFrom = simpleDateFormat.parse(fromDate);
            dateTo = simpleDateFormat.parse(toDate);

            fromDateText.setText(dateFrom.toString());
            toDateText.setText(dateTo.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

// Edit:

Check my code:

private void showDatePicker() {
    final Calendar myCalendar = Calendar.getInstance();

    DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            setDate(myCalendar.getTime());
        }
    };

    if (getActivity() != null) {
        new DatePickerDialog(getActivity(), date, myCalendar
                .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH)).show();
    }
}

private void setDate(Date time) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.US);
    editText.setText(sdf.format(time));
}
MarcinR
  • 786
  • 1
  • 6
  • 16
0

Try this code .. take boolean variable for selected textview ...

change method like this way..

public class DialogActiivty extends AppCompatActivity {
private TextView fromDateText,toDateText;
private String fromDate,toDate;
private Dialog dialog;
private DatePickerDialog fromDatePicker;
private SimpleDateFormat simpleDateFormat;

private Calendar calendar;
private int year, month, day;
private boolean fromSelected=false,toSelected=true;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    datePickerDialog(DialogActiivty.this);
}
public  void datePickerDialog(final Context context) {
    calendar=Calendar.getInstance();
    year = calendar.get(Calendar.YEAR);

    month = calendar.get(Calendar.MONTH);
    day = calendar.get(Calendar.DAY_OF_MONTH);
    dialog = new Dialog(context);

    dialog.setContentView(R.layout.date_picker_dialog);

    fromDateText = dialog.findViewById(R.id.from_date);
    toDateText = dialog.findViewById(R.id.to_date);

    fromDateText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            toSelected=false;
            fromSelected=true;
            datePicker(context);
        }
    });

    toDateText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fromSelected=false;
            toSelected=true;
            datePicker(context);
        }
    });
    dialog.show();

  //        fromDateText.setText(fromDate);
  //        toDateText.setText(toDate);
}


public  void datePicker(Context context){

    fromDatePicker = new DatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog_MinWidth
            ,fromDateListner, year, month, day);
    simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
    fromDatePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    fromDatePicker.show();


}
DatePickerDialog.OnDateSetListener fromDateListner = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        month+=1;
        fromDate = dayOfMonth+"/"+month+"/"+year;
        setDate(fromDate);
    }
};
private  void setDate(String fromDate) {
    if (fromSelected) {
        fromDateText.setText(fromDate);
    }
    if (toSelected) {
        toDateText.setText(fromDate);
    }
}
}
0

fromDateListner is initialized after the dialog creation, so the first time the DatePickerDialog is created without listener.

Move the fromDateListner = new DatePickerDialog.OnDateSetListener() ... part before fromDatePicker = new DatePickerDialog(context ...

bwt
  • 17,292
  • 1
  • 42
  • 60