1

I would like to display some custom views ( e.g. TextViews with Arabic/Asian text) on top of everything that happens in libgdx's OpenGL Surface (<--I assume it's a surface view). Is this possible? How?

The motivation is that I have a proven component for displaying text in my existing code and I would like to keep using this.

I have used other GLSurfaceViews with View hierarchies before. Just wondering how/if this can be done in libgdx. Performance doesn't matter all to much because the "game" in libgdx is a simple puzzle game, and when the text views are shown, the game is pretty much paused.

treesAreEverywhere
  • 3,722
  • 4
  • 28
  • 51

1 Answers1

8

Yes you can! This official guide on using Admob in Libgdx has all you need help you display custom views. Though it focuses on integrating Admob ads, you can use the mechanism to create and display views on top of the OpenGL Surface. For example this is how you'd display a text view on top of your android game's surface view:

   @Override public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create the layout
        RelativeLayout layout = new RelativeLayout(this);

        // Do the stuff that initialize() would do for you
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

        // Create the libgdx View
        View gameView = initializeForView(new HelloWorld(this), false);

        // Create and setup the TextView
        TextView helloText = new TextView(this); 

        // Add the libgdx view
        layout.addView(gameView);

        // Add the TextView
        RelativeLayout.LayoutParams textViewParams = 
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
        textViewParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        textViewParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        layout.addView(helloText, textViewParams);

        // Hook it all up
        setContentView(layout);
    }
Nana Ghartey
  • 7,901
  • 1
  • 24
  • 26
  • Thanks a lot! Small follow up question: If I wanted to call a method of HelloWorld, (e.g. I click the TextView in Android and want to show something in libgdx), I could just keep a reference to the HelloWorld instance and call methods on it? Or is there something else I need to consider, maybe go through a handler similar? – treesAreEverywhere May 15 '14 at 23:42
  • Yes! You can use an interface for such abstractions via the Facade design pattern. Follow this for guidance - https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code – Nana Ghartey May 15 '14 at 23:52
  • Read that just before (https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code). The interface is used for the direction libgdx --> android. How about the other way? I'm wondering if I can/should call methods from the Android UI thread --> libgdx. Or am I missing something here? – treesAreEverywhere May 16 '14 at 00:05
  • ok. That's possible, but doing this means you're targeting the android platform only. Also you'd still have to use an interface and make the calls on the android UI thread. Example: activityListener.runOnUiThread(new Runnable() { public void run() {//textview calls} }); – Nana Ghartey May 16 '14 at 00:46
  • To sum it up: For running code on the GL thread (game thread), there's this: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/Application.html#postRunnable(java.lang.Runnable), Accessible via Gdx.app.postRunnable(). The game object instance (http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/ApplicationListener.html) would have a method that is called from Android and posts runnables on the game thread. – treesAreEverywhere May 16 '14 at 01:18