Run libgdx project through clicking a native Android button?

775 views Asked by At

My group and I are trying to build an Android app for our Hackathon. Right now they're building an app in native Android while I'm building an app using libGDX that launches through the DesktopLauncher. (App is Java based)

I know I can change where the libGDX game launches from by changing the gradle scripts (so I can have the app launch from Android instead of Desktop later on).

Is there a way to integrate my libGDX game into the native Android app my teammates are building? Example: I want my game to pop up and start run when a button in my teammate's part of the application (that's completely built in native Android) is clicked.

Sorry if this stupid question, I'm relatively new to Android development and the libGDX library (followed a tutorial for Android and made one project using libGDX).

3

There are 3 answers

0
Tenfour04 On

The DesktopLauncher in a LibGDX project is an Android Activity, so it can be launched like any other Activity.

If your team's app can be a separate app from yours, this is super easy. Install their app and yours on the same phone, and their app just has to launch an Intent for your app. For example, in their button's listener:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.yourgame");
if (intent != null) { 
    startActivity(intent);
} else {
    showGameNotInstalledMessage();
}

If your game needs to be part of the same application as theirs, then you need to share a project. LibGDX projects are structured a bit differently than a default Android project. It would be easiest if you create the project using the LibGDX Setup app, and they build the native part within the Desktop module of the LibGDX project. They can change the manifest to move the <action android:name="android.intent.action.MAIN" /> to their own main activity instead of the DesktopLauncher.

0
Savehdarbandsari On

Any class that implements ApplicationListener is an actual view in Android and you can set your view to these classes inside any activity.

package com.mygdx.game;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;

public class GameLauncher extends AndroidApplication implements ApplicationListener {
SpriteBatch batch;
Texture img;
InputProcessor inputProcessor;

public void onCreate (Bundle bundle) {
    super.onCreate(bundle);
    View view = initializeForView(this);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.addView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    setContentView(layout);
}
public void onPause () {
    super.onPause();
}

@Override
public void onDestroy () {
    super.onDestroy();
    Log.w("WindowedTest", "destroying");
}


@Override
public void create() {
    batch = new SpriteBatch();
    img = new Texture("badlogic.jpg");

    inputProcessor = new InputProcessor() {
        @Override
        public boolean keyDown(int keycode) {
            return false;
        }

        @Override
        public boolean keyUp(int keycode) {
            return false;
        }

        @Override
        public boolean keyTyped(char character) {
            return false;
        }

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            exit();
            return false;
        }

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            return false;
        }

        @Override
        public boolean touchDragged(int screenX, int screenY, int pointer) {
            return false;
        }

        @Override
        public boolean mouseMoved(int screenX, int screenY) {
            return false;
        }

        @Override
        public boolean scrolled(float amountX, float amountY) {
            return false;
        }
    };
    Gdx.input.setInputProcessor(inputProcessor);
}

@Override
public void resize(int width, int height) {

}

@Override
public void render() {
    ScreenUtils.clear(1, 0, 0, 1);
    batch.begin();
    batch.draw(img, 0, 0);
    batch.end();
}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void dispose() {
    batch.dispose();
    img.dispose();
}}
 

now you can call Intent from any class who extends Activity or FragmantActivity or ... I believe that you should use libgdx Starter version 1.11.0 I mean newest version

0
ayvazj On

I ran into issues because the libgdx examples were not updated to work with the latest Android SDK AppCompatActivity.

You might take a look at this example: https://github.com/SiliconLabs/thunderboard-android

Specially

DemoMotionGdxAdapter loads the models and renders the scene (this is where you game will reside) https://github.com/SiliconLabs/thunderboard-android/blob/master/app/src/main/java/com/silabs/thunderboard/demos/ui/DemoMotionGdxAdapter.java

GdxDemoActivity is updated to work with AppCompatActivity as its base class. https://github.com/SiliconLabs/thunderboard-android/blob/master/app/src/main/java/com/silabs/thunderboard/demos/ui/GdxDemoActivity.java

DemoMotionActivity includes examples of how to interact with the game (button click triggers events in game) https://github.com/SiliconLabs/thunderboard-android/blob/master/app/src/main/java/com/silabs/thunderboard/demos/ui/DemoMotionActivity.java#L234

HTH

Don't forget to include the jars from https://github.com/SiliconLabs/thunderboard-android/tree/master/app/libs in your project.