4

I'm doing an Android application and I have to show a DatePickerDialog. The fact is that the application will be on a device with a smart screen and the calendar doesn't fit in it. I would like the calendar to be smaller while keeping the same proportions.

Look like this

I tried to use the ScaleY and ScaleX functions on the DatePicker to resize it but it just resizes all the DatePickers.

Like this

Here's the code I use to show the DatePickerDialog

public void ShowDate() {
    final Calendar c = Calendar.getInstance();


    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dateForm = new DatePickerDialog(ViewForm.this, R.style.ColorOne, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

        }
    }, year, month, day);
    dateForm.show();

    DatePicker dp = dateForm.getDatePicker();

    dp.setScaleY(Float.parseFloat("0.5"));
    dp.setScaleX(Float.parseFloat("0.5"));
}

I hope someone will know how to do this

Thank you

tung
  • 719
  • 2
  • 14
  • 30
maloromano
  • 51
  • 1
  • 5
  • Add `R.style.ColorOne` in question . – ADM Apr 06 '18 at 09:38
  • An alternative solution would be to use a `DatePicker` and creating your own Dialog. Maybe, you have more possibilities in the `DatePicker` class rather than the `DatePickerDialog` class. But i'm not sure. – M_I Apr 06 '18 at 09:50
  • I don't think because I'm getting the getting the DatePicker without the dialog with this line `DatePicker dp = dateForm.getDatePicker();` and I don't see any functions that could do what I want to do – maloromano Apr 06 '18 at 09:52

3 Answers3

3

you can change width and height DatePickerDialog programmatically, you just use WindowManager.LayoutParams params = dateForm.getWindow().getAttributes(); see this example:

        final Calendar c = Calendar.getInstance();

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dateForm = new DatePickerDialog(this, R.style.AppTheme, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

            }
        }, year, month, day);

        DatePicker dp = dateForm.getDatePicker();

        WindowManager.LayoutParams params = dateForm.getWindow().getAttributes();

        params.gravity = Gravity.TOP;
//        params.x = 0; // locationByX;
//        params.y = 0; // locationByY;
        params.width = 600; // dialogWidth;
//        params.height = 1000; // dialogHeight;

        dateForm.getWindow().setAttributes(params);

        dateForm.show();

//        dp.setScaleY(Float.parseFloat("0.7"));
//        dp.setScaleX(Float.parseFloat("0.7"));
Taher Fattahi
  • 951
  • 1
  • 6
  • 14
0

Try this

dateForm.getWindow().setLayout(width,height)

width & height specific as device resolution size maintain dimension file or use SDP Library for that.

or for that specific resolution use AlertDialog.THEME_HOLO_LIGHT as below :

DatePickerDialog dateForm = new DatePickerDialog(ViewForm.this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {}
Sameer Jani
  • 1,192
  • 9
  • 16
  • The dialog is already as bis as possible... so it's juste taking the "ok" button out of the screen. The best would be to reduce the calendar's size – maloromano Apr 06 '18 at 10:43
  • Okay, then first line of code would be reduce the size of dialog but you need to maintain dimension file for variant device resolution wise. – Sameer Jani Apr 06 '18 at 10:48
0

Following code can be used:

  if (datePickerDialog.getWindow() != null) {
        datePickerDialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, 
        WindowManager.LayoutParams.WRAP_CONTENT);
        datePickerDialog.getWindow().setGravity(Gravity.CENTER);
  }

Instead of LayoutParams.WRAP_CONTENT, You can set custom width and height as well.

Rehan
  • 39
  • 9