Joschi February 2016

creating buttons in main activity while drawing in custom surfaceView

In my MainActivity i want to handle my Buttons and in my GameSurfaceView i want to handle drawing stuff, but I can't see the buttons on my GameSurfaceView why is that? My Gameloop Class handles onDraw calls. When i set my ContentView to my MainActivity, I don't see my drawings and when i set the ContentView to my GameSurfaceView, i see the drawings but i can't see my buttons.

How do I implement the GameSufaceView to my MainActivity? Please help me. Here is the short form of my Code:

This is my MainActivity [edit: i shortened the 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 is my 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 is the xml File from the 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_        

Answers


Joschi February 2016

I finally solved my problem.

I need to define the Button bevore the gameSurfaceView, in the xml file, otherwise the gameSurfaceView is "over" the button and that's the reason I can't see it.

Post Status

Asked in February 2016
Viewed 2,411 times
Voted 13
Answered 1 times

Search




Leave an answer