java - creating buttons in main activity while drawing in custom surfaceView -
in mainactivity want handle buttons , in gamesurfaceview want handle drawing stuff, can't see buttons on gamesurfaceview why that? gameloop class handles ondraw calls. when set contentview mainactivity, don't see drawings , when set contentview gamesurfaceview, see drawings can't see buttons.
how implement gamesufaceview mainactivity? please me. here short form of code:
this mainactivity [edit: shortened extract]
public class mainactivity extends appcompatactivity { private button button1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button1 = (button) findviewbyid(r.id.btn1); button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { } }); setcontentview(new gamesurfaceview(this)); } }
this custom surfaceview
public class gamesurfaceview extends surfaceview implements surfaceholder.callback{ private context context; public gamesurfaceview(context context){ super(context); getholder().addcallback(this); this.context = context; setfocusable(true); } @override public void ondraw(canvas canvas){ canvas.drawrect(60, 60, 100, 100, p); }
and here xml file mainactivity
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" android:screenorientation="landscape" android:orientation="horizontal" > <button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn1" android:layout_gravity="center_vertical" /> </linearlayout>
[edit] after day of searching found problem similiar mine: [how use surfaceview main.xml on android?
but can still not see buttons defined on mainactivity, see black screen , rectangle drawing:
i added mainactivity gamesurfaceview = (gamesurfaceview) findviewbyid(r.id.gamesurfaceview);
, deleted setcontentview(new gamesurfaceview(this));
added xml file code:
<com.example.joschi.tryout.gamesurfaceview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/gamesurfaceview" />
i still not able see button defined, why this? can me?
i solved problem.
i need define button bevore gamesurfaceview, in xml file, otherwise gamesurfaceview "over" button , that's reason can't see it.
Comments
Post a Comment