sagar chaudhary
sagar chaudhary

Reputation: 33

admob banner ads overlaps main layout

Admob banner ads in my android app overlaps bottom of the main layout, there are many solutions on here but non of them is working for me.how do i separate this two layouts. following is my activity.main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:indeterminate="false"
            android:layout_gravity="center" />

        <WebView
            android:id="@+id/webView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_gravity="bottom"
            android:layout_width="wrap_content"
            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>

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemIconTint="#d20b0d"
        app:itemTextColor="#333"
        app:menu="@menu/navigation_items" />
</android.support.v4.widget.DrawerLayout>

Upvotes: 1

Views: 2253

Answers (2)

A.R.
A.R.

Reputation: 2641

Problem is that:

Admob Ad is align on TOP|BOTTOM wrt Parent, hence its overlapping your WebView which is acquiring Full width and Height of screen.

There is two way which you can follow to avoid overlapping of Ads on WebView

Solution 1:

Give margin_bottom to Webview

android:layout_marginBottom="50dp"  or android:layout_marginBottom="90dp"

As you are using "SMART BANNER" you can configure hardcode value i.e "50dp" and "90dp" in two different "dimens.xml" wrt Screen Resolution and Size

Solution 2:

You can use LinearLayout to contain both "WebView" and "Admob Ad" and assign weight to webview only, like below code.

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center"
            android:indeterminate="false"/>

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

            <WebView
                android:id="@+id/webView1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

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

        </LinearLayout>

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemIconTint="#d20b0d"
        app:itemTextColor="#333"
        app:menu="@menu/navigation_items"/>
</android.support.v4.widget.DrawerLayout>

Choice is yours you which you follow.

Note: Don't use fill_parent in your layouts as they are deprecated

Upvotes: 1

Dnyanesh
Dnyanesh

Reputation: 331

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:indeterminate="false" />

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

        <WebView
            android:id="@+id/webView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:layout_gravity="bottom"
            ads:adSize="SMART_BANNER"

            ads:adUnitId="@string/banner_ad_unit_id"></com.google.android.gms.ads.AdView>
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:itemIconTint="#d20b0d"
    app:itemTextColor="#333"
    app:menu="@menu/navigation_items" />

Upvotes: 0

Related Questions