0

I'm making a game and currently I have this java file

    package pap.crowslanding;

    import android.os.Bundle;

    public class Game extends MainActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tester1);

}

}

Using my custom layout GameView, I have tried to merge it with my xml file tester1

<RelativeLayout 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"
android:background="@drawable/cl_bg"
android:gravity="fill_horizontal">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="42dp"
    android:text="@string/hello_world" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="31dp"
android:orientation="horizontal" >

<Button
    android:id="@+id/play_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/custom_button"
    android:text="@string/first_button"
    android:textColor="@drawable/text_color_white" />

<Button
    android:id="@+id/sbutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"      
    android:layout_weight="1"
    android:background="@drawable/custom_button"
    android:text="@string/menu_settings"
    android:textColor="@drawable/text_color_white" />
</LinearLayout>

</RelativeLayout>

<pap.crowslanding.GameView 
 android:id="@+id/GameView" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"></pap.crowslanding.GameView
>

 </RelativeLayout>

Now:

setContentView(R.layout.tester1);

Will not work for some reason but

setContentView(GameView(this));

Works, any help please

Sorry if this seems easy, I'm quite new and still getting my head around it. Thank you for reading.

Ryanas
  • 1,757
  • 3
  • 19
  • 36
  • Possible duplicate http://stackoverflow.com/questions/15980024/how-to-add-button-into-a-canvas-drawview-android/15980200 – SimonSays Apr 15 '13 at 22:32
  • I want to merge my GameView layout with an xml layout or even add a button. I have looked at the question and it does not relate to me. – Ryanas Apr 15 '13 at 23:14

2 Answers2

0

You cannot draw a Button yourself. You need to wrap your custom View into an XML layout and add the Button there. See here for an example. You can do the same programmatically, if you really want to do it in the code, but you still gonna need to wrap it in a layout.

Community
  • 1
  • 1
SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • As I said, unfortunately I'm not really well versed in android programming just yet. All I wish to do is display a button, any button, when I run my Game.java file. I'm not too sure how I would merge GameView.java with an xml file containing a button – Ryanas Apr 15 '13 at 23:34
0

You could create a layout that contains your GameView and a Button, and use it as your content view.

Create a file main.xml in res/layout like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button Text" />

    <pap.crowslanding.GameView
        android:id="@+id/game_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

Then, in onCreate in your Activity, tell it to use the layout as the content view. You can then grab a reference to your GameView and the Button, adding click listeners or whatnot.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GameView gameView = (GameView) findViewById(R.id.game_view);
    Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            // respond to clicks
        }
    });
}

Edit: Make sure your GameView class implements all three constructors from its super:

public GameView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO
}

public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO
}

public GameView(Context context) {
    super(context);
    // TODO
}
Jschools
  • 2,698
  • 1
  • 17
  • 18
  • Whenever I do this, it just crashes and will not display it for me. However, if I do setContentView(new GameView(this)); it seems to work – Ryanas Apr 16 '13 at 00:07
  • Can you paste the stack trace of the crash? – Jschools Apr 16 '13 at 00:23
  • Ah, I sent you the logcat, is that ok? – Ryanas Apr 16 '13 at 00:38
  • Ah, you're getting a NoSuchMethodException. Your GameView needs to implement all three of the constructors from its super class, or else the layout inflater can't instantiate it. `public GameView(Context context, AttributeSet attrs, int defStyle)`, `public GameView(Context context, AttributeSet attrs)`, and `public GameView(Context context)` – Jschools Apr 16 '13 at 00:48
  • Ah yes! It works but unfortunately now, the buttons show but I have a blank grey/white screen where the GameView should be – Ryanas Apr 16 '13 at 01:02
  • It might be because you're setting both the height and width of the view to "wrap_content". Try setting them to "match_parent" instead, so that it fills the view. (Also, don't forget to upvote and/or mark your accepted answer!) – Jschools Apr 16 '13 at 01:24
  • Odd, when I do this, I can see the button and when I remove the button, I cannot see the GameView (or what its functions are meant to do)...I just see a black screen now. Is it possible to send you my app package? I cannot upvote nor chat with such a low rep – Ryanas Apr 16 '13 at 01:39