10

May I know how to resize the CalendarView in Android? The CalendarView occupied more than half of the screen. I wish to make it smaller, perhaps 40% of the screen. Thanks.

Default CalendarView size in my smartphone, occupied more than half

CalendarView

My current XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ada.landing.MainActivity">

<CalendarView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/calendarView"
    android:firstDayOfWeek="2"/></RelativeLayout>

I tried another way to define CalendarView, but the overall height reduced but it does not shrink by ratio:

CalendarView

<CalendarView
            android:layout_width="500dp"
            android:layout_height="200dp"
            android:id="@+id/calendarView"
            android:firstDayOfWeek="2" />
Micho
  • 3,929
  • 13
  • 37
  • 40
CHAN HAU YEEN
  • 343
  • 2
  • 5
  • 18

7 Answers7

5

I think CalendarView is not (really) resizeable because Android adapts the size of this view to allow user to have a great experience using this view. I think the commun size is when use a DatePickerDialog. DatePickerDialog

CalendarView needs a minimum height and width.

If you really want to resize your calendarView, maybe try to resize(or scale) it programmatically with this :

float scalingFactor = 0.5f; // scale down to half the size view.setScaleX(scalingFactor); view.setScaleY(scalingFactor);

Cocorico
  • 1,998
  • 1
  • 22
  • 38
  • scalingFactor stretches date text as well that looks bad – Rajesh N May 19 '17 at 09:02
  • 1
    So... as I said, Android optimizes this widget for a better user experience. Maybe you can try to extend the CalendarView class, find the layout and customize it (like txtView.setTextSize() ). – Cocorico May 19 '17 at 12:31
1

There's a couple of different ways you can go about shrinking the size of the CalendarView. One is you can manually adjust the size of the view in the .XML file you're using the calendar in. Another way is by creating a new layout (Such as linear layout), adjust the layout to your desired size, and put the CalendarView in that layout.

  • 1. Manually adjust the size of the view in the .XML file: Is that mean I need to change the value of layout_height? I make the height with 200dp, It doesn't work. 2. I created new linear layout with width 500dp and height 200dp, put the calendarView in that layout. The outcome is similiar with the first method. I inserted my xml code in post. – CHAN HAU YEEN Sep 19 '16 at 02:02
1

You can try like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:weightSum="5"
              android:orientation="vertical">
    <LinearLayout android:layout_width="match_parent" android:layout_height="0dp"
                  android:layout_weight="2">
        <CalendarView
            android:id="@+id/calendarView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:firstDayOfWeek="2"/>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="0dp"
                  android:layout_weight="3"></LinearLayout>
</LinearLayout>
1

Hope this may help to reduce the size of your calendar view.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <CalendarView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/calendarView"
        android:firstDayOfWeek="2"/>
        </LinearLayout>
    <LinearLayout
        android:layout_weight="60"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="Hello World hello world "
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    </LinearLayout>

Now there is a library which allows you to set the dp size into sdp (Scalable Dp) so that it will help in many screen size

Gradle for that

compile 'com.intuit.sdp:sdp-android:1.0.3'

Code Example Same above example but it will support all possible screen size

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">


    <CalendarView
        android:layout_width="@dimen/_100sdp"
        android:layout_height="@dimen/_100sdp"
        android:id="@+id/calendarView"
        android:firstDayOfWeek="2"/>
        </LinearLayout>
    <LinearLayout
        android:layout_weight="60"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="Hello World hello world "
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    </LinearLayout>
Pratik Vyas
  • 644
  • 7
  • 20
1

if you want resize

 android:scaleY="1.1" -vertically
0

Here I put code it reduce to 40% width of calander view .try this.

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.4"
        android:orientation="horizontal"
        android:weightSum="1">

        <CalendarView
            android:id="@+id/calendarView"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="0.4"
            android:firstDayOfWeek="2" />


    </LinearLayout>
</LinearLayout>
Vasant
  • 3,475
  • 3
  • 19
  • 33
  • ratio is not maintained – Rajesh N May 19 '17 at 06:16
  • so, what you need?as per question he/she want size 40% of screen. – Vasant May 19 '17 at 06:18
  • I want rectangle calender with very less space occupied in height and width must be match_parent – Rajesh N May 19 '17 at 06:31
  • 1
    i got to knew it is not possible. it tries to adjust font to specific size but after it cuts calendar view text so,we can accomplish functionality by using dialog. here we need to make any view whatever size you want .on click of that view it display one dialog where full calendar will display.now we can select date and month ,year.then On click of dialog OK button.it is dismissed. – Vasant May 19 '17 at 07:11
  • @RajeshNasit Let CHAN HAU YEEN comment on the answer. As he has put up the question with bounty. And the requirement is upto him and not you I guess – Sreehari May 24 '17 at 09:51
  • @Stallion i have put bounty. he had justed asked question – Rajesh N May 24 '17 at 10:02
  • @RajeshNasit Oh my bad... You have all the wrights in the world to comment and suggest :) – Sreehari May 24 '17 at 10:04
0

I have tried above all answer but not of work what exactly i want. Then later i have user Material Calender View.

In that we can give tile width and tile height as per as our requirement. if we want rectangle calender view with much less height compared to width, we can set tile width more then height. Thank you all those who answered.

Xantium
  • 11,201
  • 10
  • 62
  • 89
Rajesh N
  • 6,198
  • 2
  • 47
  • 58