Completely remove AdView - android

I have an AdView defined in my Layout's XML and I want to make it disappear if the user bought the ad-free version of the app.
What I'm doing is
if(userhaspurchased){
if (mAdView != null) {
mAdView.setVisibility(View.GONE);
mAdView.pause();
ViewGroup vg = (ViewGroup) mAdView.getParent();
mAdView.destroy();
vg.removeView(mAdView);
mAdView = null;
}
Log.d("Purchased","true");
} else { //Load ad }
And the ad is not visible, but in logs I'm still getting AdView messages:
02-25 11:14:58.688: I/Ads(9279): Ad is not visible. Not refreshing ad.
02-25 11:14:58.688: I/Ads(9279): Scheduling ad refresh 60000 milliseconds from now.
And they are repeated every minute. Is there any way to disable completely the AdView?
Update: #Doomsknight answer is fine in my case, where I just have 2 layouts with ads. Otherwise would be a best practice to check the purchase and eventually insert the AdView programmatically.

Your xml is defining the ad view. And the connection automatically. So while you hide it, it has already been initialised.
One solution would be to have an xml that excludes the adview completely, for the paid version.
and set it at the top
if(userhaspurchased)
setContentView(R.layout.paid_version_xml);
else
setContentView(R.layout.free_version_xml);
This however depends on how many xmls you have, and adviews. As maintaining a variation for each page with adview on it may be an issue.

Don't add the adview view in the xml file. Instead, add a placeholder in the xml layout file.
<LinearLayout
android:id="#+id/admob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
</LinearLayout>
In your code:
if(!userhasPurchased)
{
AdView adView = new AdView(this, AdSize.BANNER, adunit_id);
LinearLayout layout = (LinearLayout) findViewById(R.id.admob);
layout.addView(adView);
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);
}

Related

Android, admob advert is not displaying

I am trying to implement a banner advert in my Android application. I have used Gradle to download the dependencies, etc.
In my activity where I want the advert to be displayed I have:
<com.google.android.gms.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="#string/ad_mob_id" />
And ad_mob_id is
ca-app-pub-3940256099942544/6300978111
Which is for test ads, I took that from https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start#step_1_modify_the_main_activity_layout
However when I run the activity nothing is displayed and there are no errors/warnings generated.
Am I missing another step?
AdView mAdView = (AdView) getView().findViewById(R.id.adView); // locate the id the banner view
// if you do not use fragments and you are using it directing the remove the getView();
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest); // load it..that's all and you will see logs on it
i took it from the link you posted, it has all you need.

AdMob Banner is shown behind a View?

Hi I am using AdMoB for showing banner ad in YouTube app which retrieves Video From Channel . For showing ad I used Java Code rather than XML. Now I am facing one problem which is my Banner is shown behind the Videos after the gets loaded. Can someone please help me to find out a way to show the ad front of the Videos. This is the code i used
adView = new AdView(this, AdSize.BANNER, "caXXXXXXXXXXXX");
FrameLayout layout = (FrameLayout)findViewById(R.id.content_frame);
layout.addView(adView);
#SuppressWarnings("deprecation")
FrameLayout.LayoutParams adsParams = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
adView.loadAd(new AdRequest());
Try this way.
This is the xml code of the banner, you can place it wherever you want in your activity xml layout:
<com.google.ads.AdView android:id="#+id/banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="YOUR UNIT ID HERE"
ads:adSize="BANNER"
ads:loadAdsOnCreate="true"/>
Then inside <application> tag in AndroidManifest.xml declare the AdActivity:
<activity
android:name="com.google.ads.AdActivity"
android:label="#string/app_name" >
And finnaly get the reference on your activity of banner and start request ads from google:
AdView ad = (AdView) findViewById(R.id.banner);
ad.loadAd(new AdRequest());
What layout are you using? To get ads on top of my Libgdx projects, I had to use a RelativeLayout and specify put things together in a specific order. Here's an example from my code:
// Create the main/content layout
RelativeLayout layout = new RelativeLayout(this);
// This is just a View that LibGDX draws into, it could be any View.
View gameView = initializeForView(new SuperAwesomeGameIMade(AndroidLauncher.this));
// Add it to the new content layout.
layout.addView(gameView);
// Create and setup the AdMob View
mAdView = new AdView(AndroidLauncher.this);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId("your ad unit here");
// Create the ad load request using the AdRequest Builder
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
adRequestBuilder.addTestDevice("your test device ID so you don't get banned!");
mAdView.setVisibility(View.GONE); // Change to View.Visible to show ads
mAdView.loadAd(adRequestBuilder.build());
// Setup the AdView with new layout params so it can "float" on the other one.
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(mAdView, adParams);
// Set the content view as this new relative layout
setContentView(layout);
Hope that gets you pointed in the right direction.

Problems adding new AdMob to android app

I am trying to add the new AdMob ads to my first application, I managed to arrange the code in the correct way but I have to problems:
First problem is my ad ID ca-app-pub-61413779example is getting multiple errors like: ca cannot be resolved to a variable, ba cannot be resolved to a variable, the literal
61413779 of type int is out of range.
The second problem is R.id.mainLayout, which is mainLayout, I don't get it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_screen);
// Create the adView
adView = new AdView(this, AdSize.BANNER, ca-app-pub-61413779example);
// Lookup your LinearLayout assuming it's been given
// the attribute android:id="#+id/mainLayout"
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
// Add the adView to it
layout.addView(adView);
adView.loadAd(new AdRequest());
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
I'm not familiar with your admob adding way, but I got another great way to add admob to your app using xml only.
first in your manifest add these permissions
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
* the second is most important
then add this in your manifest inside the application tag
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
now in every activity you want the ad to appear on you add this in the layout.xml of the activity
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="xxxxxxxxxxxx"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
android:layout_gravity="center"
android:layout_margin="10dp">
</com.google.ads.AdView>
and inside the top parent layout ex. linearlayout you add these under xmlns:android="xxx
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
now the ads should work perfectly :D
UPDATE: don't forget to download and place the GoogleAdMobAdsSdk-6.4.1.jar under the libs folder.
The AdView constructor expects a String as the third argument, so you're basically missing double quotes:
adView = new AdView(this, AdSize.BANNER, "ca-app-pub-61413779example");
As for your XML, ensure that you have the right ID. If only mainLayout is underlined, it is not the correct ID. If R is underlined, then it means your XML has an error somewhere and R.java isn't being generated.
since you set your content with activity_basic_screen.xml, you have to go to that xml.
you need to show that code for activity_basic_screen.xml and tell where you want to put the ad for specific details
for general, in your activity_basic_screen.xml, you have to have a LinearLayout or other layouts where you want to show/inflate the ad. for the one in your screenshot problem/Linearlayout, you have to have this one:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLayout"
>
you're getting that error because i think you don't have a linear layout with
android:id="#+id/mainLayout"
you can name it differently
android:id="#+id/givenName"
but you have to changed your reference in java of findViewById to "R.id.givenName"
for me, i usually use Ahmed's suggestion so i won't need to program it in java but i think programming in java is more dynamic.
hope that helps.
PS.
in your screenshot you cover your app name/project but you showed your package name. it's like the same thing because you can find your app in google play by using that.
This maybe a different answer to what you are looking for, but I would highly recommend using SMART_BANNER Over BANNER. look here at The table that list the standard banner sizes:
https://developers.google.com/admob/android/banner
Banner: size 320x50 only
Smart Banner: size change to Screen width x 32|50|90
Here is an example on how to do it:
-Layout
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_r"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.adexample">
<RelativeLayout
android:id="#+id/adView_test"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
-Activity:
public class MainActivity extends AppCompatActivity {
private AdView adView;
private AdRequest adRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
admobCall();
}
// Function to Set Ads for Main Layout
private void admobCall(){
// Set the RelativeLayout id to the view
View adContainer = findViewById(R.id.adView_test);
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
//Real Admob Main Activity Banner
adView.setAdUnitId(getString(R"YOUR_ADMOB_AD_ID"));
// Test google ID for Banner
// adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
((RelativeLayout)adContainer).addView(adView);
// This Request will bulid the ADs as a Test
//adRequest = new AdRequest.Builder()
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
// This Request Will bulid the ADs as Real
adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
Also be sure to add "google()" to the project-level build.gradle, and
this implementation to dependencies
implementation 'com.google.android.gms:play-services-ads:17.2.1'
Check the Implementation instructions site just to be sure you are not missing something:
https://developers.google.com/admob/android/quick-start
Hope this was helpful :)

AdMob banner above Tabs in TabsActivity

I am trying to set the AdMob banner ABOVE the Tabs in a TabsActivity:
public class FundsReportTabsActivity extends TabsActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdView adView = new AdView(activity, AdSize.BANNER, AD_UNIT_ID);
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="#+id/mainLayout"
// Add the adView to it
getTabHost().addView(adView, 0, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice("E0154BC3F11C778234254195FA1164F");
adRequest.setTesting(true);
// Initiate a generic request to load it with an ad
adView.loadAd(adRequest);
}
However, the banner does not appear, even though I have this logcat:
09-29 22:23:09.755: INFO/Ads(16158): onReceiveAd()
and don't see any related warning/error.
Does anyone have an idea? Thanks!
Check your logcat for a warning or verbose saying no ads in inventory. Some times when you first sign up or are using admob it takes some time before the ads start appearing. Also make sure you have provided a linear layout for the ad. And if you watch the logcat file during the activity with the ad you will see a content containing items that pertain to the ad. I know i had trouble with admob at first. The same issue you are having. It just took some time.

Empty space after AdMob disappear

I have layout structure:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.ads.AdView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
/>
<LinearLayout style="#style/TitleBar"
android:layout_width="fill_parent"
android:layout_height="45dip"
// title bar
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
// main layout with all needed elements and background!" >
</RelativeLayout>
</LinearLayout>
Everything look fine, until my AdMob disappered. Then I can see empty black region with admob size.
UPDATE: my screen shot:
normally I cas see here ad block, but when I get onFailedToReceiveAd(Ad request successful, but no ad returned due to lack of ad inventory.) ad disappers and my layout not fill all screen.
What you describe looks weird...
The reason I believed caused an ad to disappear was in case an ad is refreshed and then no ad is served due to lack of ad on AdMob side.
But from my own test, once an ad is loaded, if a subsequent ad refresh fails, the previous ad stays displayed, I haven't seen ads 'disappear'.
Maybe you could look at logcat and see if you get any errors there.
Here is some code I used to test Ad Request delivery/failure on my own app.
In case the blanks appears after an Ad fails loading, I suppose you could put some code inside the onFailedToReceiveAd to resize the AdView
AdView av = (AdView)findViewById(R.id.adView);
// Set AdListener
av.setAdListener(new AdListener() {
AdView av = (AdView)findViewById(R.id.adView);
#Override
public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
System.err.println("Ad failed: " + ad.toString() + error.toString());
av.setVisibility(AdView.GONE);//By setting visibility to GONE, you hide the AdView, but the AdView won't refresh automaticaly anymore.
}
#Override
public void onReceiveAd(Ad ad) {
System.out.println("Ad received: " + ad.toString());
av.setVisibility(AdView.VISIBLE);
}
});
// Create an ad request.
AdRequest adRequest = new AdRequest();
// Start loading the ad in the background.
av.loadAd(adRequest);
Just to confirm does your adView has this as height parameter?
android:layout_height="wrap_content"
Another way to look at this would be to set up your own 'house ad' in AdMob to target your app. Then when AdMob doesn't have an ad to serve it would display your own ad to 'fill the blank'.
Use the Set Visibility function to remove it from the layout.
SetVisibility()
Check this post also on hiding the AdView

Categories

Resources