21

Can anybody tell me how to customize the date picker's width and height in android?

Illidanek
  • 996
  • 1
  • 18
  • 32
mohan
  • 13,035
  • 29
  • 108
  • 178

3 Answers3

48

The only way I've found to resize it is to simply re-scale the view. For example, if you only want 80% of the normal size, I would add these attributes:

android:scaleX="0.8"
android:scaleY="0.8"
H.Muster
  • 9,297
  • 1
  • 35
  • 46
Don
  • 659
  • 5
  • 6
  • 1
    A workaround: http://stackoverflow.com/questions/3259720/resize-android-datepicker-control – gh. Sep 22 '12 at 01:15
23

For DatePicker and TimePicker:

  1. android:scaleX="0.60" and android:scaleY="0.60"
    Above code will set the visible part of the Date and Time Picker to something like 60% of the total space and

  2. android:layout_marginLeft="-50dp", android:layout_marginTop="-30dp", android:layout_marginRight="-50dp", android:layout_marginBottom="-30dp"
    Above code will remove extra spaces around this picker

    padding="-40dp" doesn't worked for me so I have used margin.

    <DatePicker
          android:id="@+id/date_picker"
          android:calendarViewShown="false"
          android:layout_height="105dp"
          android:layout_width="wrap_content"
          android:scaleX="0.60"
          android:scaleY="0.60"
          android:layout_marginLeft="-50dp"
          android:layout_marginTop="-30dp"
          android:layout_marginRight="-50dp"
          android:layout_marginBottom="-30dp" />
    
    <TimePicker
          android:id="@+id/time_picker"
          android:layout_width="wrap_content"
          android:layout_height="105dp"
          android:scaleX="0.60"
          android:scaleY="0.60"
          android:layout_marginLeft="-55dp"
          android:layout_marginTop="-30dp"
          android:layout_marginRight="-50dp"
          android:layout_marginBottom="-30dp" />
    

Pratik Saluja
  • 373
  • 3
  • 11
  • 3
    please, provide some information beyond pure code snippet. – Crypt32 Dec 30 '15 at 06:07
  • 3
    Thanks! After trying tons of other ways the negative margins worked for me! – blueprintchris Apr 05 '16 at 15:55
  • 2
    OH MY GOD HOW HAVE PEOPLE NOT SEEN THIS THANK YOU – HaydenKai Jun 02 '16 at 12:43
  • How did you know how to calculate these margins? –  Jul 20 '16 at 16:56
  • @Andrew When you add android:scaleX="0.60" and android:scaleY="0.60" , it will reduce the size of picker to 60% of actual size, So remaining 40% will look like empty white border around the picker. So I am using margin to hide that white border. – Pratik Saluja Jul 21 '16 at 09:47
  • I understand that, but I don't understand from where did you get those negative margins? How did you know it should be -50dp, -30dp? –  Jul 21 '16 at 09:49
  • @Andrew Try to run this code again and again after altering negative margins. Also try to run this code after removing margins, you will know the difference. I am using margins according to my full screen layout, to show the example here. So, it depends upon which layout and design you are targeting to. – Pratik Saluja Jul 21 '16 at 11:29
4

In your layout's XML file, change the android:layout_height and android:layout_width attributes. I am assuming you are currently using either fill_parent or wrap_content for the values of these attributes, however you can be more specific - defining the exact or scaled pixel value.

For example, android:layout_width="15dp" will set the width at a scalable value of 15. You can change 15 to whatever you want. If you want to set a specific pixel size, you can use, for example, 15px.

Phil
  • 35,852
  • 23
  • 123
  • 164