0

I have an Activity called GameLoop (class which extends Activity). But instead of using an xml file for the layout, i'm using a class for it, since I want to be able to draw to a canvas. (I've seen on some tutorials this is what you're supposed to do)

The layout class is called GameLoopLayout.

I have a game loop running in the layout class, I can render bitmaps to the screen and control the FPS, everything is fine. But now I want to add a button to it, but since i'm not using an xml layout file, I don't know how to do it. Can anyone help me out please?

What I'm doing atm:

GameLoopLayout:

Button button;
Canvas canvas;
SurfaceHolder surfaceHolder;

public GameLoopActivityLayout(Context context) {
    //all necessary initializations here...
    button = new Button(context);
    button.setEnabled(true);
    button.setLeft(10);
    button.setTop(20);
}

//render function called during game loop
private void render() {
    if (!surfaceHolder.getSurface().isValid())
        return;
    canvas = surfaceHolder.lockCanvas();
    //draw all game objects to canvas...
    button.draw(canvas);
    surfaceHolder.unlockCanvasAndPost(canvas);
}

2 Answers 2

5

try to below code :

LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout2);

// add button
Button b = new Button(this);
b.setText("Button added dynamically!");
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
b.setId(MY_BUTTON);
b.setOnClickListener(this);
ll.addView(b);
Sign up to request clarification or add additional context in comments.

2 Comments

Care to explain how this works? What is the linearLayout2, and where do i get it? same question for id My_BUTTON
@Tirafesi private static final int MY_BUTTON = 100; add this in your code.
1

Creating custom Views does not prevent you from having XML layouts. For example:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.yourpackage.GameLoopLayout
        android:id="@+id/game_loop_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

</FrameLayout>

You can use margins and android:layout_gravity to place the button wherever you like.

In code, get access to your GameLoopLayout like you would any other view:

// inside of onCreate()
mGameLoopLayout = (GameLoopLayout) findViewById(R.id.game_loop_layout);
// do setup with GameLoopLayout

3 Comments

Ok, somehow I've made it work, even though I'm quite lost tbh. Could you explain to me what's being done here, please? Here's the onCreate method: setContentView(R.layout.game_loop); layout = (GameLoopLayout) findViewById(R.id.game_loop_layout);
@Tirafesi What are you confused about? This is the standard way to do UI on android. You pass an XML layout tosetContentView(), then you look up the view by the ID that you set in the XML. That's all that's happening.
I've finally understood it! I thought that my custom view was the same thing as a layout, and that's why i had to setContentView(myCustomLayout). But now i've realized that i am simply creating my own view object, which then i put inside the xml file, like if it was a button, or textView etc. thanks for all the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.