]> git.ktnx.net Git - mobile-ledger.git/commitdiff
quicker date selection on Android O
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Tue, 4 Dec 2018 21:19:02 +0000 (21:19 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Tue, 4 Dec 2018 21:19:02 +0000 (21:19 +0000)
app/src/main/java/net/ktnx/mobileledger/DatePickerFragment.java

index 7aa2db06b1bc1ae626ceb0fd3dae58fde65a3c8c..164371319e08e542c816d816a8d88ec9975ad150 100644 (file)
@@ -18,7 +18,7 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 public class DatePickerFragment extends AppCompatDialogFragment
-implements DatePickerDialog.OnDateSetListener
+implements DatePickerDialog.OnDateSetListener, DatePicker.OnDateChangedListener
 {
     @NonNull
     @Override
@@ -46,7 +46,15 @@ implements DatePickerDialog.OnDateSetListener
             }
         }
 
-        return new DatePickerDialog(Objects.requireNonNull(getActivity()), this, year, month, day);
+        DatePickerDialog dpd =  new DatePickerDialog(Objects.requireNonNull(getActivity()), this, year, month, day);
+
+        // quicker date selection available in API 26
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            DatePicker dp = dpd.getDatePicker();
+            dp.setOnDateChangedListener(this);
+        }
+
+        return dpd;
     }
 
     @TargetApi(Build.VERSION_CODES.O)
@@ -61,4 +69,19 @@ implements DatePickerDialog.OnDateSetListener
             date.setText(String.format(Locale.US, "%d/%d", month+1, day));
         }
     }
+
+    @Override
+    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
+        TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
+
+        final Calendar c = GregorianCalendar.getInstance();
+        if ( c.get(GregorianCalendar.YEAR) == year && c.get(GregorianCalendar.MONTH) == monthOfYear) {
+            date.setText(String.format(Locale.US, "%d", dayOfMonth));
+        }
+        else {
+            date.setText(String.format(Locale.US, "%d/%d", monthOfYear+1, dayOfMonth));
+        }
+
+        this.dismiss();
+    }
 }