Android NativeAppInstallAdView never shows the ad

587 views
Skip to first unread message

Ali Y. Akgul

unread,
Jul 13, 2016, 10:57:35 AM7/13/16
to Google Mobile Ads SDK Developers
I have a problem with showing native app install ad view in one of my activities. As Google shows the implementation of native ads on: 
Here is my gradle file:

   
 apply plugin: 'com.android.application'
   
    android
{
        compileSdkVersion
23
        buildToolsVersion
"23.0.3"
   
        defaultConfig
{
            applicationId
"com.somepack.tests"
            minSdkVersion
15
            targetSdkVersion
23
            versionCode
1
            versionName
"1.0"
       
}
        buildTypes
{
            release
{
                minifyEnabled
false
                proguardFiles getDefaultProguardFile
('proguard-android.txt'), 'proguard-rules.pro'
           
}
       
}
   
}
   
    dependencies
{
        compile fileTree
(dir: 'libs', include: ['*.jar'])
        testCompile
'junit:junit:4.12'
   
        compile
'com.android.support:appcompat-v7:23.4.0'
        compile
'com.android.support:design:23.4.0'
        compile
'com.android.support:recyclerview-v7:23.4.0'
        compile
'com.android.support:cardview-v7:23.4.0'
        compile
'com.google.android.gms:play-services-ads:9.2.0'
        compile
'com.google.firebase:firebase-ads:9.0.0'
   
}
   
    apply plugin
: 'com.google.gms.google-services'



And I have also added same code to my root gradle file:

   
// Top-level build file where you can add configuration options common to all sub-projects/modules.
   
    buildscript
{
        repositories
{
            mavenLocal
()
            jcenter
()
       
}
        dependencies
{
            classpath
'com.android.tools.build:gradle:2.1.2'
            classpath
'com.google.gms:google-services:3.0.0'
   
           
// NOTE: Do not place your application dependencies here; they belong
           
// in the individual module build.gradle files
       
}
   
}
   
    allprojects
{
        repositories
{
            mavenLocal
()
            jcenter
()
       
}
   
}
   
    task clean
(type: Delete) {
       
delete rootProject.buildDir
   
}


I have tried to run the app with Google's app id and unit id, and everything seems to be working. When I change app id and unit id to my values, it just keeps saying 'Failed to load native ad' with the error code 0. 

And here is my activity:

   
import android.os.Bundle;
   
import android.support.v7.app.AppCompatActivity;
   
import android.view.View;
   
import android.widget.Button;
   
import android.widget.FrameLayout;
   
import android.widget.ImageView;
   
import android.widget.RatingBar;
   
import android.widget.TextView;
   
import android.widget.Toast;
   
   
import com.google.android.gms.ads.AdListener;
   
import com.google.android.gms.ads.AdLoader;
   
import com.google.android.gms.ads.AdRequest;
   
import com.google.android.gms.ads.MobileAds;
   
import com.google.android.gms.ads.formats.NativeAd;
   
import com.google.android.gms.ads.formats.NativeAppInstallAd;
   
import com.google.android.gms.ads.formats.NativeAppInstallAdView;
   
   
import java.util.List;
   
   
public class AdActivity extends AppCompatActivity {
   
       
@Override
       
protected void onCreate(Bundle savedInstanceState) {
           
super.onCreate(savedInstanceState);
            setContentView
(R.layout.activity_ad);
   
           
String ADMOB_APP_ID = getString(R.string.admob_app_id);
           
// Initialize the Mobile Ads SDK.
           
MobileAds.initialize(this, ADMOB_APP_ID);
            refreshAd
();
       
}
   
       
private AdRequest.Builder getBuilder() {
           
AdRequest.Builder builder = new AdRequest.Builder();
           
if (BuildConfig.DEBUG) {
                builder
.addTestDevice("BDFA4B1F1E11FDA72D43368841CF0E04");
           
}
           
return builder;
       
}
   
       
private void refreshAd() {
           
AdLoader.Builder builder = new AdLoader.Builder(this, getString(R.string.admob_unit_id));
            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(AdActivity.this, "Failed to load native ad: "
                           
+ errorCode, Toast.LENGTH_SHORT).show();
               
}
           
}).build();
   
            adLoader
.loadAd(getBuilder().build());
       
}
   
       
private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd,
                                             
NativeAppInstallAdView adView) {
            adView
.setHeadlineView(adView.findViewById(R.id.appinstall_headline));
            adView
.setImageView(adView.findViewById(R.id.appinstall_image));
            adView
.setBodyView(adView.findViewById(R.id.appinstall_body));
            adView
.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action));
            adView
.setIconView(adView.findViewById(R.id.appinstall_app_icon));
            adView
.setPriceView(adView.findViewById(R.id.appinstall_price));
            adView
.setStarRatingView(adView.findViewById(R.id.appinstall_stars));
            adView
.setStoreView(adView.findViewById(R.id.appinstall_store));
   
           
// Some assets are guaranteed to be in every NativeAppInstallAd.
           
((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline());
           
((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody());
           
((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction());
           
((ImageView) adView.getIconView()).setImageDrawable(nativeAppInstallAd.getIcon()
                   
.getDrawable());
   
           
List<NativeAd.Image> images = nativeAppInstallAd.getImages();
   
           
if (images.size() > 0) {
               
((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable());
           
}
   
           
// Some aren't guaranteed, however, and should be checked.
           
if (nativeAppInstallAd.getPrice() == null) {
                adView
.getPriceView().setVisibility(View.INVISIBLE);
           
} else {
                adView
.getPriceView().setVisibility(View.VISIBLE);
               
((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice());
           
}
   
           
if (nativeAppInstallAd.getStore() == null) {
                adView
.getStoreView().setVisibility(View.INVISIBLE);
           
} else {
                adView
.getStoreView().setVisibility(View.VISIBLE);
               
((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore());
           
}
   
           
if (nativeAppInstallAd.getStarRating() == null) {
                adView
.getStarRatingView().setVisibility(View.INVISIBLE);
           
} else {
               
((RatingBar) adView.getStarRatingView())
                       
.setRating(nativeAppInstallAd.getStarRating().floatValue());
                adView
.getStarRatingView().setVisibility(View.VISIBLE);
           
}
   
           
// Assign native ad object to the native view.
            adView
.setNativeAd(nativeAppInstallAd);
       
}
   
}


I thought it doesn't show the ad because my apk is debug and tried with release too. But no chance. Am I missing a setting on Admob panel or any code that I need to implement? I am greatly appriciated for any help.

P.S: The code I wrote above is just a trial, I have the same code in my real app, which is on Play Store, but could not succeed.

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Jul 13, 2016, 11:19:10 AM7/13/16
to Google Mobile Ads SDK Developers
Hi Ali,

I'm wondering whether you are using a Native Ad Express Ad Unit ID with an Advanced one. Basically we have two types of Natives Ads - Native Ad Express and Native Advanced. Recently we have enabled Native Ad Express for public while Native Advanced is still in closed beta. Would you mind testing your Ad Unit ID with our Native Ad Express sample app and let us know? If you are able to receive ads, then you simply got mixed up with the two. You can read more about Native Ads in our guidelines.

Thanks,
Veer Busani
Mobile Ads SDK Team

Ali Y. Akgul

unread,
Jul 13, 2016, 3:11:03 PM7/13/16
to Google Mobile Ads SDK Developers
Hi Veer, 

Thanks for writing me back. I used the following code in the url: https://github.com/googleads/googleads-mobile-android-examples/tree/master/admob/NativeExample


I have tried both. With my same key. But still could not get it work.

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Jul 13, 2016, 3:59:29 PM7/13/16
to Google Mobile Ads SDK Developers
Hi Ali,

Can you send us your Ad Unit ID for us to test as well? Also, did you match the AdSize parameter as per your settings in your Ad Unit ID? 

Thanks,
Veer Busani
Mobile Ads SDK Team

Ali Y. Akgul

unread,
Jul 13, 2016, 4:22:56 PM7/13/16
to Google Mobile Ads SDK Developers
Hi Veer, 

Sure, here is the ids:

app unit id: ca-app-pub-2677083695887295/6946421316
app id: ca-app-pub-2677083695887295~8739713313

Veer Arjun Busani(Mobile Ads SDK Team)

unread,
Jul 13, 2016, 4:46:39 PM7/13/16
to Google Mobile Ads SDK Developers
Hi Ali,

I was able to load Native Ad Express ads using your Ad Unit ID. The only thing that I have changed with our sample app is the AdSize parameter to 360x320. Make that change, launch on your device or the emulator. Also make sure that you have sufficient space to render your 360x320 Ad Size. 

If you are still unable to load any ads, use this link to send us your complete logcat.

Thanks,
Veer Busani
Mobile Ads SDK Team

Ali Y. Akgul

unread,
Jul 13, 2016, 4:54:41 PM7/13/16
to Google Mobile Ads SDK Developers
Hi Veer,

Thanks a lot for your help! I could get it work! 

Really appreciated, have a nice day.
-ali
Reply all
Reply to author
Forward
0 new messages