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
Related
I'm trying to reload manually an AdView when orientation changes. I'm able to issue the request and get an add back. The problem is that when I try to remove the old view and add a new one, I get this warning:
W/Ads: Not enough space to show ad. Needs 592x32 dp, but only has 640x0 dp.
And the view is not shown. This is my onConfigurationChanged method:
#Override
public void onConfigurationChanged(Configuration newConfig) {
Log.v(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
ViewGroup.LayoutParams adViewParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adViewParent.removeView(adView);
adView.destroy();
AdView newAdView = new AdView(this);
newAdView.setAdSize(AdSize.SMART_BANNER);
newAdView.setAdUnitId(getString(R.string.admob_ad_unit_id_main));
newAdView.setLayoutParams(adViewParams);
newAdView.setId(R.id.adView);
adView = newAdView;
adViewParent.addView(adView);
loadAd();
}
I've read other similar answers like this one android admob resize on landscape but they didn't work for me and I really don't know what I'm doing wrong.
Thank you for the help.
I hope it helps you:
ViewGroup.LayoutParams adViewParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.dpToPx(32));
And hide adViewParent.setVisibility(GONE) if loading of ad is failed
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 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.
I am developing a game and it's coming along quite nicely. I do have a bit of a problem about the AdMob ad refreshing though. Every time the ad is refreshed or it draws a different aspect of the ad, my frame rate plummets and almost makes the game unplayable. Here is what I have for the loading of the ad...
ad = new AdView(this, AdSize.BANNER, "...");
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice("...");
adRequest.addTestDevice("...");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
ad.setLayoutParams(lp);
RelativeLayout layout = new RelativeLayout(this);
layout.addView(renderView);
layout.addView(ad);
ad.loadAd(new AdRequest());
setContentView(layout);
My solution for rendering the ad on top of the SurfaceView was to just add it to a RelativeLayout and add both the SurfaceView and AdView to it. This all works fine and dandy, but every time the ad refreshes (UI or new Ad request), it bogs down the UI thread, which in turn slows down my render thread.
Is there a way that I can make all of this work together nicely to have all work done by the AdView done separately from the main thread? I am not too sure about dynamically updating the current layout from another thread.
Thanks for the help.
I had this problem too. I found it was caused by Google ads which animate when they change, rather than static Admob banner ads which don't animate at all. There's a setting in your admob app settings for controlling whether Google ads are used... try turning it off to see if it makes a difference.
Apologies for resurrecting this question, but I have managed to come across the same issue, and found out that turning LAYER_TYPE_SOFTWARE on the AdView's internal WebView made it smoother.
I have confirmed this working on my side with version 8.4.0 running on Android 5.1, although it is possible that this workaround will benefit more devices with stronger CPUs.
In my activity's onResume I have
/*
* The AdView attaches it's internal WebView only once it
* has been loaded, so we need to wait until that happens.
*/
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
runOnWebView(mAdView, new WebViewAction() {
#Override
public void run(WebView view) {
// the most important part is here
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
});
}
});
with the runOnWebView method and WebViewAction interface defined as
/**
* Recursively searches for a WebView instance located in
* the hierarchy of view and invokes the callback method on
* action with the found instance.
*/
private void runOnWebView(View view, WebViewAction action) {
if (view instanceof WebView) {
action.run((WebView) view);
return;
}
if (view instanceof ViewGroup) {
final ViewGroup parent = (ViewGroup) view;
for (int i = 0; i < parent.getChildCount(); i++) {
runOnWebView(parent.getChildAt(i), action);
}
}
}
private interface WebViewAction {
void run(WebView view);
}
I'm trying to hide the Ads from AdMob after 2 minutes, what I've done is, after 2 minutes, I'll stop requesting fresh ad and to hide AdView, have written
if (adView.getVisibility() == AdView.VISIBLE)
adView.setVisibility(AdView.GONE);
what's happening is Ad becomes invisible but still occupies the space, not releasing it. and I want to release that space.
Any Ideas, Thanks in Advance.
if (adView.getVisibility() == AdView.VISIBLE)
adView.setVisibility(View.GONE);
I would use a flag and wrap it like this. Then set it to true/false whenever.
if (Settings.ADMOB_ACTIVE) {
if (Settings.D) {
Log.d(TAG, "Adding Admob");
}
final AdView adView = new AdView(this, AdSize.BANNER, Settings.PUBLISHERID);
final LinearLayout layout = (LinearLayout)findViewById(R.id.adLayout);
if (adView != null && layout != null) {
layout.addView(adView);
final AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
}
}
I handled this function inside a thread, that's why I got exception and it didn't releases the space, instead now I used Thread Handler to handle the function and now its working fine and now its not occupying any space.