Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Banner Space when there there are no Ads Android

Tags:

android

view

ads

I recently changed from Admob SDK to GPS Lib and there is 1 thing bothering me. I have a simple View to define line in my Layout to prevent users from clicking on ads unintentionally. With Admob SDK that line would drop to the bottom of layout when there are no ads, but with GPS Lib, that View remains where it is. enter image description here

And when there are no ads, when user is using app in offline mode there is that empty ugly space. enter image description here

How can I get rid of that space when there are no ads? Here is my XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/back2" >
    <RelativeLayout
        android:id="@+id/upperBar"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="#000000" >
        <RelativeLayout
            android:id="@+id/RelativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/bar"
            android:paddingBottom="2dp"
            android:paddingLeft="2dp"
            android:paddingRight="2dp"
            android:paddingTop="2dp" >
            <ImageButton
                android:id="@+id/infop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:background="@drawable/info_select_blue" />
            <ImageButton
                android:id="@+id/share"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/share_selector_blue" />
            <ImageButton
                android:id="@+id/moreaps"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_marginRight="20dp"
                android:background="@drawable/more_selector_blue" />
        </RelativeLayout>
    </RelativeLayout>
    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/view"
        android:layout_below="@+id/sbsz"
        android:columnWidth="110dp"
        android:horizontalSpacing="10dp"
        android:listSelector="#00000000"
        android:numColumns="auto_fit"
        android:verticalSpacing="12dp" >
    </GridView>
    <SeekBar
        android:id="@+id/sbsz"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/upperBar"
        android:progressDrawable="@drawable/apptheme_scrubber_progress_horizontal_holo_dark"
        android:thumb="@drawable/apptheme_scrubber_control_selector_holo_dark" />
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner" >
    </com.google.android.gms.ads.AdView>
    <View
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="17dp"
        android:layout_above="@+id/adView"
        android:layout_alignParentLeft="true"
        android:background="@drawable/bar"
        android:visibility="visible" />

</RelativeLayout>
like image 236
Slim C. Avatar asked Jun 09 '14 11:06

Slim C.


People also ask

What is adaptive banner?

Adaptive banners are the next generation of responsive ads, maximizing performance by optimizing ad size for each device. Improving on smart banners, which only supported fixed heights, adaptive banners let developers specify the ad width and use this to determine the optimal ad size.

What are banner ads in Android?

Banner ads occupy a spot within an app's layout, either at the top or bottom of the device screen. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. If you're new to mobile advertising, they're a great place to start.

What are adaptive ads?

Adaptive banners are the newest generation of banner ads and offer the best user experience. They're intended to replace smart and standard banners. Note: Support for smart banners is deprecated. Consider implementing adaptive banners as a replacement for smart banners. Type.


2 Answers

Hide your adView initially by adding android:visibility="gone"

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/banner" >
</com.google.android.gms.ads.AdView>

Now handle adview visibility in setAdListener

adView.setAdListener(new AdListener() {

    @Override
    public void onAdLoaded() {
        adView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onAdFailedToLoad(int error) {
        adView.setVisibility(View.GONE);
    }    

});

This will result in exact same behavior as the old Admob SDK.

like image 77
A.J. Avatar answered Sep 20 '22 00:09

A.J.


you have to check programatically there are ad present or no.if ads are not present then just set

adView.setVisibility(View.GONE) 

else

adView.setVisibility(View.Visible)

thats it...

like image 44
Jayesh Khasatiya Avatar answered Sep 22 '22 00:09

Jayesh Khasatiya