Android AdMob banner ad not showing up - android

I am using the following view in my activity xml.
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-6844894412719021~8904567900">
</com.google.android.gms.ads.AdView>

Your adUnitId seems to be incorrect. Did you get it via email?
You should be using following app id (replace ~ with /)
ads:adUnitId="ca-app-pub-6844894412719021/8904567900">

Try to add to your xml:
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
and to gradle:
compile 'com.google.android.gms:play-services-ads:9.8.0'

It looks like you are using app_id instead of banner_id.
<string name="app_id">ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx</string>
<string name="banner_id">ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx</string>
these two values are different from each other.

Check if you have latest versions of adMob and GooglePlayServices lib.

When you try to show banner inside of svg images then care about it are you using smart banner or banner it may effect, sometimes you implement the code right but when you try to show toast in ad loaded like this.
adView2.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Toast.makeText(App_Mainpage.this, "add is loaded", Toast.LENGTH_SHORT).show();
adView2.setVisibility(View.VISIBLE);
}
#Override
public void onAdFailedToLoad(int errorCode) {
}
#Override
public void onAdOpened() {
}
#Override
public void onAdLeftApplication() {
}
#Override
public void onAdClosed() {
}
});
AdRequest adRequest = new AdRequest.Builder().build();
adView2.loadAd(adRequest);
if it shows you in toast the ad is loaded but not showing when you testing, then actually you are implementing Smart_banner that takes large space to show, so use Banner Just it will show then, i solve it by change this thing.
then i changed to smart banner .

Related

Android AdView not showing

I'm having issues with ADs in my Android App.
Sometimes the AD is not shown, probably depending on the DPI or Size of the screen's device.
In my code, I implemented a check to set View.GONE in case the AD fails to load, but despite that on some devices I still have blank spaces where it's supposed to be the AD
For example, here one of my ADs
if(holder.nativeExpressAdView != null) {
AdRequest request = new AdRequest.Builder()
.build();
holder.nativeExpressAdView.loadAd(request);
Utilities.setAdListenerNative(holder.nativeExpressAdView);
}
//ADs listener to remove view if it fails load
//NATIVE ADS
public static void setAdListenerNative(final NativeExpressAdView nativeExpressAdView) {
nativeExpressAdView.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
nativeExpressAdView.setVisibility(View.GONE);
}
});
}
First show me your log
Second Dont use "Gone" because this will change the position of the adView if you are using other sizes of Android scale you will see that.
Third use a debugger.
Use "invisible"

Failed to load Native ad: 0 Error

I am implementing code for native ads that is given by google. That is working okay with google
ad unit id ADMOB_AD_UNIT_ID = "ca-app-pub-3940256099942544/2247696110"
But when I replace with my id its not showing ad and execute onAdFailedToLoad method and got Failed to load native ad: 0 error .
I tried two different ad unit id but it didn't load ad.
so what should I do??please help me
I tried below code
final LayoutInflater li = (LayoutInflater) appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AdLoader.Builder builder = new AdLoader.Builder(appContext, Global.ADMOB_AD_UNIT_ID);
builder.forContentAd(new NativeContentAd.OnContentAdLoadedListener() {
#Override
public void onContentAdLoaded(NativeContentAd ad) {
//viewHolder.rlnativead.setVisibility(View.VISIBLE);
NativeContentAdView adView = (NativeContentAdView)li
.inflate(R.layout.ad_content, null);
populateContentAdView(ad, adView);
viewHolder.fl_adplaceholder.removeAllViews();
viewHolder.fl_adplaceholder.addView(adView);
}
});
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
// Toast.makeText(appContext, "Failed to load native ad: "
// + errorCode, Toast.LENGTH_SHORT).show();
Log.e("Call List adapter","Failed to load native ad: "+errorCode);
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
Please add at least one testing device and try, It may be help you like following,
AdRequest adRequest = new AdRequest.Builder() .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID") .build();
this could be lately answered but expecting to help for others having similiar problem.One of the major cause for this is incorrect dimension
In my case before it was
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.NativeExpressAdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="#string/native_ad_id"
ads:adSize="320x250"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto">
</com.google.android.gms.ads.NativeExpressAdView>
I was making the mistake in giving the adSize ,so i just changed the add size with the below dimension and got fixed. Thanks.
ads:adSize="320x250"

Implementing native ads in android using Admob? is it possible?

I'm trying to implement native ads in my android application. But I want to do it using admob only. I searched a lot for solution but could not find exact one to do so.
I know it is possible using MoPub.
What I wanted to do is this:
Show ads inside list item which means one of the ListView/RecyclerView item can be one ad like below image.
I found some links and references but that doesn't explain proper implementation of the native ads.
Link 1 : Native ads overview
Link 2 : DFP Android Guides > Targeting
Link 3 : DFP Quick Start Guide
If it is not possible to do it using admob, MoPub is best solution for me right now.
Any help and guidance would be helpful. thanks.
Recently I stucked with the same question. Then I decided to post my solution for that to admobadapter. Hope it will help you.
The basic usage could look like:
ListView lvMessages;
AdmobAdapterWrapper adapterWrapper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initListViewItems();
}
/**
* Inits an adapter with items, wrapping your adapter with a {#link AdmobAdapterWrapper} and setting the listview to this wrapper
* FIRST OF ALL Please notice that the following code will work on a real devices but emulator!
*/
private void initListViewItems() {
lvMessages = (ListView) findViewById(R.id.lvMessages);
//creating your adapter, it could be a custom adapter as well
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
adapterWrapper = new AdmobAdapterWrapper(this);
adapterWrapper.setAdapter(adapter); //wrapping your adapter with a AdmobAdapterWrapper.
//here you can use the following string to set your custom layouts for a different types of native ads
//adapterWrapper.setInstallAdsLayoutId(R.layout.your_installad_layout);
//adapterWrapper.setcontentAdsLayoutId(R.layout.your_installad_layout);
//Sets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
adapterWrapper.setLimitOfAds(3);
//Sets the number of your data items between ad blocks, by default it equals to 10.
//You should set it according to the Admob's policies and rules which says not to
//display more than one ad block at the visible part of the screen,
// so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
adapterWrapper.setNoOfDataBetweenAds(10);
//It's a test admob ID. Please replace it with a real one only when you will be ready to deploy your product to the Release!
//Otherwise your Admob account could be banned
//String admobUnitId = getResources().getString(R.string.banner_admob_unit_id);
//adapterWrapper.setAdmobReleaseUnitId(admobUnitId);
lvMessages.setAdapter(adapterWrapper); // setting an AdmobAdapterWrapper to a ListView
//preparing the collection of data
final String sItem = "item #";
ArrayList<String> lst = new ArrayList<String>(100);
for(int i=1;i<=100;i++)
lst.add(sItem.concat(Integer.toString(i)));
//adding a collection of data to your adapter and rising the data set changed event
adapter.addAll(lst);
adapter.notifyDataSetChanged();
}
And the result will look like the following
Try using some other ad networking where it provides different types of native ads. Developers can customize the ads where to place and use it. For eg: if you need to place ads at second cell every 15 row, you can use like this.
Avocarrot provides that.
AvocarrotInstream myAd = new AvocarrotInstream(<yourListAdapter>);
myAd.initWithKey( "<your API Key>" );
myAd.setSandbox(true);
myAd.setLogger(true ,"ALL");
// Populate with In-Stream ads
myAd.loadAdForPlacement(this, "<your Placement Name>" );
// Bind the adapter to your list view component
<yourListView>.setAdapter(myAd);// here you are integrating ads to listview
myAd.setFrequency(2,15); // every 15 cells starting from the 2nd cell.
Here is Documentation it provides List ads and Feed ads.
Native ads are included in Google Play Services along with the rest of DFP/AdMob advertising. Make sure you have the following listed as dependencies in your build.gradle (note that 7.5.0 is the highest version as of this posting).
compile "com.google.android.gms:play-services-base:7.5.0"
compile "com.google.android.gms:play-services-ads:7.5.0"
Then you can display native ads
AdLoader adLoader = new AdLoader.Builder(context, "/6499/example/native")
.forAppInstallAd(new OnAppInstallAdLoadedListener() {
#Override
public void onAppInstallAdLoaded(NativeAppInstallAd appInstallAd) {
// Show the app install ad.
}
})
.forContentAd(new OnContentAdLoadedListener() {
#Override
public void onContentAdLoaded(NativeContentAd contentAd) {
// Show the content ad.
}
})
.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
// Handle the failure by logging, altering the UI, etc.
}
})
.withNativeAdOptions(new NativeAdOptions.Builder()
// Methods in the NativeAdOptions.Builder class can be
// used here to specify individual options settings.
.build())
.build();
Click here for complete documentation.
As an addition to this thread, you can now implement NativeAds for Admob very easily by following the guide provided by Google using NativeExpressAdView .
For more information, check the google documentation :
https://firebase.google.com/docs/admob/android/native-express?hl=en
Add this code to your Listview Adapter
builder.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
#Override
public void onAppInstallAdLoaded(NativeAppInstallAd ad) {
FrameLayout frameLayout =
(FrameLayout) findViewById(R.id.fl_adplaceholder);
NativeAppInstallAdView adView = (NativeAppInstallAdView) getLayoutInflater()
.inflate(R.layout.ad_app_install, null);
populateAppInstallAdView(ad, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(MainActivity.this, "Failed to load native ad: "
+ errorCode, Toast.LENGTH_SHORT).show();
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
Make some changes for listview Adapter and you will get populateAppInstallAdView() method from below link
Everything is covered in this example please go through this
https://github.com/googleads/googleads-mobile-android-examples/tree/master/admob
Adding to the mix, Tooleap Ads SDK provides a simple way to implement Admob's native ads.
Instead of requiring you to use the traditional listView adapter and showing the ad inside you content, they are showing admob native ads as a small floating bubble. When pressing on it you can see the full native ad.
Here is an example of using their SDK inside your activity class:
BubbleImageAd = new BubbleImageAd(this);
bubbleImageAd.setAdUnitId("YOUR_AD_UNIT_ID");
bubbleImageAd.loadAndShowAd(this);
You can check them out here.
This is working for me:
Copy Native template in project library.
in Build.gradle(Module:Appname) - add this:
implementation project(':nativetemplates')
implementation 'com.google.android.gms:play-services-ads:20.4.0'
in Setting.gradle(Appname) - add this:
include ':nativetemplates'
in Mainfest - update as earlier.
Before Application:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
add this Permission:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
In Layout file add this:
<com.google.android.ads.nativetemplates.TemplateView
android:id="#+id/my_template"
app:gnt_template_type="#layout/gnt_medium_template_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.google.android.ads.nativetemplates.TemplateView>
In program file:
MobileAds.initialize(this)
//build ad
val adLoader = AdLoader.Builder(this,"ca-app-pub-3940256099942544/2247696110")
.forNativeAd {
val style = NativeTemplateStyle.Builder().withMainBackgroundColor(ColorDrawable(Color.WHITE))
.build()
val template = findViewById<TemplateView>(R.id.my_template)
template.setStyles(style)
template.setNativeAd(it)
}.build()
//show ad
adLoader.loadAd(AdRequest.Builder().build())
If still not loading, Check with your admod credentials.
Well, this thread is probably outdated. But starting May 2015, and as of now, AdMob does support native ads (still in beta though).
https://support.google.com/admob/answer/6239795
Also it's available only to a limited number of developers as of its beta stage.
Admob in your android these are the codes needed.
<com.google.android.gms.ads.AdView
android:layout_alignParentBottom="true"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-4549020480017205/6066702579"
/>
//In your java class file
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
⟩⟩ In project structure, navigate to activity_main.xml and paste the following code in your layout.
<com.google.android.gms.ads.NativeExpressAdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="320x300"
ads:adUnitId="#string/ad_unit_id">
</com.google.android.gms.ads.NativeExpressAdView>
and in the same file i.e activity_main.xml add below lines of code in the header part
xmlns:ads="http://schemas.android.com/apk/res-auto"
⟩⟩ Now open MainActivity.java and add below lines of code in public class
private static String LOG_TAG = "EXAMPLE";
NativeExpressAdView mAdView;
VideoController mVideoController;
⟩⟩ Then under MainActivity.java and add below lines of code in onCreate() method.
// Locate the NativeExpressAdView.
mAdView = (NativeExpressAdView) findViewById(R.id.adView);
// Set its video options.
mAdView.setVideoOptions(new VideoOptions.Builder()
.setStartMuted(true)
.build());
// The VideoController can be used to get lifecycle events and info about an ad's video
// asset. One will always be returned by getVideoController, even if the ad has no video
// asset.
mVideoController = mAdView.getVideoController();
mVideoController.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
#Override
public void onVideoEnd() {
Log.d(LOG_TAG, "Video playback is finished.");
super.onVideoEnd();
}
});
// Set an AdListener for the AdView, so the Activity can take action when an ad has finished
// loading.
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
if (mVideoController.hasVideoContent()) {
Log.d(LOG_TAG, "Received an ad that contains a video asset.");
} else {
Log.d(LOG_TAG, "Received an ad that does not contain a video asset.");
}
}
});
mAdView.loadAd(new AdRequest.Builder().build());
⟩⟩ Now open string.xml file in values folder and paste the below line of code.
<string name="ad_unit_id">ca-app-pub-39402560999xxxxx/21772xxxxx</string>
⟩⟩ Then open the Manifest file and add Internet permission to it.
<uses-permission android:name="android.permission.INTERNET" />
THE RESOURCE : How to insert AdMob Native Ad in your Android App
It is restricted to selected publishers at the moment. You'll need to contact a Google account manager in your region for implementation.
Yes it is possible you can use below code in xml file
<com.google.android.gms.ads.NativeExpressAdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="320x300"
ads:adUnitId="#string/ad_unit_id">
mAdView.setVideoOptions(new VideoOptions.Builder()
.setStartMuted(true)
.build());
mVideoController = mAdView.getVideoController();
mVideoController.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
#Override
public void onVideoEnd() {
Log.d(LOG_TAG, "Video playback is finished.");
super.onVideoEnd();
}
});
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
if (mVideoController.hasVideoContent()) {
Log.d(LOG_TAG, "Received an ad that contains a video asset.");
} else {
Log.d(LOG_TAG, "Received an ad that does not contain a video asset.");
}
}
});
mAdView.loadAd(new AdRequest.Builder().build());
To add Native Templates in our app we have to follow some basic steps:
1-First, we have to download the Native Templates, so got to developers.google
2-Then click on the Download Native Templates, now you will be directed to Github
3-Then download the zip file from GitHub and extract the zip file to any folder and remember the location of the folder, we will use it later
4-Now go inside Android studio and click on File->New->Import Module, Now you will see a new window (Import-Module from Source) Now click on the Browse icon and select the nativetemplates folder and click on finish and wait for Gradle build to finish.
5-Now open the Gradle Scripts->build.gradle (Module: app) section and import the nativetemplates project and click the "sync Now" show at the top as shown below:
//adding native templates
implementation project(':nativetemplates')
https://www.studytonight.com/post/how-to-add-admob-native-ad-in-android-app#

Android: how to integrate admob in app?

I was trying to integrate admob banner in my application several hours, but I couldn't do that, so I created new app the only purpose of which is show admob banner but it doesn't work too. here is code
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdView adView = new AdView(this, AdSize.BANNER, "XXX");
adView.setVisibility(View.VISIBLE);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(adView, adParams);
setContentView(layout);
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
and here is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zzzzzz_admobtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.zzzzzz_admobtest.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>
what am I doing wrong?
Also, when I output adView.isActivated() and adView.isReady() they both are false.
I am using "XXX" instead of ID because I don't have one. Could it be a problem?
I am using device. Here is errors on ligcat
08-07 22:43:04.248: E/ActivityThread(24181): Failed to find provider info for com.google.plus.platform
08-07 22:43:04.454: E/Ads(24181): JS: Uncaught ReferenceError: AFMA_getSdkConstants is not defined (http://media.admob.com/:1)
08-07 22:43:04.454: E/Web Console(24181): Uncaught ReferenceError: AFMA_getSdkConstants is not defined at http://media.admob.com/:1
the last 2 errors could be fixed by this code
(new Thread() {
public void run() {
Looper.prepare();
adView.loadAd(new AdRequest());
}
}).start();
as I understand the first one is not an error but just a warning.
so why this isn't work??
I am using "XXX" instead of ID because I don't have one. Could it be a
problem?
Yes. Absolutely yes. Sign up for an Admob account. Get yourself an ID and use that.
Meta-Data Tag is missing inside the Android-manifest.look into the already available resources Example
<meta-data
android:name="ADMOB_PUBLISHER_ID"
android:value="XXXX" />
This question is kinda old, but I want to answer it, because new technology appeared, which lets AdMob integration really easy. You don't need to code anything additionally in most cases, just create and .apk file and Admob will be injected into it automatically.
Enter FGL Enhance. Select, which SDK to integrate. You can select multiple various SDKs using this service, for this tutorial we'll pick only Admob from "I want to choose SDK myself".
Actually you can integrate more than 20 SDKs available by just selecting their checkbox. This list includes various ads providers, Crash reporting, Dolby Audio, etc.
Select, what development technology was used to build your app. If you want to show/hide the banner, you'll need to connect the lightweight drag-and-drop library and add 1 line of code. Depending on the technology you selected you'll be given a code sample and connector library download link.
But if you just want the banner to be present on your screen all the time, which is the easiest solution, you don't need to change your game code! This solution is called ZeroCode and it works for any development technology: event for the .apk and .ipa, which you've got from an HTML5-game or from Game Maker.
On this page you can select the ads types which will be shown in your app.
Interstitial - ads between the levels
Rewarded Video - videos, with a reward for viewing
Flexible Banner - banner, which can be hidden/shown in the game code
Persistent Banner - banner, which will stay on the screen all the time (but banner rotation is available, from AdMob settings)
Pre-Roll - ads, shown as the app starts (like in ketchapp's games)
Two last types are marked as ZeroCode. You don't need to code anything in your game to have them integrated. For our sample we'll select just a Persistent banner.
Now upload our app.
On the next screen we can select a banner type. We can use, for example SMART_BANNER or any other banner type from AdMob help. We'll also input the banner id from our AdMob account. When creating a banner in AdMob account, we can set refresh interval.
The final step - signing method selection. You ca use your own developer's certificate, then you'll be able to submit the game to the store right after the AdMob has been injected. You can select a testing certificate to test the app on your own device. Or you can download an unsigned app and sign it locally before submitting to the store.
One more progress bar...
And here we are!
The Enhance service is completely free for the developers. You'll receive the same amount of money from ads as if you'd integrated the ads manually. FGL receives the income from the SDK providers, because, in fact, FGL Enhance makes SDKs more attractive for the new developers.
source: http://www.airapport.com/2016/09/tutorial-how-to-quickly-integrate-admob.html
how to integrate AdMob and Facebook banner ads into Android applications
Build.Gradle :
implementation 'com.google.android.gms:play-services-ads:19.1.0'
implementation 'com.facebook.android:audience-network-sdk:5.+'
MyAplication.java:
AudienceNetworkAds.initialize(this);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
Manifest.xml:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="App_ID_Here.."/> //Test App ID For Testing Purpose.
<activity
android:name="com.facebook.ads.AudienceNetworkActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
Permission:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
public class MainActivity extends AppCompatActivity {
private LinearLayout bannerView;
//Banner
private AdView facebookAdView;
private com.google.android.gms.ads.AdView admobAdView;
//Interstitial
private InterstitialAd facebookInterstitialAds;
private com.google.android.gms.ads.InterstitialAd adMobInterstitialAds;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bannerView = (LinearLayout) findViewById(R.id.bannerView);
//Load Banner Ads
showFacebookBannerAds();
//Load Interstitial Ads
showFacebookInterstitial();
}
private void showFacebookBannerAds() {
facebookAdView = new AdView(this, "Facebook_Banner_ID", AdSize.BANNER_HEIGHT_50);
bannerView.addView(facebookAdView);
facebookAdView.setAdListener(new AdListener() {
#Override
public void onError(Ad ad, AdError adError) {
//If Load Fail then
showAdMobBanner();
}
#Override
public void onAdLoaded(Ad ad) {
}
#Override
public void onAdClicked(Ad ad) {
}
#Override
public void onLoggingImpression(Ad ad) {
}
});
facebookAdView.loadAd();
}
private void showAdMobBanner() {
admobAdView = new com.google.android.gms.ads.AdView(this);
admobAdView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
admobAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
bannerView.addView(admobAdView);
admobAdView.loadAd(new AdRequest.Builder().build());
}
//Interstitial Ads
private void showFacebookInterstitial() {
facebookInterstitialAds = new InterstitialAd(this, "Interstitial_Ad_ID");
facebookInterstitialAds.setAdListener(new InterstitialAdListener() {
#Override
public void onInterstitialDisplayed(Ad ad) {
}
#Override
public void onInterstitialDismissed(Ad ad) {
//any work on ads close
finish();
}
#Override
public void onError(Ad ad, AdError adError) {
//if load fail then
loadAdMobInterstitial();
}
#Override
public void onAdLoaded(Ad ad) {
}
#Override
public void onAdClicked(Ad ad) {
}
#Override
public void onLoggingImpression(Ad ad) {
}
});
facebookInterstitialAds.loadAd();
}
private void loadAdMobInterstitial() {
adMobInterstitialAds = new com.google.android.gms.ads.InterstitialAd(this);
adMobInterstitialAds.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
adMobInterstitialAds.loadAd(new AdRequest.Builder().build());
adMobInterstitialAds.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
super.onAdClosed();
//any work on ads close
finish();
}
});
}
//Interstitial Ads show on button click listener
public void showAds(View view){
if (facebookInterstitialAds.isAdLoaded())
facebookInterstitialAds.show();
else if (adMobInterstitialAds!=null)
if (adMobInterstitialAds.isLoaded())
adMobInterstitialAds.show();
}
#Override
protected void onPause() {
super.onPause();
if (admobAdView != null)
admobAdView.pause();
}
#Override
protected void onResume() {
super.onResume();
if (admobAdView != null)
admobAdView.resume();
}
#Override
protected void onDestroy() {
super.onDestroy();
if (facebookAdView != null)
facebookAdView.destroy();
if (admobAdView != null)
admobAdView.destroy();
if (facebookInterstitialAds != null)
facebookInterstitialAds.destroy();
}
}
Add Just two line in your onCreate
AdView ad = (AdView) findViewById(R.id.adView);
ad.loadAd(new AdRequest());
And add AddView in Your XML like
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/ads_unit_id"
ads:loadAdsOnCreate="true" />
You can download complete source code from here
Add following lib in your dependency
compile 'com.google.android.gms:play-services-ads:7.8.0'
For Smart Banner
Add following things in xml file
<com.google.android.gms.ads.AdView
android:id="#+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/banner_ad_unit_id" />
Add following things in java file
mAdView = (AdView) findViewById(R.id.ad_view);
AdRequest adRequest = new AdRequest.Builder().build();
// Start loading the ad in the background.
mAdView.loadAd(adRequest);
For Interstitial Advertise
mInterstitialAd = new InterstitialAd(MainActivity.this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.full_screen_ad_unit_id));
AdRequest adRequestFull = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequestFull);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
// Full screen advertise will show only after loading complete
mInterstitialAd.show();
}
});

How to know when someone has clicked on an Adview?

I'm using an adview in my application and want to know when someone has clicked that Adview!
How I can do that?
The onclicklistener does not seem to work.
You can set the listener this way:
adView.setAdListener(this);
And then implement this:
#Override
public void onPresentScreen(Ad arg0) {
//OnClick here
}
The click is handled inside of the AdMob API, and I don't think there is a way to do what you are asking with the API as its against the TOS to use a "unlock feature by clicking X number of ads" model. For that reason I don't believe it is available in the API where the onclicklistener is.
Try this
mAdView.setAdListener(new AdListener() {
#Override
public void onAdOpened() {
super.onAdOpened();
}
});

Categories

Resources