Reputation: 69
I have an activity and a fragment. Where fragment will be loaded dynamically in activity. Fragment has around 20 views and most of them are custom views. For loading the fragment its taking around 3 seconds, this time is just for loading the layout, without any initialization or functionality. I feel 3 seconds is more and want to reduce it to 1 second. What could be the possible ways to achieve this. Things I did: ** Removed weights and nested layouts. ** Removed unused attributes of view ** Flattened the layout hierarchy. Layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gauge="http://schemas.android.com/apk/res/com.test.app.testApp"
xmlns:customview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_light_grey" >
<RelativeLayout
android:id="@+id/top_components_dummy"
android:layout_width="match_parent"
android:layout_height="135dp"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/ dummy_layout"
android:layout_width="370dp"
android:layout_toLeftOf="@+id/host_layout"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/dummy_parent"
android:layout_width="280dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/water_and_drain_tactile"
android:orientation="horizontal" >
<FrameLayout
android:id="@+id/dummy_parent_water"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="40dp"
android:layout_marginRight="20dp" >
<com.test.app.ui.components.CustomView
android:id="@+id/level_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:src="@drawable/xyz" />
<ImageView
android:layout_width="85dp"
android:layout_height="4dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:background="@drawable/empty_indiactor"
android:contentDescription="@string/empty_string"
android:visibility="gone" />
</FrameLayout>
<FrameLayout
android:id="@+id/dummy_parent_drain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="40dp" >
<com.test.app.ui.components.CustomView
android:id="@+id/drain_level_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="45dp"
android:src="@drawable/xyz" />
<ImageView
android:layout_width="85dp"
android:layout_height="4dp"
android:layout_gravity="top|center"
android:layout_marginTop="20dp"
android:background="@drawable/empty_indiactor"
android:contentDescription="@string/empty_string"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
<TextView
android:id="@+id/Waterdummy_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="@+id/dummyCenter"
android:text="Water"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />
<View
android:id="@+id/dummyCenter"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#00ffffff" />
<TextView
android:id="@+id/Draindummy_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/dummyCenter"
android:text="Drain"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/host_layout"
android:layout_width="400dp"
android:layout_centerHorizontal="true"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/host_Stop_Background"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/xyz"
android:contentDescription="@string/empty_string" />
<ImageView
android:id="@+id/dull_stop_icon"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_above="@+id/host_stop_text"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:contentDescription="@string/empty_string"
android:scaleType="fitXY"
android:src="@drawable/xyz" />
<TextView
android:id="@+id/host_stop_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:text="STOP"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000" />
<RelativeLayout
android:id="@+id/host_Rate_Layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/host_Stop_Background" >
<ImageView
android:id="@+id/host_Rate_Icon"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/xyz"
android:contentDescription="@string/empty_string" />
<TextView
android:id="@+id/host_rate_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/host_rate_unit_textView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="-5dp"
android:gravity="bottom|center_horizontal"
android:text="1.5"
android:textAlignment="gravity"
android:textColor="#000000"
android:textSize="34sp" />
</RelativeLayout>
<ImageView
android:id="@+id/host_system_Icon"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_toRightOf="@+id/host_Stop_Background"
android:background="@drawable/host_system_top_tactile"
android:contentDescription="@string/empty_string"
android:paddingBottom="18dp"
android:paddingTop="18dp"
android:scaleType="fitCenter"
android:src="@drawable/xyz" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/dull_rate_layout"
android:layout_width="350dp"
android:layout_toRightOf="@+id/host_layout"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/dull_rate_layout"
android:layout_width="175dp"
android:layout_height="match_parent" >
<TextView
android:id="@+id/dull_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/dull_rate"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="130dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/xyz" />
<TextView
android:id="@+id/dull_rate_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/dull_rate_unit_textView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="-5dp"
android:gravity="bottom|center_horizontal"
android:text="500"
android:textAlignment="gravity"
android:textColor="#000000"
android:textSize="36sp" />
<TextView
android:id="@+id/dull_rate_unit_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:text="mL/min"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/salad_layout"
android:layout_width="175dp"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/dull_rate_layout" >
<TextView
android:id="@+id/salad_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/salad"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />
<ImageView
android:id="@+id/salad_Icon"
android:layout_width="130dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/dull_salad_tactile"
android:contentDescription="@string/empty_string"
android:paddingBottom="18dp"
android:paddingTop="18dp"
android:scaleType="fitCenter"
android:src="@drawable/xyz" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/midLinear"
android:layout_width="match_parent"
android:layout_height="460dp"
android:layout_below="@+id/top_components_dummy" >
<TextView
android:id="@+id/boomSpeed_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="@string/boom_speed"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="@+id/boomSpeed_Value_Text"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_below="@+id/boomSpeed_textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="-5dp"
android:gravity="center"
android:maxLength="3"
android:text="300"
android:textColor="@android:color/black"
android:textSize="50sp"
android:textStyle="normal" />
<ImageView
android:id="@+id/imageView_down"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignBottom="@+id/boomSpeed_Value_Text"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/boomSpeed_Value_Text"
android:scaleType="fitXY"
android:src="@drawable/xyz" />
<ImageView
android:id="@+id/imageView_up"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignBottom="@+id/boomSpeed_Value_Text"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/boomSpeed_Value_Text"
android:src="@drawable/xyz" />
<TextView
android:id="@+id/boom_Speed_Value_Unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/boomSpeed_Value_Text"
android:layout_centerHorizontal="true"
android:layout_marginTop="-10dp"
android:text="mL/min"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#444444" />
<ImageView
android:id="@+id/IV_boomSpeed"
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_below="@+id/boom_Speed_Value_Unit"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:hapticFeedbackEnabled="true"
android:src="@drawable/xyz" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:layout_toLeftOf="@+id/IV_boomSpeed"
android:gravity="center_vertical" >
<ImageView
android:layout_width="380dip"
android:layout_height="380dip"
android:layout_centerInParent="true"
android:src="@drawable/xyz" />
<com.test.app.ui.components.CustomView1
android:id="@+id/circularprogressbar2"
android:layout_width="345dip"
android:layout_height="345dip"
android:layout_centerInParent="true"
customview:cpb_backgroundColor="#EDEDED"
customview:cpb_borderColor="#22B7B3"
customview:cpb_maintitle="@string/time_remaining"
customview:cpb_progressColor="#22B7B3" />
<RelativeLayout
android:id="@+id/clickablearea"
android:layout_width="180dip"
android:layout_height="80dip"
android:layout_alignTop="@+id/circularprogressbar2"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dip"
android:background="#00fff000" >
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:layout_toRightOf="@+id/IV_boomSpeed"
android:gravity="center_vertical" >
<ImageView
android:layout_width="380dip"
android:layout_height="380dip"
android:layout_centerInParent="true"
android:src="@drawable/dialsbg" />
<ImageView
android:id="@+id/IV_StopML"
android:layout_width="120dip"
android:layout_height="80dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="54dp"
android:src="@drawable/xyz" />
<TextView
android:layout_width="100dip"
android:layout_height="80dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="40dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:text="STOP\n ML"
android:textColor="@android:color/holo_red_dark"
android:textStyle="bold" />
<com.test.app.ui.components.CustomView1
android:id="@+id/circularprogressbar3"
android:layout_width="345dip"
android:layout_height="345dip"
android:layout_centerInParent="true"
customview:cpb_borderColor="#E7AB0F"
customview:cpb_progressColor="#e2a000"
customview:backgroundColor="#EDEDED"
customview:goal="@string/goal"
customview:goal_unit="mL"
customview:goal_value="2800"
customview:ml_unit="mL"
customview:rate="@string/rate"
customview:rate_unit="mL/hr"
customview:rate_value="700"
customview:removed_title="@string/removed"
customview:removed_value="1500" />
<RelativeLayout
android:id="@+id/editMLGoal_clickArea"
android:layout_width="100dip"
android:layout_height="50dip"
android:layout_alignStart="@+id/circularprogressbar3"
android:layout_marginLeft="60dip"
android:layout_marginTop="210dip"
android:background="#00fff000" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/editMLRate_clickArea"
android:layout_width="100dip"
android:layout_height="50dip"
android:layout_alignEnd="@+id/circularprogressbar3"
android:layout_marginRight="60dip"
android:layout_marginTop="210dip"
android:background="#00fff000" >
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/RL_DialView2"
android:layout_width="300dp"
android:layout_height="190dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginBottom="-40dp" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="170dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/xyz" />
<com.test.app.ui.components.CustomView2
android:id="@+id/gauge_view2"
android:layout_width="match_parent"
android:layout_height="match_parent"
gauge:isFloat="false"
gauge:scaleEndValue="400"
gauge:scaleMaxLimit="350"
gauge:scaleMinLimit="150"
gauge:scaleStartValue="70"
gauge:showLimitText="true"
gauge:showNeedle="true"
gauge:showRanges="true"
gauge:showScale="false"
gauge:showText="true"
gauge:textTitle="@string/ven"
gauge:textUnit="mmHg"
gauge:textUnitColor="#000000"
gauge:textValueColor="#000000" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/RL_DialView1"
android:layout_width="220dp"
android:layout_height="140dp"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginBottom="-20dp"
android:layout_toLeftOf="@+id/RL_DialView2" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="120dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@drawable/xyz" />
<com.test.app.ui.components.CustomView2
android:id="@+id/gauge_view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
gauge:isFloat="false"
gauge:scaleEndValue="500"
gauge:scaleMaxLimit="400"
gauge:scaleMinLimit="-200"
gauge:scaleStartValue="-300"
gauge:showLimitText="true"
gauge:showNeedle="true"
gauge:showRanges="true"
gauge:showScale="false"
gauge:showText="true"
gauge:textTitle="@string/art"
gauge:textUnit="mmHg"
gauge:textUnitColor="#000000"
gauge:textValueColor="#000000" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/RL_DialView4"
android:layout_width="220dp"
android:layout_height="140dp"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginBottom="-20dp"
android:layout_toRightOf="@+id/RL_DialView2" >
<ImageView
android:id="@+id/imageView4"
android:layout_width="120dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@drawable/xyz" />
<com.test.app.ui.components.CustomView2
android:id="@+id/gauge_view4"
android:layout_width="match_parent"
android:layout_height="match_parent"
gauge:isFloat="true"
gauge:scaleEndValue="40"
gauge:scaleMaxLimit="38"
gauge:scaleMinLimit="36"
gauge:scaleStartValue="34"
gauge:showLimitText="true"
gauge:showNeedle="true"
gauge:showRanges="true"
gauge:showScale="false"
gauge:showText="true"
gauge:textTitle="@string/dt"
gauge:textUnit="\u2103"
gauge:textUnitColor="#000000"
gauge:textValueColor="#000000" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/RL_DialView3"
android:layout_width="220dp"
android:layout_height="140dp"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginBottom="-20dp"
android:layout_toLeftOf="@+id/RL_DialView1" >
<ImageView
android:id="@+id/imageView3"
android:layout_width="120dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@drawable/xyz" />
<com.test.app.ui.components.CustomView2
android:id="@+id/gauge_view3"
android:layout_width="match_parent"
android:layout_height="match_parent"
gauge:isFloat="false"
gauge:scaleEndValue="320"
gauge:scaleMaxLimit="300"
gauge:scaleMinLimit="90"
gauge:scaleStartValue="70"
gauge:showLimitText="true"
gauge:showNeedle="true"
gauge:showRanges="true"
gauge:showScale="false"
gauge:showText="true"
gauge:textTitle="@string/tmp"
gauge:textUnit="mmHg"
gauge:textUnitColor="#000000"
gauge:textValueColor="#000000" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/RL_DialView5"
android:layout_width="220dp"
android:layout_height="140dp"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginBottom="-20dp"
android:layout_toRightOf="@+id/RL_DialView4" >
<ImageView
android:id="@+id/imageView5"
android:layout_width="120dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@drawable/xyz" />
<com.test.app.ui.components.CustomView2
android:id="@+id/gauge_view5"
android:layout_width="match_parent"
android:layout_height="match_parent"
gauge:isFloat="true"
gauge:scaleEndValue="17"
gauge:scaleMaxLimit="15.9"
gauge:scaleMinLimit="13.1"
gauge:scaleStartValue="12"
gauge:showInnerRim="false"
gauge:showNeedle="true"
gauge:showRanges="true"
gauge:showScale="false"
gauge:showText="true"
gauge:textTitle="@string/cond"
gauge:textUnit="mS/cm"
gauge:textUnitColor="#000000"
gauge:textValueColor="#000000" />
</RelativeLayout>
Upvotes: 1
Views: 1915
Reputation: 8210
You can check the Layout Optimizating
guide:
ViewGroup
. You should make your Layout
as low-level hierarchy as possibleUsing nested instances of LinearLayout can lead to an excessively deep view hierarchy. Furthermore, nesting several instances of LinearLayout that use the layout_weight parameter can be especially expensive as each child needs to be measured twice. This is particularly important when the layout is inflated repeatedly, such as when used in a ListView or GridView.
It is always good practice to run the lint tool on your layout files to search for possible view hierarchy optimizations.
Using ViewStub
:
Sometimes your layout might require complex views that are rarely used. Whether they are item details, progress indicators, or undo messages, you can reduce memory usage and speed up rendering by loading the views only when they are needed
Upvotes: 1
Reputation: 3434
Have you heard from CustomViews? You can lay out the child views directly. So it don't need calculate again and again and again. For example:
<RelativeLayout
android:id="@+id/RL_DialView2"
android:layout_width="300dp"
android:layout_height="190dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginBottom="-40dp" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="170dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/xyz" />
<com.test.app.ui.components.CustomView2
android:id="@+id/gauge_view2"
android:layout_width="match_parent"
android:layout_height="match_parent"
gauge:isFloat="false"
gauge:scaleEndValue="400"
gauge:scaleMaxLimit="350"
gauge:scaleMinLimit="150"
gauge:scaleStartValue="70"
gauge:showLimitText="true"
gauge:showNeedle="true"
gauge:showRanges="true"
gauge:showScale="false"
gauge:showText="true"
gauge:textTitle="@string/ven"
gauge:textUnit="mmHg"
gauge:textUnitColor="#000000"
gauge:textValueColor="#000000" />
</RelativeLayout>
This is 5 times in your Layout.
Change the RelativeLayout to a ImageWithCustomView2Layout
which extends from ViewGroup
.
Then override onMeasure(int widthMeasureSpec, int heightMeasureSpec)
and onLayout(boolean changed, int left, int top, int right, int bottom)
.
However. When you use CustomViews which know exactly the position (and size) of its Childs it will be much faster.
Learn more about CustomViews at: DAC or GitHub Examples (1), (2)
Our can also a look at this post at medium.
Upvotes: 0
Reputation: 25595
Using RelativeLayouts are expensive, as laying out the views is a 'complex' task - each time you inflate the xml, the inflater must calculate the relations between the views, which is rather expensive.
You should try to use as much LinearLayouts as you can. This will boost things up a bit. LinearLayouts are much more faster to inflates, as the relations are at most 2 nodes per view.
Upvotes: 0