Shayts
Shayts

Reputation: 107

Android- Smart banner ad isn't showing on screen on portrait mode

I'm using ads in my app, and I have followed the tutorial here: https://developers.google.com/admob/android/banner

If I set the adSize to just "BANNER" in the xml, everything works fine, but the ad doesn't look good on landscape mode or in larger screens. So now I'm trying to change the adSize to "SMART_BANNER", but for some reason the banner is showing only on landscape mode, and on portrait I have just a white adview. What am I missing here?

Here is the code:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="0dp"
    android:padding="0dp"
    tools:context="com.example.shaytsabar.footballtables
    .activities.MainActivity"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="81dp"
    android:layout_marginLeft="0dp">


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar_con"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="0dp"
    android:elevation="0dp"
    android:minHeight="?attr/actionBarSize"
    android:padding="0dp"
    android:paddingBottom="0dp"
    android:paddingEnd="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingStart="0dp"
    android:paddingTop="0dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<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"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/ad_id"
    ads:layout_constraintBottom_toBottomOf="parent"
    ads:layout_constraintLeft_toLeftOf="parent"
    ads:layout_constraintRight_toRightOf="parent">
</com.google.android.gms.ads.AdView>

<FrameLayout
    android:id="@+id/fragment_con"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:elevation="0dp"
    android:padding="0dp"
    app:layout_constraintBottom_toTopOf="@+id/adView"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar_con"
    app:layout_constraintVertical_bias="0.0">

  </FrameLayout>
</android.support.constraint.ConstraintLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity{
    InterstitialAd mInterstitialAd;
    AdView mAdView;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobileAds.initialize(this, APPCODE);
        ShowLittleAd();
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdFailedToLoad(int i) {
                super.onAdFailedToLoad(i);
                ShowLittleAd();
            }
        });
      }
    public void ShowLittleAd(){
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new 
      AdRequest.Builder().addTestDevice("//MY DEVICE").
                addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
        mAdView.loadAd(adRequest);
    }          
}

Upvotes: 1

Views: 1049

Answers (1)

Aditi Parikh
Aditi Parikh

Reputation: 1522

Okay..I caught the Problem...

Please remove all margins from Your Adview. Replace this

<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"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/ad_id"
    ads:layout_constraintBottom_toBottomOf="parent"
    ads:layout_constraintLeft_toLeftOf="parent"
    ads:layout_constraintRight_toRightOf="parent">
</com.google.android.gms.ads.AdView>

to this -

<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"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/ad_id"
    ads:layout_constraintBottom_toBottomOf="parent"
    ads:layout_constraintLeft_toLeftOf="parent"
    ads:layout_constraintRight_toRightOf="parent">
</com.google.android.gms.ads.AdView>

As Banner will take full width now and can be shown. The problem was,with the margins, It was not getting enough width for display...

Upvotes: 1

Related Questions