How To Add Dynamic Ad To AdView In Admob Android? - android

This is My Code:
AdView mAdView;
MobileAds.initialize(this, "ca-app-pub-9233630887794553~4351962429");
// Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
// values/strings.xml.
mAdView = (AdView) findViewById(R.id.ad_view);
mAdView.setAdUnitId(getString(R.string.banner_ad_unit_id));
AdRequest adRequest = new AdRequest.Builder()
.build();
mAdView.loadAd(adRequest);

Check the 2 answers from this link: admob only sends test ad not real ads
Publish your app and live ads will automatically come.

Related

Is there any special way of implementing admob ads?

I am trying to add admob ads to my app by whenever i try the ads do not show but when i use the test ads it shows. I have also cheked my admob account it has been approved for displaying ads
I have tried using the admob test ads and it shows perfectly
Here is an example of the banner ad
mAdView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
I expect it to show my ads but it shows only the test ads whenever I replace my ad unit with the test ad unit.
Try to confirm that you have used valid banner ad id, If you still not getting real ad wait sometimes and test in another device. Hope it will work
-----------:Add custom layout:--------------
<!--Admob Banner Ad-->
<RelativeLayout
android:id="#+id/rlAdViewBannerAdmob"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
public AdView ad;
AdRequest adRequest;
//init Ad
ad =new AdView(context);
ad.setAdSize(AdSize.SMART_BANNER);
ad.setAdUnitId(adUnitId);
//Load ad
adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("14C739553C9657EB85559903949D428B") //add test Ids
.build();
//load ad
ad.loadAd(adRequest);
rlAdViewBannerAdmob.addView(ad); // add loaded ad in customview
//do not forget to add *YOUR_ADMOB_APP_ID* in manifest
<manifest>
<application>
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="YOUR_ADMOB_APP_ID"/>
</application>
</manifest>

AdMob No Live-Ads appearing

I have made my Application with Android Studio, and now i have a finished app.
I added an AdView to my Application, and used the TestAd-UnitId, and i got test ads. On my Emulator and also on my Mobile Phone. That worked, and i had Test Ads running. Then i created an AdMob account, and added my app in there, created a Bannner ad, like i had in my application, and used the adUnitId, from the AdMob page. But when i ran my Application on my phone i got no ads at all.
In case it matters: The app isn't on the Play Store.
I read that you have to wait several hours, until you get Live-Ads, but i have been waiting for over 12h, and im Still not getting any ads on my Phone.
If you need it, here is my Code:
My AdView:
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent" />
My onCreate Method:
MobileAds.initialize(getApplicationContext(), "ca-app-pub-hiddenhiddenhide~hiddenhide");
AdView ad = (AdView) findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder().build();
ad.loadAd(request);
You need to add this line to the builder.
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
so you need to have something like this:
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
If you want to use your own mobile phone. you can look for the id in the logcat that needs to be added it will look something like this:
AdRequest request = new AdRequest.Builder()
.addTestDevice("here goes your id of your phone")
.build();
The reason why you need to do this is because when you are developing you can't see adds because the app is not placed in the playstore.
As long as he isn't in the playstore you won't see ads. You can create test-ads with the code is just provided
If you go to production try using this code: (it wil use a testdevice for debugging and developing but it will use real adds for your device as well when in production )
AdRequest adRequest = new AdRequest.Builder()
.build();
if (BuildConfig.DEBUG)
adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("your id goes here")
.build();

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.

Track Admob Event in Google Analytics

I want to track clicks on an AdMob banners with Google Analytics, but a problem occurs and I don't understand why.
Currently, my AdMob banner is implemented like this:
Layout:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="YOUR_AD_UNIT_ID"
ads:adSize="BANNER"/>
Java : AdView adView = (AdView)this.findViewById(R.id.adView);
However, the Google demonstration project which shows how to add an AdListener (project available here) does not specify anything in the layout and use the following code to add the banner:
LinearLayout layout = (LinearLayout) findViewById(R.id.leLinearLayoutDeMonChoix);
layout.addView(adView);
But if the implementation described at the beginning is used, the AdListener does not detect any event anymore. Why?
You can find this defective implementation in the following demonstration project: https://drive.google.com/file/d/0B8rE1pbtzNJ1UXg5QllubEFidGc/edit?usp=sharing
I thank you in advance for your time and help.
In the provided implementation you are doing the following:
// Create an ad.
adView = new AdView(this);
// Set the AdListener.
adView.setAdListener(new AdListener() {
/** stuff **/
}
AdView adView = (AdView)this.findViewById(R.id.adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
There is two instances of AdView, and this is your problem.
first one created from inflating your main.xml layout by setContentView(),
second one when you wrote adView = new AdView(this);
You set your listener on the second one, but only the first one is displayed.
It cannot work. :)
Choose one method (create it from layout) or the other (create it from code), but don't mix them up.
If you want to create your ad from layout, do the following:
// Retreive the adView.
AdView adView = (AdView)this.findViewById(R.id.adView);
// Set the AdListener.
adView.setAdListener(new AdListener() {
/** stuff **/
}
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);

Android AdMob cannot set into testing mod

the line adView.setTesting(true); gives e a error saying the method does not exits,
code blocksetTesting
adView = new AdView(this, AdSize.BANNER, "a14e10cb6b18825");
ad = new AdView(this, AdSize.BANNER, "a14e10cb6b18825");
// 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);
// Initiate a generic request to load it with an ad
adView.setTesting(true);
adView.loadAd(new AdRequest());
I'm unfamiliar with the setTesting() api call. In my AdMob code, I use
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(MY_TEST_DEVICE_ID);
adView.loadAd(adRequest);
When I run the code and I don't specify the test device I usually see a log message in the LOGCAT telling me to add my device id using the addTestDevice api

Categories

Resources