2

I have an private App that works great till I upgraded my phone to Android 7.
The issue is I need picker to choose only month and year (with no day).
On the old Android versions, I had this one:
No month

but - after the upgrade, all I can choose is:
No working

Is there a way to set the picker in the app to be the old style, or any other idea to select only month and year?

I tried the theme style parameter on the constructor like this:

final Calendar c = Calendar.getInstance();
DatePickerDialog dpd = new DatePickerDialog(context, android.R.style.Theme_Holo_Dialog ,listener,
                        c.get(Calendar.YEAR),
                        c.get(Calendar.MONTH),
                        c.get(Calendar.DAY_OF_MONTH));

But it did not helped either, I am getting same results (different theme):

Black cal

Why does it change in the first place without me as developer setting it to the new picker?

You can see the clean code here:
GitHub DatePicker test

gabi
  • 1,003
  • 5
  • 12
  • 30

3 Answers3

1

Try this:-

DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
     AlertDialog.THEME_HOLO_LIGHT,this,year,month,day);

datepickerdialog.OnDateSetListener() {
   @Override
   public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
      // arg1 = year
      // arg2 = month
      // arg3 = day     
   }
};
cpt. Sparrow
  • 415
  • 8
  • 22
0

When creating DatePickerDialog, you can set the old style for it

import android.app.DatePickerDialog;
import android.content.res.Resources;
import android.widget.DatePicker;
import java.util.Calendar;

...
final Calendar c = Calendar.getInstance();
DatePickerDialog dpd = new DatePickerDialog(context, android.R.style.Theme_Holo_Dialog ,listener,
        c.get(Calendar.YEAR),
        c.get(Calendar.MONTH),
        c.get(Calendar.DAY_OF_MONTH));

dpd.show();
dpd.findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);

Here is the result when I test on Device Android 6 (API 23) enter image description here

Linh
  • 57,942
  • 23
  • 262
  • 279
  • Tried that, did not helped. Please see the question, I updated it with screenshot – gabi Oct 24 '16 at 10:59
  • @gabi I have updated my answer, please check it again – Linh Oct 24 '16 at 14:18
  • I am using Android 7 now.. do you think it is related? – gabi Oct 24 '16 at 16:52
  • so please update your question. in your question you say `I upgraded my phone to Android 6`. :(. also have you test this code in device android 7, make sure you use my code and my import – Linh Oct 25 '16 at 01:35
  • @Phan-Van-Lihn updated, I used your code in new project but still no help :( - see code here: https://github.com/gabik/AndroidDatePickerTest – gabi Oct 25 '16 at 05:18
0
private DatePickerDialog createDialogWithoutDateField() {
    DatePickerDialog onlyMonthdAndYearPicker = new DatePickerDialog(this, null, 2014, 1, 24);
    try {
        java.lang.reflect.Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
        for (java.lang.reflect.Field datePickerDialogField : datePickerDialogFields) {
            if (datePickerDialogField.getName().equals("mDatePicker")) {
                datePickerDialogField.setAccessible(true);
                DatePicker datePicker = (DatePicker) datePickerDialogField.get(onlyMonthdAndYearPicker);
                java.lang.reflect.Field[] datePickerFields = datePickerDialogField.getType().getDeclaredFields();
                for (java.lang.reflect.Field datePickerField : datePickerFields) {
                    Log.i("test", datePickerField.getName());
                    if ("mDaySpinner".equals(datePickerField.getName())) {
                        datePickerField.setAccessible(true);
                        Object dayPicker = datePickerField.get(datePicker);
                        ((View) dayPicker).setVisibility(View.GONE);
                    }
                }
            }
        }
    } 
    catch (Exception ex) {
    }
    return onlyMonthdAndYearPicker;
}

and Call this method

 createDialogWithoutDateField().show();
Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38