The interface of your app needs to render quickly, and interaction, such as scrolling through a list, for example, should run smoothly. In particular, older or low-end devices often have a hard time to do these things right. An unresponsive or slow UI can be the result, which is often caused by something that is called overdraw.
Overdraw is the phenomenon of a pixel on a view being drawn more than once. A colored background with a view on top of that has another background color is an example of overdraw (the pixel is drawn twice), but that's not really an issue. Too much overdraw, however, will have an impact on your app's performance.
You will need to have a real device and you need to complete the The Bad
app from the previous recipe to demonstrate overdraw issues, but you can examine any other app as well if you like.
Your device contains a couple of interesting developer options. One of them is the Debug GPU overdraw option which can be obtained by following next steps:
The Bad
app from the previous recipe, shown as follows:activity_main.xml
layout file. Remove the background
property from the list view, since it is not being used anyway. Also, remove the background property from the RelativeLayout
file, as I do not like orange any way, at least not for apps.background
property from the main_text_genre
, main_text_director
, and the main_text_actors
text views. Also, remove the background
property from their parent view, which is the last TableRow
appearing within TableLayout
.FrameLayout
at the root to RelativeLayout
. Get rid of TableLayout
and position the text views relatively:<?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:background="@android:color/holo_green_light" android:padding="8dp" android:layout_height="match_parent"> <ImageView android:id="@+id/main_image"android:src="@android:drawable/ic_media_play"android:layout_marginTop="8dp"android:layout_width="80dp"android:scaleType="fitCenter"android:layout_height="60dp" /> <TextView android:layout_width="match_parent"android:id="@+id/main_text_title"android:layout_marginTop="8dp"android:layout_toRightOf="@+id/main_image"android:background="@android:color/holo_purple"android:textSize="24sp"android:layout_height="wrap_content"android:textColor="@android:color/white"android:text="Line 1"/> <TextView android:layout_width="match_parent"android:id="@+id/main_text_year"android:layout_height="wrap_content"android:layout_toRightOf="@+id/main_image"android:layout_below="@+id/main_text_title"android:background= "@android:color/holo_blue_light"android:textSize="20sp"android:layout_marginTop="8dp"android:textColor="@android:color/white"android:text="Line 2"/> <TextView android:layout_width="match_parent"android:id="@+id/main_text_genre"android:layout_height="wrap_content"android:layout_toRightOf="@+id/main_image"android:layout_below="@+id/main_text_year"android:textSize="16sp"android:layout_marginTop="8dp"android:textColor="@android:color/white"android:text="Sub 1"/> <TextView android:layout_width="match_parent"android:id="@+id/main_text_director"android:layout_height="wrap_content"android:layout_toRightOf="@+id/main_image"android:layout_below="@+id/main_text_genre"android:textSize="16sp"android:layout_marginTop="8dp"android:textColor="@android:color/white"android:text="Sub 2"/> <TextView android:layout_width="match_parent"android:id="@+id/main_text_actors"android:layout_height="wrap_content"android:layout_toRightOf="@+id/main_image"android:layout_below="@+id/main_text_director"android:textSize="16sp"android:layout_marginTop="8dp"android:textColor="@android:color/white"android:text="Sub 3"/> </RelativeLayout>
text
properties. They were only there to check whether we were doing the right thing using the layout_toRightOf
and layout_below
properties.In this recipe, we have further improved our bad app by optimizing its layout. Also, it is no longer ugly. Actually, it has become quite good.
Using RelativeLayout
is more effective than LinearLayout
but unfortunately it is not so developer friendly if, for example, you want to move or remove a text view that another view is referring to using a relative property.
The FrameLayout
is much less complex, but it does not have this problem, and it seems to perform as well as RelativeLayout
.
On the other hand it is not intented to contain many child widgets. Please be aware that in the end what counts is the smallest number of nested layout views, so you should pick the container that suits your needs and performs best.
Awesome! Our app runs smoothly on all devices. We do not expect any weird errors any more.
Now let's ship it to our beta users to find out what they think of it. We will find out once we have completed the final chapter, where we will discuss adhoc distribution.
There are more interesting tools that you perhaps would like to examine in order to improve the quality and performance of your app.
We have mentioned Espresso
before. Robotium
is another Android test automation framework for UI testing purposes. You can find it at http://robotium.com.