66428

How to handle date picker dialog not to set in edittext when clicked outside of dialog android?

Question:

When I click edittext, date picker dialog opens. without clicking done button in date picker and clicked outside of dialog, it sets in edittext.

I <a href="https://stackoverflow.com/questions/11444238/jelly-bean-datepickerdialog-is-there-a-way-to-cancel/12737234#12737234" rel="nofollow">checked this</a> but this works for first time. When selecting second time by choosing dialog and without clicking done button, it fails.

private final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { private boolean fired; public void resetFired(){ fired = false; } @Override public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { if (fired) { Log.e("DatePicker", "Ignoring for first time"); birthDayValue = selectedDay; birthMonthValue = selectedMonth; birthYearValue = selectedYear; String birthDayDateFormt = Utils.getBirthDayDate(selectedYear, selectedMonth, selectedDay); dateOfBirth.setText(birthDayDateFormt); dateOfBirth.setError(null); return;//ignore and return. } fired = true;//first time fired } }; };

How to solve this issue?

Answer1:

@Override public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { if(view.isShown()) { if (fired) { Log.e("DatePicker", "Ignoring for first time"); birthDayValue = selectedDay; birthMonthValue = selectedMonth; birthYearValue = selectedYear; String birthDayDateFormt = Utils.getBirthDayDate(selectedYear, selectedMonth, selectedDay); dateOfBirth.setText(birthDayDateFormt); dateOfBirth.setError(null); return;//ignore and return. } fired = true;//first time fired } }

<strong>if(view.isShown())</strong> add code in this condition, its a simple step do it and check output

Recommend

  • intent.setClass(getActivity, .class) vs Intent(this, .class)
  • Android Fragment isn't accepting setcontentview
  • Android error in passing id of selected item in listview using ContextMenu
  • How can I force a DatePicker to be used for input instead of the keyboard on an HTML text input insi
  • My Date Picker is not Working
  • passing string values to other function from onclick date listner
  • DatePickerDialog is not displayed on click of the button
  • Using Multiple date picker in application
  • Refresh content of a div without ajax
  • How to resize datepicker text programmatically
  • App Crashes When TextField is Empty
  • IIS application pool recycling and “shutdown time limit” role in overlapping
  • Getting “The parameter app_id is required” with OAuth.io
  • How do I make a write operation in Realm (iOS) synchronous?
  • Symfony2 redirect to https route fails (uses wrong port)
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • Regarding starting the threads on a condition
  • npm 5.4.1 install/uninstall all failing
  • Implementing “partial void” in VB
  • How do I signal completion of my dataflow?
  • c# open webrowser in many tab
  • Instantiate interface in JAVA?
  • how to avoid repetitive constructor in children
  • System.InvalidCastException: Specified cast is not valid
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • How to delay loading a property with linq to sql external mapping?
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Can I display google adwords (AdView) in javafx on android
  • Uncaught Error: Could not find module `ember-load-initializers`
  • PHP - How to update data to MySQL when click a radio button
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • output of program is not same as passed argument
  • Running a C# exe file
  • Javascript + PHP Encryption with pidCrypt
  • Getting Messege Twice Using IMvxMessenger
  • Bitwise OR returns boolean when one of operands is nil
  • How can i traverse a binary tree from right to left in java?
  • How can I use threading to 'tick' a timer to be accessed by other threads?