HelloCW
HelloCW

Reputation: 2255

The latest Admob Ad can't be displayed if I set android:padding in layout, why?

If I use the layout of Case A, the Admob Ad can't be displayed.

If I use the layout of Case B, the Admob Ad can be displayed correctly! I can't understand why?

And more, if I set android:background such as android:background="@drawable/border_ui", the Admob Ad can't be displayed too even if the border_ui.xml is empty.

You can test the sample at https://dl.dropboxusercontent.com/u/209352/aa.zip

Case A

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.aa.MainActivity" >

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

    <com.google.android.gms.ads.AdView  
        xmlns:ads="http://schemas.android.com/apk/res-auto"       
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="YouID" />

</RelativeLayout>

Case B

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"   
    tools:context="com.example.aa.MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <com.google.android.gms.ads.AdView  
        xmlns:ads="http://schemas.android.com/apk/res-auto"       
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="YouID" />

</RelativeLayout>

dimens.xml

 <resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>

MainActivity Class

package com.example.aa;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class MainActivity extends ActionBarActivity {

    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       adView = (AdView)this.findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder()
                         .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                         .build();
            adView.loadAd(adRequest);
    }

      @Override
      public void onResume() {
        super.onResume();
        if (adView != null) {
          adView.resume();
        }
      }

      @Override
      public void onPause() {
        if (adView != null) {
          adView.pause();
        }
        super.onPause();
      }

      @Override
      public void onDestroy() {
        if (adView != null) {
          adView.destroy();
        }
        super.onDestroy();
      }


}

Upvotes: 0

Views: 446

Answers (2)

William
William

Reputation: 20196

Quite simply the padding in case A means there is not enough room to display the ad. If you look at your log you will see messages to that effect.

SmartBanner just means that an adsize suitable for the device size and resolution is chosen, it doesn't mean that it chooses one that fits any arbitrary screen layout.

Upvotes: 1

raj
raj

Reputation: 2088

latest admob sdk is integrated in googleplayservices which start showing ad after adding this to your manifest file

 <meta-data android:name="com.google.android.gms.version" android:value="5089000"/>

Upvotes: 0

Related Questions