androidandroid-layoutlayoutsizeadmob

adMob | Extend Banner size?


My goal is to make admob banner stand at the bottom of the screen as in this photo:

enter image description here

But I do not know how to do banner sits hit the screen because of the higher layout i set padding.

LogCat say: Not enough space to show ad. Needs 320x50 dp, but only has 288x135 dp.

How to extend my space and how to set banner at bottom ?

My layout xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="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:background="@color/background"
    android:orientation="vertical"
    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="eu.gabrielatanasov.demoViewer.Home_activity" >

    <ImageView
        android:id="@+id/img_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/img_logo"

    <Button
        android:id="@+id/btn_subscription"
        style="@style/button"
        android:layout_marginTop="1dp"
        android:text="@string/btn_subscription" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="@string/adMob_ID" >
        </com.google.android.gms.ads.AdView>
    </LinearLayout>

</LinearLayout>

My 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>

My activity:

package eu.gabrielatanasov.demoViewer;

import com.google.android.gms.ads.*;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class Home_activity extends Activity {
    Button btn_subscription;

    AdView adView;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_home);
        
        adView = (AdView) this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
        btn_subscription = (Button) findViewById(R.id.btn_subscription);

        btn_subscription.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                vibrateButton(v);
            }
        });
    }

    public void vibrateButton(View v) {
        buttonVibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        buttonVibrate.vibrate(30);
    }
}

Solution

  • Wrap your LinearLayout inside another RelativeLayout. That way the LinearLayout's padding won't reduce the available space for the banner.

    See example:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mainLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >   
    
        <LinearLayout  
    
        //copy paste your linear layout code here (without AdView).. 
    
        ...
    
        </LinearLayout>
    
        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="@string/adMob_ID" />
    
    </RelativeLayout>
    

    Note the android:layout_alignParentBottom="true" which aligns the AdView to the bottom