How to add libgdx to existing project - android

I have provided an aplication made in android that has a navigation drawer and in it has a list of games. I have to create another game and to put it there. The game that has to be created by my must use libGDX but the original application didn't use this library.
Is it possible to do this ? If Yes, how can I add the libgdx to the exiting project. On github I found only how to start a new project with libGDx, an not how to add it to exising code. thanks.

You can achieve this with these 2 steps:
1. Add LibGDX to your project
On your android gradle file: build.gradle (app or android module)
dependencies {
...
// Add this line, it is recommended to use the latest LibGDX version
api "com.badlogicgames.gdx:gdx-backend-android:1.9.10"
}
2. Initialize LibGDX for a view or use a LibGDX managed activity
Option 1: Initialize for a view
Since you have a navigation drawer and more code native to android this option fits your needs better. From this question (How to add LibGDX as a sub view in android):
The AndroidApplication class (which extends activity) has a method named initializeForView(ApplicationListener, AndroidApplicationConfiguration) that will return a View you can add to your layout.
-- Matsemann
Also here's documentation on how to implement this (Fragment based LibGDX).
Option 2: Use a managed activity
This is the default/most common use of LibGDX, you will need to change your code as follows:
Your activity needs to extend Android application:
public class MainActivity extends AndroidApplication {
Create a class extending Game or ApplicationAdapter:
import com.badlogic.gdx.Game;
public class LibGDXGame extends Game {
#Override
public void create() {
System.out.println("Success!");
}
}
Initialize the LibGDX managed activity:
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
public class MainActivity extends AndroidApplication {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new LibGDXGame(), config);
}
}

Generally what you want is possible, I reckon your best bet would be to create a new libgdx project with their GUI and to then manually merge the files that are needed.

Related

How to load a specific layout of an activity in android

I am working on an augment reality android app. I developed the app in unity using vuforia and then imported it in android studio as i wanted to design it in android studio. The activity name in generated project structure is UnityPlayerActivity which has no layout in the project. The activity is passed to UnityPlayerclass and the layout is generated. This is some code of my UnityPlayerActivity activity.
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// Setup activity layout
#Override protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy
mUnityPlayer = new UnityPlayer(this);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
}
as it uses that layout how can i design that activity by referencing it.
It is not possible to do this by referencing the object(Java does not support call by reference).
You need to define a new function in UnityPlayer class that generates a layout and returns the LayoutParams object.
Then use the Layout inflator. Something like this:
LayoutParams mParams=mUnityPlayer.layoutGenerator() //some function to generate LayoutParams
LayoutInflator.getLayoutInflator().inflate(LayoutParams);
Finally i found an easy solution to my problem. I displayed the unity scenes in a subview using a framelayout and now i can customize the UI easily. Check here and here to see more information.

React Native modules into an existing app

I've been working on adding react native to an existing app. I followed the guide on the official docs and after fixing some errors that popped up all was well.
Now I want to use react native components to talk to native code. There are a few use-cases that I need but the current one is that I have an AndroidToolbar and when clicking the navIcon I want to end the current Activity (I want to use native navigation). My thought was to create a native module and just simply call it from my react native code (natively calling onBackPressed). The problem is that when following the docs they suggest to add the package to getPackages function within your ReactApplication. Something like:
public class MainApplication extends Application implements ReactApplication {
...
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new AnExampleReactPackage());
}
...
}
The problem is that if you follow the "Integration with existing apps" docs then you never create a ReactApplication. Instead you just create an Activity somewhere in your app and just start that activity somewhere in your app. If anyone can point me in the right direction for how to call native code when you have integrated into an existing app, that would be greatly appreciated.

AS3 Class Error gotoAndPlay(33)

i´m trying to add admob ane to my game but i´m getting an error.
My error is this, when i import the class i got and erro in my project with the gotoAndPlay(33);
how can i define into class this gotoAndPlay(33); to stops error.
i´m using this package
package
{
import flash.display.Sprite;
import so.cuo.platform.admob.Admob;
import so.cuo.platform.admob.AdmobPosition;
public class SimpleDemo extends Sprite
{
public function SimpleDemo()
{
super();
var admob:Admob= Admob.getInstance();
if(admob.supportDevice){
admob.setKeys("your banner id ");
admob.showBanner(Admob.BANNER,AdmobPosition.BOTTOM_CENTER);
}
}
}
}
So, i don´t know how to declare this gotoAndPlay(33) event in the class.
Since now thanks to all!
Sounds like somewhere your code is trying to use SimpleDemo like it's a movie clip.
To give SimpleDemo all the powers of a MovieClip, make it extends that class
public class SimpleDemo extends MovieClip
instead of extending Sprite which doesn't have timeline functionality.
Now SimpleDemo will have all the MovieClip Methods, like gotoAndPlay.
If SimpleDemo is set as the class file for your document class (or a library object that has more than one frame or code on a frame), then you MUST extend MovieClip (though for the document class it CAN extend Sprite or DisplayObjectContainer if no timeline code or additional frames are used).

Android + Phonegap + Admob

As the title suggests I am trying to create an Android app using Phonegap and then trying to insert Admob into it.
I am using this tutorial provided by Adobe and it is working fine. After this when I use this tutorial provided by Google I run into problems.
According to the Google tutorial, I have to change the activity class, below is the original and the second block of code is the altered code. There doesn't seem to be any errors preventing it from compiling. It's just when I try and run it using the Android emulator I get an error stating "Unfortunately MyFirstPhonegapPlugin has stopped"
Note, using the Google tutorial, there are two ways of implementing the code, using just the class below and using a mixture of XML and the class below. I tried the other way and due to getting errors which doesn't let me compile, I've gone for this way.
package com.tricedesigns;
import org.apache.cordova.DroidGap;
import android.os.Bundle;
public class MyFirstPhoneGapPluginActivity extends DroidGap {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
Adjusted code:
package com.tricedesigns;
import org.apache.cordova.DroidGap;
import android.os.Bundle;
import com.google.ads.*;
public class MyFirstPhoneGapPluginActivity extends DroidGap {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
// Look up the AdView as a resource and load a request.
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
}
}
I am using Phonegap 1.6.1 and the most up to date SDK for Android and Admob
The way Phonegap is implemented, it pretty much hides all the details about Android and lets you basically implement a mobile web app as a native app. If you care to implement Android natively, I'd recommend reading the Hello World tutorial. The code you have here is crashing on AdView adView = (AdView)this.findViewById(R.id.adView); because you don't have a layout file with an AdView element with id adView.
If you're more interested in the mobile web part of it and plan to continue sticking with Phonegap, your best bet is to try this plugin which lets you make JavaScript calls using the Phonegap framework, and the plugin performs all of the underlying Android calls necessary to get ads.
Here is a fully functional project of what I was looking for
old version phonegap not support extenal jar lib.
but now phonegap add some new feathers.
use phonegap builder ,add config
add some config
<gap:plugin name="com.admob.plugin" version="1.0.0" source="plugins.cordova.io"/>
so not need any java code any more.
just need some js code.
admob.initAdmob("admob banner ID","admob interstitial ID");
admob.showBanner(admob.BannerSize.BANNER,admob.Position.TOP_APP);
this will create and show a admob banner at top of your app.

Libgdx and Leadbolt

I'm thinking of integrating Leadbolt (or Tapjoy) to my Libgdx game. I want to make a store based on the clicked forms. More click - more stuff from the store for the user. I found Leadbolt and Tapjoy. These ad providers are providing direct helps for these stuffs. I have take a look on the Leadbolt integrating guide. I have a problem with it. It needs me to pass a context to the AdController. I don't know if it possible in any way to pass the context for the AdController or not, so I would like to ask you about it, that how can I do, if I can. (I haven't tried it in Libgdx yet, but with the superjumper example had problem too, when I wanted to pass a context for something, and I think because the Libgdx app don't extends an Activity, it will have problems too.)
Here is the sample code from Leadbolt:
AdController myControllerForm = new AdController(this, "MY_LB_ID", new AdListener() {
public void onAdProgress() {}
public void onAdLoaded() {
myControllerForm.hideAd();
}
public void onAdFailed() {
launchMain();
}
public void onAdCompleted() {
myControllerForm.hideAd();
launchMain();
}
public void onAdClosed() {
launchMain();
}
public void onAdClicked() {}
public void onAdAlreadyCompleted() {
launchMain();
}
public void onAdHidden() {
launchMain();
}
});
myControllerForm.setAsynchTask(true);
myControllerForm.loadAd();
}
public void launchMain()
{
finish();
startActivity(new Intent(Splash.this, MainApp.class));
}
}
The class extends Activity, and the methods are in the onCreate() method in this example.
If you have integrated the Leadbolt or Tapjoy to your Libgdx game, then could you please give me a code about how did you do it?
Thanks in advance!
LibGDX actually extends AndroidActivity (by AndroidApplication, which extends AndroidActivity).
If you setup your project correctly, you can access it from "Android starter" project. This is also the only place, where you can play with ads, because "Desktop starter" in no way extends AndroidActivity.
Here's also adMob tutorial, which you may find useful (creating overlaying views).
This is all info I can give you, as I don't know whether you want to display your ads always, reload them as time passes or just hide them after particular events. In such cases, you might want to implement your custom interfaces.
Good luck!
Check out the libGDX tutorial on AdMob: http://code.google.com/p/libgdx/wiki/AdMobInLibgdx
Skip the stuff at the top about setting up AdMob, I think you need the part in the "Control" section which talks about getting events from your generic libGDX code (which has to also run on the desktop) into your Android-specific code (for example to show an ad). The general way is to define your own interface (see the IActivityRequestHandler in the AdMob tutorial), and pass an object that implements that interface into your libGDX code. On the desktop this object would do nothing, and on the Android side you can use all the standard Android code to do the right thing.

Categories

Resources