I am really new to programming. I have followed the tutorial in Libgdx and google,
but i cannot put a banner ad using admob in my application.
Can someone help me to teach what i should do to put admob banner ad in my application ?
Thank you,
You need to create a View to contain both the adMob View and the Libgdx View, then setContentView() to this layout to which you must add both Views. Is this the tutorial to which you refer? It is very helpful.
Inside your android game-android project open the Activity and create a relative layout that is fullscreen and has no title bar as-
RelativeLayout layout=new RelativeLayout(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
Now below this change this line-
initialize(new Mygame(), cfg);
to
View gameView=initializeForView(new MyGame() , cfg);
This method initializeForView(new MyGame() , cfg); returns a view which is saved as a instance of view that will be added to the relative layout. Now create adview also to add to relative layout as-
Adview adView=new AdView(this);// create new adview instance
adView.setAdSize(com.google.android.gms.ads.AdSize.BANNER);// set the adsize
adView.setAdUnitId("XXXXXXXXX");// your adunit id
//Create layout params for adview
RelativeLayout.LayoutParams adParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.ALIGN_PARENT_RIGHT);//set position
//Add both gameview and adview to the relative layout-
layout.addView(gameView);
layout.addView(adView, adParams);
//Now just request ad by creating new instance of AdRequest
AdRequest request=new AdRequest.Builder().build();//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) to add test device
adView.loadAd(request);
// At last setcontent view to the Relative Layout that has both gameView and adView.
setContentView(layout);
You need to override onPause , onResume and onDestroy also to handle pause , resume or destorying of ad.
#Override
protected void onResume() {
super.onResume();
if(adView!=null){
adView.resume();
}
}
#Override
protected void onPause() {
super.onPause();
if(adView!=null){
adView.pause();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if(adView!=null){
adView.destroy();
}
}
Upto here adView is added to your game, but if you want to handle on when to show adView.You may want to hide adView when game is running and show when game is over, which can be achieved by creating handler.
Related
I’m trying to insert Appodeal banner adds into a LibGDX application but the banner ad is covering the screen instead of being placed above it. I cannot find any tutorials for using Appodeal in LibGDX, so I’m working from this tutorial on using Admob in LibGDX. https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx
I cannot figure out how to get the Appodeal banner ad in the form of a View which can be added to the RelativeLayout using the addView() method. I have tried a couple ways to do this which are labeled “ATTEMPT #1” and “ATTEMPT #2” but in both attempts the banner ad does not display at all. The line “Appodeal.show(this, BANNER);” places the banner on top of the screen, so that is commented out. Any help getting this figure out would be greatly appreciated. Thanks.
Here is my AndroidLauncher.java file where I create and show the banner ad.
public class AndroidLauncher extends AndroidApplication {
private final int BANNER = 4;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// LAYOUT: combines LibGDX View and Ad View
RelativeLayout relativeLayout = new RelativeLayout(this);
// Ad View
Appodeal.initialize(this, "fee50c333ff3825fd6ad6d38cff78154de3025546d47a84f", BANNER);
Appodeal.setTesting(true);
// Appodeal.show(this, BANNER);
// ATTEMPT #1
BannerView bannerView = Appodeal.getBannerView(this);
// ATTEMPT #2
// BannerView bannerView = new BannerView(this, null);
// Appodeal.setBannerViewId(bannerView.getId());
// LibGDX View
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
View libgdxView = initializeForView(new AppodealGDXDemo(), config);
// add to view
relativeLayout.addView(bannerView);
relativeLayout.addView(libgdxView);
// set layout
setContentView(relativeLayout);
}
}
I finally found info on the Appodeal website that helped me out. https://wiki.appodeal.com/en/android/2-5-8-android-sdk-integration-guide/ad-types
It appears they have made a lot of changes to their website as the links to documentation on their website that I found from google redirected to the main page.
I got it working now and here is the updated code.
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// LAYOUT: combines LibGDX View and Ad View
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// Ad View
Appodeal.initialize(this, "fee50c333ff3825fd6ad6d38cff78154de3025546d47a84f", Appodeal.BANNER_VIEW);
Appodeal.setTesting(true);
BannerView bannerView = Appodeal.getBannerView(this);
Appodeal.show(this, Appodeal.BANNER_VIEW);
// LibGDX View
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
View libgdxView = initializeForView(new AppodealGDXDemo(), config);
// add to layout
layout.addView(bannerView);
layout.addView(libgdxView);
// set layout
setContentView(layout);
}
}
I want something like this two other questions:
Android AdMob Vertical ads
How to show admob ads vertical in landscape mode? (Android)
And I start from this:
https://github.com/TheInvader360/tutorial-libgdx-google-ads
I tried so hard to put left side vertical banner on landscape mode, but I only get vertical admob at the center of screeen, I can't move it to the left.
Also, I am doing it in two differents "layers": banner overlap app view. I want this because I need full screen for app view.
Another point: I need to pause and resume app to show the banner, seems "layout.bringChildToFront" called before ad is loaded so not working.
(I have nothing special at AndroidManifest.xml, I am trying to do it by code)
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:screenOrientation="landscape"
android:orientation="vertical"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
/>
Here is my code: (I'm using LIBGDX)
public class AndroidLauncher extends AndroidApplication {
private static final String AD_UNIT_ID_BANNER = "ca-app-pub-7938591782705263/4150532439";
protected AdView adView;
protected View gameView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useAccelerometer = false;
cfg.useCompass = false;
// 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);
FrameLayout layout = new FrameLayout(this);//base container layout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
AdView admobView = createAdView();//ads the banner and the app view to the base layout
layout.addView(admobView);
View gameView = createGameView(cfg);
layout.addView(gameView);
setContentView(layout);
startAdvertising(admobView);
layout.bringChildToFront(admobView);//this is currently not working, I need to pause and resume app to show the banner
}
private AdView createAdView() {
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID_BANNER);
adView.setId(12345);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.LEFT|LinearLayout.FOCUS_LEFT|Gravity.TOP|LinearLayout.FOCUS_UP);
//last hard try to get vertical banner at left side :/
adView.setRotation(270);
adView.setPadding(0, 0, 0, 0);
adView.setTop(0);
adView.setLeft(0);
adView.setRight(0);
adView.setX(0);
adView.setY(0);
adView.refreshDrawableState();
adView.setLayoutParams(params);
return adView;
}
private View createGameView(AndroidApplicationConfiguration cfg) {
gameView = initializeForView(new Main(), cfg);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
gameView.setLayoutParams(params);
return gameView;
}
private void startAdvertising(AdView adView) {
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
#Override
public void onResume() {
super.onResume();
if (adView != null) adView.resume();
}
#Override
public void onPause() {
if (adView != null) adView.pause();
super.onPause();
}
#Override
public void onDestroy() {
if (adView != null) adView.destroy();
super.onDestroy();
}
(...)
Thankss a lot and have a nice day !!! ;)
Solved by having FrameLayout containing GameView and a relative layout which contains AdView with relative layout params (wrap content and align parent left) and changing rotation pivot then rotateview works fine.
But ad still not visible, I tried this: Admob ads appear only after first refresh and didn't work. I have to press home and resume app to see the ad...
i added banner ads from admob to my game and the banner add appears only after the first refresh (30 seconds) . And for the first 30 seconds it's there and if you click the bottom of the page where the addvertisment should be it takes you to the advertising page but the ad is not visible.
You can see for yourself when you start the game and click bottom of the screen.
Game #1
And my other game (if you start the intent from the menu in this game ( like facebook) and then return to the menu, ads appear instantly):
Game #2
Anyone had this happen ? What can i do to fix this ?
How I initialize my ads :
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
opener = new AndroidGalleryOpener(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
View gameView = initializeForView(new Zaidimas(opener), config);
AdView adView = new AdView(this);
adView.setAdUnitId("xxxxx my secret number xxxxxxxx");
adView.setAdSize(AdSize.BANNER);
adView.loadAd(new AdRequest.Builder()
.build());
layout.addView(gameView);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(adView, adParams);
setContentView(layout);
//initialize(new Zaidimas(opener), config);
}
It is hard to say what happens in your case, but I have a suspect that your libgdx screen with the admob banner might show up before the ad finished loading. You can try adding an AdListener that forces the ad-view to draw itself once the ad finished loading. Something like:
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
System.out.println("ad banner finished loading!");
adView.setVisibility(View.GONE);
adView.setVisibility(View.VISIBLE);
}
});
The code looks a bit odd, but changing visibility should really make sure that your adView gets drawn again ;-)
If you're using a Relative Layout be sure to include the position of the view relative to other layout objects. I was having the same issue until I placed android:layout_below="#+id/adView" on the object directly below my adView object. You could probably just as well place android:layout_above="#+id/someView" on the adView object itself.
I am making a game and I would like the ad to be visible during the menu but not when the game is actually being played.
If I simple set the adView Visibility to INVISIBLE when I don't want it shown then the ad will not refresh and thus i don't think will register with admob.
Is there an accepted way of implementing ads like this?
edit* Code in main activity to add the adView
private void initialiseAd()
{
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("");
AdRequest adRequest = new AdRequest.Builder().build();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adView.setLayoutParams(lp);
//load ads
adView.loadAd(adRequest);
//add the adView to the window
mainLayout.addView(adView);
}
The way you're facing it is correct, you can easily hide the AdView simply wrapping it within a FrameLayout (which you already seem to be doing) and calling the setVisibility() method over it.
If you're using Activity, this would be the way:
FrameLayout containingFrame = (FrameLayout) findViewById(R.id.ad_containing_framelayout_id);
containingFrame.setVisibility(View.GONE);
If you're using Fragment:
FrameLayout containingFrame = (FrameLayout) getActivity().findViewById(R.id.ad_containing_framelayout_id);
containingFrame.setVisibility(View.GONE);
I hide admob adview by view.gone:
//adView.setClickable(false);
//adView.clearFocus();
//adView.setEnabled(false);
//adView.setFilterTouchesWhenObscured(true);
//adView.setFocusable(false);
//adView.setFocusableInTouchMode(false);
adView.setVisibility(View.GONE);
adView.startAnimation( animation );
This hides the ad, but the adview itself is still touchable, so if I touch the adview's space, it still opens the browser and redirects me to the ad, although the ad itself is not visible.
How to disable the touch event too? I've tried all lines above but none of them worked.
Any advice?
Setting adView.setVisibility(View.GONE) and removing the AdMob view from the view hierarchy will hide the ad and prevent user interaction in most cases.
Don't forget to end the AdView lifecycle when the Activity displaying the ad is finished (destroyed). From the AdMob SDK Javadoc:
public void destroy()
Destroys the AdView. The AdView should no longer be used after this method is called.
Make a call to destroy() in the Activity's onDestroy() callback:
#Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
Try to use setOnTouchListener and Override onTouch like you want. Also you can use removeView():
LinearLayout linLay = (LinearLayout)findViewById(R.id.ad_layout);
linLay.removeView(adView);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
linLay.addView(adView, params);
and add it back when you need.
final com.google.ads.AdView ad = (AdView) findViewById(R.id.rect_ad);
if ( ad != null) {
ad.stopLoading();
ad.destroy();
getWindowManager().removeView(ad);
}
even this code doesn't destroy AdMob =(((
I have it's Handler and WebView in memory holding my activity