My application does not appear admob ads - android

In my application AdMob ads do not appear please help and here is my code.
thank you.
code:
public class ViewHolderAdMob extends RecyclerView.ViewHolder {
public AdView mAdView;
public ViewHolderAdMob(View view) {
super(view);
MobileAds.initialize(getContext(),"ca-app-pub-3790716330382724~3051722333");
mAdView = (AdView) view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
mAdView.loadAd(adRequest);
}
}

Related

Unable to show Admob ads in android activity

I am trying to implement Admob ads in android activity but after everything done as per instruction given on firebase-Admob ads integration guide there is problem in showing ads in activity.What I have done so far is:
AndroidManifest.XML
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="MY_APP_ID" />
XML
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/ad_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="MY_ADUNIT_ID">
</com.google.android.gms.ads.AdView>
Java
public class Ad extends AppCompatActivity {
AdView ad_View;
AdRequest adRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ad);
MobileAds.initialize(this,#string/appid);
ad_View = findViewById(R.id.adView);
ad_View = new AdView(this);
ad_View.setAdSize(AdSize.SMART_BANNER);
ad_View.setAdUnitId(#string/adunit);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
adRequest = new AdRequest.Builder().build();
ad_View.loadAd(adRequest);
}
}
Someone please let me know what I am doing. Any help would be appreciated.
THANKS
Please try to remove this lines from your code,
ad_View = new AdView(this);
ad_View.setAdSize(AdSize.SMART_BANNER);
ad_View.setAdUnitId(#string/adunit);
because you have already set in XML
So try this,
public class Ad extends AppCompatActivity {
AdView ad_View;
AdRequest adRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ad);
MobileAds.initialize(this,#string/appid);
ad_View = findViewById(R.id.adView);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
adRequest = new AdRequest.Builder().build();
ad_View.loadAd(adRequest);
}
}
Note : Please make sure to add Internet Permission in AndroidManifest.xml

NullObjectReference for Admob in different activities

I had read a few q&a in stackoverflow, but still not able to get it work.
Admob on Multiple Activities?
Similar to above solutions, I got the below error
android.content.res.Resources android.content.Context.getResources()' on a null object reference
in this line
AdView adView = new AdView(this);
Can anyone find out why it happens?
CommonCode.java
public class CommonCode extends AppCompatActivity{
//private AdView mAdView;
public void createAdview(){
AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CommonCode cc = new CommonCode();
cc.createAdview();}}
You extends AppCompatActivity in CommonCode that define CommonCode as an Activity.Activity context is null untill it start.
You need to remove extnds AppCompatActivity from CommonCode class and pass the context in a parameter as below :
public class CommonCode{
public void createAdview(Context context){
AdView adView = new AdView(context);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
Now in a MainActivity you need to call the method like this :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CommonCode cc = new CommonCode();
cc.createAdview(this);
}
}
You need to pass activity context in this line
AdView adView = new AdView(this);
Create common class like this
public class CommonCode {
public CommonCode (){
}
public void createAdview(Context context){
AdView adView = new AdView(context);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}}
And use it like this in your activity
CommonCode cc = new CommonCode();
cc.createAdview(this);

Unable to display ads ErrorCode: 0 in ads android

I integrate google admob ads but it's displaying an error as,
W/Ads: There was a problem getting an ad response. ErrorCode: 0
08-09 16:05:43.933 24002-24002/com.locationtracking W/Ads: Failed to load ad: 0
I created a new ad id and copied the interstital ad unit id and app id but it's not working.
But when I used testing interstial id, it's displaying an ad. But when i copy and use the interstital id created, it's not displaying the ads. For past few days, I am struggling in this. I created new account and checked. Still same error.
MobileAds.initialize(this, "ca-app-pub-9172823052987868~3234462570");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-9172823052987868/8576265071");
AdRequest adRequestInterstitial = new AdRequest.Builder().addTestDevice("deviceid").build();
mInterstitialAd.loadAd(adRequestInterstitial);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
}
#Override
public void onAdLoaded() {
mAdIsLoading = false;
showInterstitial();
}
#Override
public void onAdFailedToLoad(int errorCode) {
mAdIsLoading = false;
}
});
}
private void showInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
AdRequest adRequest = new AdRequest.Builder().addTestDevice("deviceid").build();
mInterstitialAd.loadAd(adRequest);
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services-ads:9.2.1'
compile 'com.google.android.gms:play-services-analytics:9.2.1'
}
Common Error! There is no fault in your code. It's all good.
You just need to wait a little. Your ad id is newly created so it will take some time to fetch ads from google servers. You can verify this by adding banner/interstitial ad id you creating for earlier applications and you'll see that they work. So give it some time and it will work soon. At least for me, it happens all the time.
W/Ads: There was a problem getting an ad response. ErrorCode: 0 08-09 16:05:43.933 24002-24002/com.locationtracking W/Ads: Failed to load ad: 0
remove addTestDevice("deviceid"); from your code display real ads
AdRequest adRequestInterstitial = new AdRequest.Builder().addTestDevice("deviceid").build();
to
AdRequest adRequestInterstitial = new AdRequest.Builder().build();
try bellow code its running well:
public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
////////////////////////// banner/////////////////////////
AdView mAdView = (AdView) findViewById(R.id.adView);
//AdRequest adRequest = new AdRequest.Builder().addTestDevice("7339C0A9091FA2D6AA9A2BF29077B5EA").build();
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
///////////////////////////////////////////////////////////
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_unit_id));
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
loadAdd();
}
});
loadAdd();
}
#Override
protected void onStart() {
super.onStart();
showInterstitial();
}
#Override
protected void onStop() {
super.onStop();
showInterstitial();
}
//////////////////////////////////add Interstitial section//////////////////////////////////////////////
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and restart the game.
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
//Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
loadAdd();
}
}
private void loadAdd() {
// Request a new ad if one isn't already loaded, hide the button, and kick off the timer.
if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
//AdRequest adRequest = new AdRequest.Builder().addTestDevice("7339C0A9091FA2D6AA9A2BF29077B5EA").build();
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
compile 'com.google.android.gms:play-services-ads:8.3.0'
You can not display real ad in the test device, so you should remove the test device code when you upload your apk=> change your code as this
AdRequest adRequestInterstitial = new AdRequest.Builder().build();

Android InterstitalAd gray small bar bevor the ad

Im trying to get a InterstitialAd into my application.
It works now,
However:
1) I always get a small gray bar displayed on top of the screen during the ad is loading
2) The ad takes really long to load
any ideas how to fix these two problems?
BannerActivity:
public class BannerActivity extends Activity{
private InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-XXXXXX");
requestNewInterstitial();
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded(){
mInterstitialAd.show();
}
#Override
public void onAdClosed() {
finish();
}
});
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().addTestDevice("XXXXXXXXXXXX").build();
mInterstitialAd.loadAd(adRequest);
}
}
How i load the banner:
Intent bannerIntent = new Intent(myContext, BannerActivity.class);
myContext.startActivity(bannerIntent);
xml:
android:name="kletzgames.crazyballs.BannerActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
I fixed it:
I now call the Ad during another View so it can load, and ones it loaded it will be displayed during screenTransaction.
AdRequest adRequest = new AdRequest.Builder().addTestDevice("XXXX").build();
mInterstitialAd.loadAd(adRequest);
** some if's **
mInterstitialAd.show();

How to specify adUnitId programmatically for AdMob?

I'm trying to set adUnitId programmatically to ads from the new Google Play services (old AdMob).
I have this in XML (used in an <include>):
<com.google.android.gms.ads.AdView
xmlns:android="http://schemas.android.com/apk/res/android"
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="BANNER"/>
and this in onCreate():
AdView mAdview = (AdView)findViewById(R.id.adView);
mAdview.setAdUnitId(((App)getApplication()).getAdmobKey());
mAdview.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
findViewById(R.id.adView).setVisibility(View.VISIBLE);
}
});
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mAdview.loadAd(adRequest);
And I get:
The ad size and ad unit ID must be set before loadAd is called.
So the second option was to make the ad programmatically.
The new XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/adView"
/>
The new code:
AdView mAdview = new AdView(this);
...
((LinearLayout)findViewById(R.id.adView)).addView(mAdview);
mAdview.loadAd(adRequest);
But I get the same error.
I tried also to inherit from com.google.android.gms.ads.AdView to make a custom view, but it's final.
Any suggestion?
The method loadAd() checks if (mAdView.getAdSize() == null || mAdView.getAdUnitId() == null) when loadAd happens.
Try logging the boolean output of (mAdView.getAdSize() == null || mAdView.getAdUnitId() == null) before calling loadAd to determine its state:
mAdView = new AdView(this);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
if(mAdView.getAdSize() != null || mAdView.getAdUnitId() != null)
mAdView.loadAd(adRequest);
// else Log state of adsize/adunit
((LinearLayout)findViewById(R.id.adView)).addView(mAdview);
If you using ConstraintLayout or CoordinatorLayout solution here
For ConstraintLayout
private AdView mAdView;
private AdRequest mAdRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addAdView();
initAd();
}
private void addAdView() {
ConstraintLayout constraintLayout = findViewById(R.id.constraint_layout);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
mAdView = new AdView(this);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(getString(R.string.banner1_id));
mAdView.setId(View.generateViewId());
constraintLayout.addView(mAdView);
constraintSet.connect(mAdView.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
constraintSet.connect(mAdView.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);
constraintSet.connect(mAdView.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0);
constraintSet.constrainHeight(mAdView.getId(), ConstraintSet.WRAP_CONTENT);
constraintSet.constrainWidth(mAdView.getId(), ConstraintSet.WRAP_CONTENT);
constraintSet.applyTo(constraintLayout);
}
private void initAd() {
MobileAds.initialize(this, initializationStatus -> {
});
initBanner();
}
private void initBanner() {
mAdRequest = new AdRequest.Builder().build();
mAdView.loadAd(mAdRequest);
mAdView.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
}
#Override
public void onAdOpened() {
super.onAdOpened();
}
});
}
For CoordinatorLayout
private AdView mAdView;
private AdRequest mAdRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addAdView();
initAd();
}
private void addAdView() {
CoordinatorLayout coordinatorLayout = findViewById(R.id.coordinator_layout);
mAdView = new AdView(this);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(getString(R.string.banner1_id));
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER | Gravity.BOTTOM;
mAdView.setLayoutParams(params);
mAdView.setId(View.generateViewId());
coordinatorLayout.addView(mAdView);
}
private void initAd() {
MobileAds.initialize(this, initializationStatus -> {
});
initBanner();
}
private void initBanner() {
mAdRequest = new AdRequest.Builder().build();
mAdView.loadAd(mAdRequest);
mAdView.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
}
#Override
public void onAdOpened() {
super.onAdOpened();
}
});
}

Categories

Resources