Skip to content

Commit

Permalink
Implemented Interstitial
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDT authored and MrDT committed Apr 18, 2014
1 parent 9a4c934 commit 0a5ea37
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 9 deletions.
2 changes: 1 addition & 1 deletion tutorial-libgdx-google-ads-android/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My LibGDX Game</string>
<string name="app_name">Google Ads Tutorial</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

public class MainActivity extends AndroidApplication {
public class MainActivity extends AndroidApplication implements ActionResolver {

private static final String AD_UNIT_ID = "ca-app-pub-6916351754834612/3101802628";
private static final String AD_UNIT_ID_BANNER = "ca-app-pub-6916351754834612/9855033027";
private static final String AD_UNIT_ID_INTERSTITIAL = "ca-app-pub-6916351754834612/3808499421";
private static final String GOOGLE_PLAY_URL = "https://play.google.com/store/apps/developer?id=TheInvader360";
protected AdView adView;
protected View gameView;
private InterstitialAd interstitialAd;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -52,12 +57,25 @@ public void onCreate(Bundle savedInstanceState) {

setContentView(layout);
startAdvertising(admobView);

interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Toast.makeText(getApplicationContext(), "Finished Loading Interstitial", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdClosed() {
Toast.makeText(getApplicationContext(), "Closed Interstitial", Toast.LENGTH_SHORT).show();
}
});
}

private AdView createAdView() {
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setAdUnitId(AD_UNIT_ID_BANNER);
adView.setId(12345); // this is an arbitrary id, allows for relative positioning in createGameView()
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
Expand All @@ -68,7 +86,7 @@ private AdView createAdView() {
}

private View createGameView(AndroidApplicationConfiguration cfg) {
gameView = initializeForView(new AdTutorial(), cfg);
gameView = initializeForView(new AdTutorial(this), cfg);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
Expand All @@ -81,7 +99,27 @@ private void startAdvertising(AdView adView) {
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}


@Override
public void showOrLoadInterstital() {
try {
runOnUiThread(new Runnable() {
public void run() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
Toast.makeText(getApplicationContext(), "Showing Interstitial", Toast.LENGTH_SHORT).show();
}
else {
AdRequest interstitialRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(interstitialRequest);
Toast.makeText(getApplicationContext(), "Loading Interstitial", Toast.LENGTH_SHORT).show();
}
}
});
} catch (Exception e) {
}
}

@Override
public void onResume() {
super.onResume();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.theinvader360.tutorial.libgdx.google.ads;

public class ActionResolverDesktop implements ActionResolver {

@Override
public void showOrLoadInterstital() {
System.out.println("showOrLoadInterstital()");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public static void main(String[] args) {
cfg.width = 480;
cfg.height = 320;

new LwjglApplication(new AdTutorial(), cfg);
new LwjglApplication(new AdTutorial(new ActionResolverDesktop()), cfg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.theinvader360.tutorial.libgdx.google.ads;

public interface ActionResolver {
public void showOrLoadInterstital();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Vector2;

public class AdTutorial implements ApplicationListener {
public class AdTutorial implements ApplicationListener, GestureListener {
private ActionResolver actionResolver;
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;

@Override
AdTutorial(ActionResolver actionResolver) {
this.actionResolver = actionResolver;
}

@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
Expand All @@ -33,6 +41,9 @@ public void create() {
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);

GestureDetector gd = new GestureDetector(this);
Gdx.input.setInputProcessor(gd);
}

@Override
Expand All @@ -52,6 +63,12 @@ public void render() {
batch.end();
}

@Override
public boolean tap(float x, float y, int count, int button) {
actionResolver.showOrLoadInterstital();
return true;
}

@Override
public void resize(int width, int height) {
}
Expand All @@ -63,4 +80,45 @@ public void pause() {
@Override
public void resume() {
}

@Override
public boolean touchDown(float x, float y, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean fling(float velocityX, float velocityY, int button) {
return false;
}

@Override
public boolean longPress(float x, float y) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean panStop(float x, float y, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean zoom(float initialDistance, float distance) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
// TODO Auto-generated method stub
return false;
}
}

0 comments on commit 0a5ea37

Please sign in to comment.