Admob size does not fill parent

Viewed 4012

I will post the code below, my issue is this, in the emulator (Genymotion Galaxy Nexus 4.2.2) the admob banner stretches and fill its parent:

enter image description here

In a real device (Galaxy Nexus 4.3) the admob banner does not fill the whole width of its parent, leaving two white stripes on the sides:

enter image description here

this is the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView  
 android:id="@+id/webview"
 android:layout_width="fill_parent"
 android:layout_height="0px"
 android:layout_weight="1"/>

    <com.google.android.gms.ads.AdView android:id="@+id/adView"
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content"
                         ads:adUnitId="ca-app-pub-1470640527107044/6766749615"
                         ads:adSize="SMART_BANNER"/>

</LinearLayout>

and this is the onCreate of the activity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);    
        setContentView(R.layout.activity_detail);


        ActionBar actionBar = getSupportActionBar();            
        actionBar.setDisplayHomeAsUpEnabled(true);

        WebView myWebView = (WebView) findViewById(R.id.webview);    
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);    
        Bundle b = getIntent().getExtras();
        url = b.getString("url");           
        myWebView.loadUrl(url);
        myWebView.setWebViewClient(new MyWebViewClient());


        // Initiate a generic request.
        AdRequest adRequest = new AdRequest.Builder().build();    
        adView = (AdView) findViewById(R.id.adView);
        adView.loadAd(adRequest);
    }

What am I doing wrong? I have examined it a lot but I can not figure it out...

3 Answers

Be sure to use the newly introduced AdMob Adaptive Banners. They don't require adjustments on your side and they utilize all the width available.

Embed them into a FrameLayout like following

<FrameLayout
    android:id="@+id/adFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center|bottom" />
Related