17

This is driving me crazy. I have a fragment in my Android app which is laid out using a RelativeLayout. Problem is that for some reason it takes ages to render, about 5 seconds just to load about 4 elements on screen.

One of the elements is a CalendarView and when I remove it it goes back to proper speeds (ie: instant). I'd imagine the problem is got to do with the way I'm asking Android to lay it out, must not make much sense or is inefficient or something.

Here's the XML file:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp"> <TextView android:id="@+id/title" android:layout_width="0dp" android:layout_height="wrap_content" android:textStyle="bold" android:text="TODAY" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/time" /> <TextView android:id="@id/time" android:gravity="right" android:textStyle="bold" android:layout_width="100dp" android:layout_height="wrap_content" android:text="11:32" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentRight="true" /> <TextView android:id="@+id/date_info" android:layout_margin="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Wednesday, 15th August 2012" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_below="@id/title" android:layout_alignParentLeft="true" /> <CalendarView android:id="@+id/calendar_view" android:layout_width="250dp" android:layout_height="250dp" android:background="@drawable/white_back_black_border" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" /> </RelativeLayout> 

I've tried loads of different layout options but removing the Calendar seems to be the only thing that makes a difference.

Any insights would be greatly appreciated. Thanks

5
  • Are you doing anything in code with the calendarview? Like setting dates etc. Commented Dec 11, 2012 at 5:25
  • Even when I comment out all of the code that deals with it it's still slow. But as soon as I remove it from the layout file its fixed. I reckon it must be got to do with the RelativeLayout because if I remember correctly when I used a LinearLayout it worked fine... Commented Dec 11, 2012 at 18:36
  • Heh sure be that way. It just doesn't make any sense. My guess is that its something else. Commented Dec 11, 2012 at 18:44
  • I know it's strange. Just to make sure I switched it to a LinearLayout just there. Works fine, loads instantly. I suppose for the time being I can just live with LinearLayout but it's still annoying me. Commented Dec 11, 2012 at 20:25
  • This guy had the same problem: How to stop Garbage collection in Android 2.3.3 Commented Aug 5, 2013 at 10:38

6 Answers 6

6

I've had this problem too, both with relative layouts and linear layouts. It doesn't seem to be layout-related.

Here is an example to see the error, just with a simple layout, no code at all:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".FechaHoraActivity" > <TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" /> <CalendarView android:id="@+id/calendarView1" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> 

And now just change the linear layout height to:

android:layout_height="match_parent" 

With this change the problem is gone, at least in my Samsung Galaxy Tab 2

So it seems it is more "space" related, but I'm still not sure why this problem appears. And I haven't found any other interesting results on google searching "calendarview slow"...

EDIT Let's see if we can find an answer with a small bounty

Sign up to request clarification or add additional context in comments.

1 Comment

The CalendarView layout_height also needs to be match_parent.
5
+100

I take some hours to find the answer, but not figure out why. Here is something strange, may it can help you to find the answer.

the cpu consumption when CalendarView slow enter image description here

The getView in WeekAdapter has a param parent that was never used.

 public View getView(int position, View convertView, ViewGroup parent) { WeekView weekView = null; if (convertView != null) { weekView = (WeekView) convertView; } else { weekView = new WeekView(mContext); android.widget.AbsListView.LayoutParams params = new android.widget.AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); weekView.setLayoutParams(params); weekView.setClickable(true); weekView.setOnTouchListener(this); } int selectedWeekDay = (mSelectedWeek == position) ? mSelectedDate.get( Calendar.DAY_OF_WEEK) : -1; weekView.init(position, selectedWeekDay, mFocusedMonth); return weekView; } 

Looking forward the answer.

6 Comments

How do you view the cpu consumption? Is this in Eclipse?
Look at this, it has been integrated in DDMS.
interesting, we are closer now. I've used your approach to compare two layouts (see my answer). The first one (the one that hangs): wrap_content And the one than runs fine: match_parent As you can see the difference starts at number #4 with "obtainView". Inside that we can see: inside obtainView Yeah, WeekAdapter.getView is clearly the problem, you are right, now we have to find why.
we still don't know why this happens, but you did a great investigation and I don't want to leave the bounty without a winner so enjoy =)
Thks TheBronx, this mystery still open , anyone who slove this will be reward.
|
3

It seems like CalendarView directly inside a RelativeLayout really doesn't work that well. The following solution with the trick of using a FrameLayout is loading much faster in the emulator:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp" > <TextView android:id="@+id/title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/time" android:text="TODAY" android:textAppearance="?android:attr/textAppearanceLarge" android:textStyle="bold" /> <TextView android:id="@id/time" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:gravity="right" android:text="11:32" android:textAppearance="?android:attr/textAppearanceLarge" android:textStyle="bold" /> <TextView android:id="@+id/date_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/title" android:layout_margin="20dp" android:text="Wednesday, 15th August 2012" android:textAppearance="?android:attr/textAppearanceMedium" /> <FrameLayout android:layout_width="250dp" android:layout_height="250dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" > <CalendarView android:id="@+id/calendar_view" android:layout_width="250dp" android:layout_height="250dp" android:background="@drawable/white_back_black_border" /> </FrameLayout> </RelativeLayout> 

3 Comments

It is not the FrameLayout that solves the problem, it is the fixed height. Seems android needs the height to build the weeks smoothly...
I tested this again and used a RelativeLayout instead of a FrameLayout and it took +10sec to load the app. I changed the surrounding layout of the calendarView back to FrameLayout and it loaded instantly again. So I'm inclined to say it has definitely to do with the way RelativeLayout handles CalendarViews.
Yes true, dont no why relative layout is sucks in rendering cal view. use linearlayout... Thanks bro for help +1 from my side
3

Found that CalendarView widget needs height to be set to match_parent, then you can put it to any layout. For example in relative with height 200. This works fine on 4.0.3 emulator and on Samsung Galaxy Tab with 4.2.2.

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="200dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" > <CalendarView android:id="@+id/cv_dialog_filter_date" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/relativeLayout2" android:layout_centerHorizontal="true" android:text="05.12.2013" /> </RelativeLayout> 

Comments

2

Putting the CalendarView in a FrameLayout will solve the problem.. here is a XML code example:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#cc00b1cc"> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="102dp" android:layout_alignParentBottom="true"> <CalendarView android:layout_width="425dp" android:layout_height="374dp" android:id="@+id/calendarView" android:layout_gravity="left|top" /> </FrameLayout> </RelativeLayout> 

Comments

0

The above answers were right but, still hasn't solved one mystery.

That is, When i want to include calendarview inside layout with other layout. It's just disappears or i can see only Week letters. I tried the above match parent and everything and still i couldn't solve it.

If anyone struggling like me Here's the answer.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:descendantFocusability="blocksDescendants" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <CalendarView android:id="@+id/calendarService" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/linearLayout"></CalendarView> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical"> ///Your views /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.