Change TextView without completely re-drawing layout?
Posted
by twk
on Stack Overflow
See other posts from Stack Overflow
or by twk
Published on 2010-05-25T19:13:55Z
Indexed on
2010/05/25
21:41 UTC
Read the original article
Hit count: 725
I've found that updating a text view every second in my app burns a lot of CPU. The textview is in a horizontal LinearLayout, which is in turn inside of a vertical LinearLayout. Switching to a RelativeLayout (as recommended to increase perf) is not an option right now (I tried to get that working originally, but it was too complicated).
The horizontal LinearLayout has 3 elements. The outer ones are TextViews with a layout_weight of 0, and the middle one is a progress bar with a layout_weight of 1 to make it expand to take up most of the space. I'm changing the contents of the leftmost TextView every second
So, is there a way to change the contents of the text view without re-drawing everything? Or, can I force the TextViews to use a fixed amount of space to simplify the layout. Other tips for speeding up a LinearLayout are greatly appreciated as well.
For reference, here is my entire layout. The field I'm updating is the timeIn one.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="Artist Name"
android:id="@+id/curArtist"
android:textSize="8pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="5dp"></TextView>
<TextView
android:text="Song Name"
android:id="@+id/curSong"
android:textSize="10pt"
android:textStyle="bold"
android:layout_below="@id/curArtist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"></TextView>
<TextView
android:text="Album Name"
android:id="@+id/curAlbum"
android:textSize="8pt"
android:layout_below="@id/curSong"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"></TextView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/curAlbum"
android:orientation="vertical">
<LinearLayout
android:id="@+id/seekWrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="10dp"
android:maxHeight="10dp"
android:orientation="horizontal">
<TextView
android:text="0:00"
android:id="@+id/timeIn"
android:textSize="4pt"
android:paddingLeft="10dp"
android:gravity="center_vertical"
android:layout_weight="0"
android:layout_gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"></TextView>
<ProgressBar
android:layout_below="@id/curAlbum"
android:id="@+id/progressBar"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:maxHeight="10dp"
android:minHeight="10dp"
android:indeterminate="false"
android:layout_weight="1"
android:layout_gravity="center_horizontal|center_vertical"
style="?android:attr/progressBarStyleHorizontal"></ProgressBar>
<TextView
android:text="0:00"
android:id="@+id/timeLeft"
android:paddingRight="10dp"
android:textSize="4pt"
android:layout_gravity="right|center_vertical"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="fill_parent"></TextView>
</LinearLayout>
<ImageView
android:id="@+id/albumArt"
android:layout_weight="1"
android:padding="5dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:src="@drawable/blank_album_art"></ImageView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:src="@drawable/button_prev"
android:paddingLeft="10dp"
android:background="@null"></ImageButton>
<ImageButton
android:id="@+id/playPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/button_play"
android:layout_weight="1"
android:background="@null"></ImageButton>
<ImageButton
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_next"
android:layout_gravity="right"
android:paddingRight="10dp"
android:background="@null"></ImageButton>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
© Stack Overflow or respective owner