I have a custom view in my layout and I also have a TextView in the layout as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_alignParentTop="true" android:layout_centerInParent="true"
android:text="Tiny" android:orientation="vertical">
<my.package.drawView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView android:text="Application Name" android:id="@+id/appName"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="30px" android:textStyle="italic" />
</LinerLayout>
I set the reference to the TextView in the onCreate method of the activity and in the onDraw method of my custom view, I change the text of the TextView as follows:
public void onDraw(Canvas canvas) {
tv.setText("player score: "+score);
}
But setting the text in the ondraw method causes it to go in an infinite loop.
Could someone please tell me how do I update the text of the TextView in or after the onDraw method of the custom view is called.
When you change a visual thing in the current view i.e. textview in this case. The entire visible region is redrawn and as part of that my.package.drawView is told to draw itself and hence the infinite loop.
Changing the visual properties in onDraw should be avoided and the design is incorrect.
Maybe you need some sort of timer to update the score or somewhere else that increases the score when something happens in the game?