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.
Related
I have a libgdx game and I successfully added Admob ads to it. When I run it on my physical device using Android Studio it shows ads as I used my device as test device but when I send it to another device from my phone it didn't show ads. It only showed a black rectangle with no ads as I set adView background to black colour.
My question is: should it be uploaded to the Google Play Store to show the ads or what?
Here is my code:
protected AdView adView;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
View gameView = initializeForView(new GameMain(), config);
layout.addView(gameView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setVisibility(View.VISIBLE);
AdRequest.Builder builder = new AdRequest.Builder();
adView.loadAd(builder.build());
adView.setBackgroundColor(0xff000000);
adView.setAdUnitId("xxxxxxxxxxxxxxxxxxx");
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT );
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, adParams);
setContentView(layout);
}
Sounds to me like the problem is the ad size is changing. The banner needs to be the exact size specified by Google. If it's even a couple pixels smaller it will not display. My guess would the new device you're testing on has a smaller screen or different aspect ratio, and that size difference is propagating down to the view where you have the advertisement embedded. Can't be sure without a little more info though.
You need to specify admob which are your test devices on which you want to see ads. For that use addTestDevice.
AdRequest adRequest = new AdRequest.Builder()
.setBackgroundColor(0xff000000)
.setAdUnitId("xxxxxxxxxxxxxxxxxxx")
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // Emulator
.addTestDevice("FirstDeviceID")
.addTestDevice("SecondDeviceID")
.build();
adView.loadAd(adRequest);
Hope this helps.
Please check your code you load add before set the admob id. you just change this in your code
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setVisibility(View.VISIBLE);
AdRequest.Builder builder = new AdRequest.Builder();
adView.setBackgroundColor(0xff000000);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
adView.loadAd(builder.build());
I have a game on Libgdx and I have been trying to add AdMob banner to it.
I have 2 problems with it,
1) The banner takes like a minute to load and to be shown on the screen and it shouldn't
2) The banner is showing on every screen and I want it to show itself only on the Menu screen and not the Game Screen.
Here is the code I used:
public class AndroidLauncher extends AndroidApplication {
AdView adView ;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// initialize(new GameMain(), config);
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 GameMain(),config);
layout.addView(gameView);
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-4647665408548722/8293730490");
adView.setAdSize(AdSize.BANNER);
AdRequest adRequest = new AdRequest.Builder().build();
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(adView, adParams);
// Hook it all up
adView.loadAd(adRequest);
setContentView(layout);
}
The banner really takes a few minutes to load, so you must to load the banner a little time before show it. About the second question, you should set false when you don't want to show the banner; and set true to show it when you want.
Concerning your first issue,
Loading an ad requires a network request which is quiet slow. You can try defining the ad in a chronological order, though I doubt this will provide a better experience:
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-4647665408548722/8293730490");
adView.setAdSize(AdSize.BANNER);
AdRequest adRequest = new AdRequest.Builder().build();
// Hook it all up
adView.loadAd(adRequest);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(adView, adParams);
Concerning your second issue...
There are multiple ways to solve that. The best solution is to use an interface that has the ability to show and hide ads.
Define an interface AdHandler.java in your game's core project:
public interface AdHandler {
public void showAds(boolean show);
}
Then, implement the interface in your android project.
Just follow the official libgdx guide on controlling admob ads.
Go to the topic:
" Control
This example will show you how to turn the AdMob View's visibility on and off from within your libgdx app. "
https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx .
It has all you need.
I read this and implemented it on the code but the ads aren't going away.
here is the android launcher:
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// initialize(new GameMain(), config);
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 GameMain(),config);
layout.addView(gameView);
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-4647665408548722/8293730490");
adView.setAdSize(AdSize.BANNER);
AdRequest adRequest = new AdRequest.Builder().build();
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(adView, adParams);
// Hook it all up
adView.loadAd(adRequest);
setContentView(layout);
}
private final int SHOW_ADS = 1;
private final int HIDE_ADS = 0;
protected Handler handler = new Handler()
{
#Override
public void handleMessage(Message msg) {
switch(msg.what) {
case SHOW_ADS:
{
adView.setVisibility(View.VISIBLE);
break;
}
case HIDE_ADS:
{
adView.setVisibility(View.GONE);
break;
}
}
}
};
#Override
public void showAds(boolean show) {
handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
}}
and in the window i dont want the ads to show up i did this:
constructor:
public Main(Game game,AdHandler a)
{
this.game = game;
this.ad =a;
}
and the call to the function:
ad.showAds(false);
what is wrong with the code?
edit: i checked and the showads function in the androidLauncher isn't even been called i cant find the reason
I've found this solution. Please test this one:
Add this into LinearLayout
<com.google.ads.AdView
android:id="#+id/adMob1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="{AdID}"
ads:loadAdOnCreate="true"/>
Then add this into OnCreate
AdView adView = (AdView) findViewById(R.id.adMob1);
adView.loadAd(new AdRequest());
If it works, you can add your specific code.
For more information https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals
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.
Finally, I have a banner ad at the top of my GLSurfaceview. However, it appears with a black background that takes up the whole width of the screen and covers the top of my game area like so (I should also point out that the play area is also moved downwards a little so the bottom is also missing).
What I need to do is move the banner to the bottom of the screen and, keep it central and remove this black background so it looks like this:
I tried to use XML but was getting a lot of errors so I switched to doing this completely in Java (and managed to get this far) - however quality information on how to do this with GLSurfaceView is lacking IMHO so I'm hoping someone can tell me where I'm going wrong.
Code
Here is my onCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// Create an ad request.
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(TestDeviceID)
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
//Request full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create a displayMetrics object to get pixel width and height
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
//Create and set GL view (OpenGL View)
myView = new MyGLSurfaceView(MainActivity.this);
layout.addView(adView);
layout.addView(myView);
//Create a copy of the Bundle
if (savedInstanceState != null){
newBundle = new Bundle(savedInstanceState);
}
//Set main renderer
setContentView(layout);
}
When the banner changes, it also appears to 'flicker', but I can deal with that in a separate question.
Use a RelativeLayout or a FrameLayout as your parent layout, then just define the layout parameters for the adView to be positioned at the bottom center of the screen like this. Below is a solution:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setBackgroundColor(Color.TRANSPARENT);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// Create an ad request.
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(TestDeviceID)
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
//Request full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create a displayMetrics object to get pixel width and height
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
//Create and set GL view (OpenGL View)
myView = new MyGLSurfaceView(MainActivity.this);
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(myView);
layout.addView(adView, adParams);
//Create a copy of the Bundle
if (savedInstanceState != null){
newBundle = new Bundle(savedInstanceState);
}
//Set main renderer
setContentView(layout);
}
You have two separate views that are divided when you add them both to a linear layout (surface view and your ad view). I think what you want to accomplish is to overlay the ad on top of your surface view. This page is old but check out http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/ for an example on how to do this.
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);