Relationship Between Custom SurfaceViews and Standard Android Views

My problem is communication between custom GameView (extends SurfaceView) and TextView: I want to set TextView text from GameView. I mainly use this layout file, it should explain the structure of my application:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#00ff00" > <TextView android:id="@+id/scoreTV" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="score: 0" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="back" android:layout_alignParentRight="true" /> </RelativeLayout> <org.gk.grApp.GameView android:id="@+id/gameplayScreen" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> 

I cannot change the text of the TextView in my GameView object because it is impossible to touch the UI stream from another. The handler doesn’t work either, because I can’t give a link to the GameView handler that is executed after loading this xml file (read about the default constructor for xml files, for example, here How can I use GLSurfaceView in LinearLayout together with other views, such as TextView and a Button? ). Do you have any idea what to do now? Maybe my conclusion is wrong, so please tell me about it.

EDIT: I changed my xml file, instead of GameView I now have:

 <LinearLayout android:id="@+id/gameplayScreen" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout> 

I also added an argument (third) to the constructor signature:

 public GameView(Context context, AttributeSet as, Handler h) { ... } 

and changed my onCreate in GameplayActivity:

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gameplay); LinearLayout ll = (LinearLayout)findViewById(R.id.gameplayScreen); GV = new GameView(this, null, scoreHandler); ll.addView(GV); } 

It works, now I can set the TextView text, but after clicking on the "Back" button, another exception is thrown: "Pausing an activity that does not resume: {org.gk.grApp / org.gk.grApp.MainMenuActivity}". I just started searching for information about this.

+4
source share
1 answer

First make a link to the TextView at the activity level:

TextView txv;

In onCreate, assign this link:

txv = (TextView) findViewById (R.id.MyTextView);

Then create a method in your activity after onCreate as follows:

 public void setTextView(final String txt){ MyActivity.this.runOnUiThread(new Runnable() { public void run() { txv.setText(txt); } }); } 

Then you call your custom view:

((MyActivity) getContext ()). setTextView (str);

+5
source

Source: https://habr.com/ru/post/1388684/


All Articles