Admob SMART_BANNER in android is not working when orientation is in Landscape mode

1.9k Views Asked by At

I am using Admob SMART_BANNER in my android app. The app is showing when device is in portrait mode. But if the device orientation is in landscape mode then the add is not showing. I know banner add is not shown if there is not enough space to display an add. But in my case it seems to have enough space to display an add. Here is my code:

activity_main.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="15dp"/>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>


</RelativeLayout>

strings.xml file in values folder:

<resources>
    <string name="app_name">Banner Example</string>

    <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>

</resources>

Java code:

TelephonyManager tManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
       String uid = tManager.getDeviceId();

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder`enter code here`()
                .addTestDevice(uid)
                .build();
        mAdView.loadAd(adRequest);
3

There are 3 best solutions below

1
On BEST ANSWER

You can dynamically load adsize based on the devices' screen resolutions:

 AdSize adSize = AdSize.SMART_BANNER;  

    DisplayMetrics dm = getResources().getDisplayMetrics();  
    double density = dm.density * 160;
    double x = Math.pow(dm.widthPixels / density, 2);
    double y = Math.pow(dm.heightPixels / density, 2);
    double screenInches = Math.sqrt(x + y);   
    if (screenInches > 8) { // > 728 X 90
        adSize = AdSize.LEADERBOARD;
    } else if (screenInches > 6) { // > 468 X 60
        adSize = AdSize.MEDIUM_RECTANGLE;
    } else { // > 320 X 50
        adSize = AdSize.BANNER;
    }

   AdView mAdView = (AdView) findViewById(R.id.adView);
     mAdView.setAdUnitId(yourAdUnitHere); 
     mAdView.setAdSize(adSize);
    AdRequest adRequest = new AdRequest.Builder().build();
     mAdView.loadAd(adRequest);
4
On
try this below code

private void showBannerAd()
{
    AdView adView = new AdView(HomeActivity.this);
    //cal method to show banner add

    if(getResources().getConfiguration().orientation == 
    Configuration.ORIENTATION_LANDSCAPE)
    {
        adView.setAdSize(AdSize.BANNER);
    }
    else
    {
        adView.setAdSize(AdSize.SMART_BANNER);
    }

    adView.setAdUnitId("AD_ID");
    AdRequest adRequest = new
     AdRequest.Builder().addTestDevice(Secure.getString(getContentResolver()  
    ,Secure.ANDROID_ID)).build();
    adView.loadAd(adRequest);
}
1
On

This may not apply to the original question, but I wanted to share my solution to a case of SMART_BANNER not working.

I found that in my case that SMART_BANNER wants to take the entire width of the screen regardless of the orientation - landscape or portrait. I put the AdView at the bottom. If it is in a column that takes 80% of the width of the screen, it will neither show nor complain (i.e. not invoke onAdFailedToLoad). It simply hides. Once it occupies the entire width, the ad shows without any problem.