0

I have a image if i click on it then, date picker is displaying from that. If i select date and done then date will be displayed.

Its working fine but System.out.println("Current Date2 is greater"); Toast.makeText(getApplicationContext(), "Invalid Date", Toast.LENGTH_SHORT).show(); in console getting data 2 times. Its repeating 2 times.

    public class LoginActivity extends ActionBarActivity implements OnClickListener  {

     private Calendar calendar;
     private int day;
     private int month;
     private int year;

    private ImageView dateOfBirthImage;

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

            dateOfBirthImage = (ImageView) findViewById(R.id.dobImage);
            calendar = Calendar.getInstance();
            day = calendar.get(Calendar.DAY_OF_MONTH);
            month = calendar.get(Calendar.MONTH) + 1;
            year = calendar.get(Calendar.YEAR);

            dateOfBirthImage.setOnClickListener(this);

    }


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



     @Override
       protected Dialog onCreateDialog(int id) {
       // TODO Auto-generated method stub

           //  return new DatePickerDialog(this, datePickerListener, year, month, day);
             if (id == 999) {
                 return new DatePickerDialog(this, datePickerListener, year, month, day);
               }
              return null;
       }


     private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

      public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {

          SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
          try {

              String currentDate = ""+day + "/" + month  + "/" + year ;           
              String selectedDate = ""+selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear ;

             java.util.Date date1 = sdf.parse(currentDate);
             java.util.Date date2 = sdf.parse(selectedDate);          

             System.out.println("Current compare " + currentDate + "sasd" + selectedDate );

             if (date1.compareTo(date2) < 0 ) {

                 System.out.println("Current Date2 is greater");
                 Toast.makeText(getApplicationContext(), "Invalid Date", Toast.LENGTH_SHORT).show();

                }
                  else {

                }


        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


      }
     };
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • possible duplicate of [Jelly Bean DatePickerDialog --- is there a way to cancel?](http://stackoverflow.com/questions/11444238/jelly-bean-datepickerdialog-is-there-a-way-to-cancel) – Cheok Yan Cheng Jan 21 '15 at 09:42
  • declare int noOfTimesCalled = 0; in DatePickerDialog and onDateSet add if( noOfTimesCalled%2 == 0) {your code} noOfTimesCalled++; – Pavya Jan 21 '15 at 09:42

1 Answers1

0

Check the DatePicker view using isShown method

public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
if(view.isShown()){
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      try {

          String currentDate = ""+day + "/" + month  + "/" + year ;           
          String selectedDate = ""+selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear ;

         java.util.Date date1 = sdf.parse(currentDate);
         java.util.Date date2 = sdf.parse(selectedDate);          

         System.out.println("Current compare " + currentDate + "sasd" + selectedDate );

         if (date1.compareTo(date2) < 0 ) {

             System.out.println("Current Date2 is greater");
             Toast.makeText(getApplicationContext(), "Invalid Date", Toast.LENGTH_SHORT).show();

            }
              else {

            }


    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

   }
  }
Bruce
  • 8,609
  • 8
  • 54
  • 83