Quicklaunch Technology
Quicklaunch Technology

Reputation: 337

How to add libgdx as a sub view in android

I have main.xml as follows:

  <RelativeLayout>
     ...
     <FrameLayout
                    android:id="@+id/panel_sheet"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

        <com.libgdx.Sheet3DViewGdx 
                android:id="@+id/m3D"
                android:layout_width="1000dp"
                android:layout_height="600dp"
        />

    </FrameLayout>

...
</RelativeLayout>

And my main activity class is as follows:

public class Test extends Activity {

    MainActivity  m3DActivity;

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

My GDX class is as follows which extend ApplicationListener class rather than View.

public class Sheet3DViewGdx implements ApplicationListener{

    @Override
    public void create() {
        InputStream in = Gdx.files.internal("data/obj/Human3DModel.obj").read();
        model =  ObjLoader.loadObj(in);
    }

    @Override
    public void dispose() {
    }

    @Override
    public void pause() {
    }


    @Override
    public void render() {
        Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        model.render(GL10.GL_TRIANGLES);
    }

    @Override
    public void resize(int arg0, int arg1) {
        float aspectRatio = (float) arg0 / (float) arg1;
    }

    @Override
    public void resume() {
    }
}

Now, how should I add Sheet3DViewGdx as a subview in my main layout?

Upvotes: 26

Views: 14439

Answers (4)

Matthew
Matthew

Reputation: 841

Using a libgdx project as a view inside an Android app is now documented clearly, with example code, in the libgdx wiki, implemented as a Fragment (the best practice for modern Android apps):

  1. Add Android V4 Support Library to the -android project and its build path if you haven't already added it. This is needed in order to Extend FragmentActivity later
  2. Change AndroidLauncher activity to extend FragmentActivity, not AndroidApplication
  3. Implement AndroidFragmentApplication.Callbacks on the AndroidLauncher activity
  4. Create a Class that extends AndroidFragmentApplication which is the Fragment implementation for Libgdx.
  5. Add the initializeForView() code in the Fragment's onCreateView method.
  6. Finally, replace the AndroidLauncher activity content with the Libgdx Fragment.

Upvotes: 9

Kamran Bigdely
Kamran Bigdely

Reputation: 8456

I have created a Hello World program on github for libgdx running in a fragment using Android Studio 2.1. It follows the instructions on the official libgdx wiki.

enter image description here

AndroidLauncher class:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.badlogic.gdx.backends.android.AndroidFragmentApplication;

public class AndroidLauncher extends FragmentActivity implements  AndroidFragmentApplication.Callbacks {
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout);

        // Create libgdx fragment
        GameFragment libgdxFragment = new GameFragment();

        // Put it inside the framelayout (which is defined in the layout.xml file).
        getSupportFragmentManager().beginTransaction().
                add(R.id.content_framelayout, libgdxFragment).
                commit();
    }

    @Override
    public void exit() {

    }


}

The GameFragment class:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.badlogic.gdx.backends.android.AndroidFragmentApplication;

public class GameFragment extends AndroidFragmentApplication{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // return the GLSurfaceView on which libgdx is drawing game stuff
        return initializeForView(new MyGdxGame());
    }
}

layout.xml:

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

    <FrameLayout
    android:id="@+id/content_framelayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2">
    </FrameLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#FF0000"
        android:textColor="#00FF00"
        android:textSize="40dp"
        android:text="I'm just a TextView here with red background :("/>

</LinearLayout>

MyGdxGame class:

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture img;
    private BitmapFont font;


    @Override
    public void create () {
        batch = new SpriteBatch();
        img = new Texture("badlogic.jpg");
        font = new BitmapFont();
        font.setColor(Color.BLUE);
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();

        //batch.draw(img, 0, 0);
        font.getData().setScale(6.0f);
        font.draw(batch, "Hello World from libgdx running in a fragment! :)", 100, 300);

        batch.end();
    }

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

Make sure you've added the following:

compile "com.android.support:support-v4:24.1.1"

To the project gradle script in the "dependencies {.}" section inside project (":android") section.

Upvotes: 16

Matsemann
Matsemann

Reputation: 21784

The AndroidApplication class (which extends activity) has a method named initializeForView(ApplicationListener, AndroidApplicationConfiguration) that will return a View you can add to your layout.

So your Test-class can extend AndroidApplication instead of Activity so that you can call that method and add the View to your layout.

If that's not an option, for some reason, take a look at what AndroidApplication source code does, and mimic that.

Upvotes: 23

Suman
Suman

Reputation: 71

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
    cfg.useGL20 = false;
    //initialize(new LoveHearts(), cfg);
    LinearLayout lg=(LinearLayout)findViewById(R.id.game);
    lg.addView(initializeForView(new LoveHearts(), cfg));
}

Upvotes: 7

Related Questions