3

I have built an app for crypto currencies, to maintain a portfolio. I planned to show the latest news items in the same app.

The whole project is an Android app done in LibGdx.

Question

  1. How to have a webview for the area "Part-B"?

  2. LigGdx have anything like WebView?

  3. Does LibGdx has any extensions to take care of WebView?

Screenshot

enter image description here

Zoe
  • 27,060
  • 21
  • 118
  • 148
iappmaker
  • 2,945
  • 9
  • 35
  • 76

1 Answers1

4

Starting with your last two questions; no. LibGDX does currently no extensions, nor native support for webviews or something similar. You can, however, create a custom layout. LibGDX has a initializeForView method, which you can use to grab the View itself. This is inflated in a Fragment, which is added to the layout itself. The WebView is then the other view.

First off, the launcher:

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.gdxwebview);

        // Create the fragment
        GDXWithWebview fragment = new GDXWithWebview();

        getSupportFragmentManager().beginTransaction().
                add(R.id.fragmentRoot, fragment).
                commit();
    }

    @Override
    public void exit() {}


}

Since fragments are involved, the launcher is different from the generated launcher

For the fragment:

The reason behind using a fragment is to inflate LibGDX into the fragment, which ends up as a sub-unit of the activity. If you set the content view in the activity, you will not get a WebView in there as well.

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 GDXWithWebview extends AndroidFragmentApplication{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // return the GLSurfaceView on which libgdx is drawing game stuff
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();//Configure with whatever you need
        return initializeForView(new MyGdxGame(), config);//WARNING!! Replace MyGdxGame with your game class.
    }
}

And finally, the layout. It's a basic layout, and the weights (/sizes) may need tweaking for you to get it to look just like you want it to

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.gdxwebview);//The layout; comes later

        // Create the fragment
        GDXWithWebview fragment = new GDXWithWebview();

        getSupportFragmentManager().beginTransaction().
                add(R.id.fragmentRoot, fragment).
                commit();//Add the fragment 
    }

    @Override
    public void exit() {}


}

In addition, if you plan on using the WebView to go online, you need the Internet permission (if you use it to execute JavaScript or other web content locally, without going on the Internet, you shouldn't need it):

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 GDXWithWebview extends AndroidFragmentApplication{
    View root;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    { 
        // return the GLSurfaceView on which libgdx is drawing game stuff
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();//Configure with whatever you need
        root = initializeForView(new MyGdxGame(), config);//WARNING!! Replace MyGdxGame with your game class.
        //declaredInTheClassWebView = (WebView) root.findViewById(R.id.webView);//For initialization, make sure you call root.findViewById, not findViewById. You have to specify the view in which to find the ID when dealing with Fragments.
        return root;
    }
}

And per the LibGDX documentation on use with Fragments, you also need the v4 support library. Assuming you use Gradle 4.1 and Android Studio:

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

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

    <WebView android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"/>

</LinearLayout>

If you use an earlier version of Gradle and the Gradle plugin:

<uses-permission android:name="android.permission.INTERNET" />

And if you don't use Gradle, find the appropriate call for your build system. If you don't have a build system, grab the jar manually and add it to the classpath.

Zoe
  • 27,060
  • 21
  • 118
  • 148