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.
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 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
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 have a problem with AdSence. I have tried to resolve it the last couple of days but without any good results. I used developers guide but it still empty on the place of banner.
What am I doing in an application. First of all I have added a TableRow object and added AdView to it:
banner = new TableRow(engine);
banner.setGravity(Gravity.TOP & Gravity.RIGHT);
adView = new AdView(engine, AdSize.BANNER, "a14def8xxxxxxxx");
banner.addView(adView);
AdRequest request = new AdRequest();
adView.loadAd(request);
Afterwards I just added this "banner" object to the other view. At the end - there is no output there. In case I changed AdView to TextView, just for the proof of concept, it works fine.
The output log:
06-13 18:04:38.476: INFO/Ads(576): Received ad url: <"url": "http://r.admob.com:80/ad_source.php?... >
06-13 18:04:40.406: INFO/Ads(576): onReceiveAd()
The only strange thing for me in log is:
06-13 18:04:40.336: WARN/webcore(576): Can't get the viewWidth after the first layout
I haven't found what is it means and is it because of AdSence.
Update
I have the class:
public class QuestEngine extends Activity {
Then I am trying to produce new AdView in method:
public IntroView(QuestEngine engine) {
That is why in "new AdView" I am using engine object.
Change the new AdView declaration to:
adView = new AdView(this, AdSize.BANNER, "a14def820df0417");
The engine object does not contain the context for its dimensions. The dimensions of the table row are in the Activity context "this"
Well if you are setting your adView via xml/layout file then do as directed below:-
In your activity.xml file insert the following tag where you want your AdView to be shown:-
<com.google.ads.doubleclick.DfpAdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="/XXXXX/XXXXX"
ads:adSize="BANNER"
/>
And in your Activity.java file write this code in your onCreate() Method of the Activity Life Cycle:-
// Look up the DfpAdView as a resource and load a request.
adView = (DfpAdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
Now if you want to programmatic set the AdView in your Activity then follow the steps as directed below:-
Write the following code in your onCreate() method,
I am demonstrating for Fragment, so write the following code in your onActivityCreated()Method.
// Create an ad view as a second header for now to meet deadlines
DfpAdView mAdView=null; //you can also define this as a Global Variable.
if (mAdView == null) {
mAdView = new DfpAdView(getActivity(), AdSize.BANNER, "/XXXX/XXXX"); //give your adId incase of XXXX
iaddHeight= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()); //This code is used to set the height of the addHeight as 50 pixels, which could be used in layout params.
RelativeLayout fl =new RelativeLayout(this.getActivity());
RelativeLayout.LayoutParams flParams =new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
flParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
FrameLayout.LayoutParams para =new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
para.setMargins(0, 0, 0,iaddHeight);
getListView().setLayoutParams(para);
this.getActivity().addContentView(fl, flParams);
fl.addView(mAdView,flParams);
}
mAdView.loadAd(new AdRequest());
Hope This can help Any Future users...