Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android How to align AdView to center in a linear layout

I am trying to align gogole Adview to the bottom and center of a list, till now I was able to force the Adview to come at the bottom of list without interfering with the list but it is not coming in center .

Code is pasted below , Sorry cudn't attach the screenshot

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/topLayout"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/linearlayoutlistview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/ad_holder"
        android:orientation="vertical" >

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dip"
            android:drawablePadding="4dip"
            android:paddingTop="2dip"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textStyle="bold"
            android:typeface="serif" >
        </ListView>
    </LinearLayout>

    <!-- Ad Placeholder -->

    <LinearLayout
        android:id="@+id/ad_holder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="bottom" >

        <com.google.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="MY_AD_UNIT_ID"
            ads:loadAdOnCreate="true"
            ads:testDevices="TEST_EMULATOR" >
        </com.google.ads.AdView>

    </LinearLayout>

</RelativeLayout>
like image 308
Shakti Avatar asked Apr 13 '12 14:04

Shakti


3 Answers

Don't use LinearLayout as the AdView's parent. Instead, use RelativeLayout and layout_centerInParent="true" for the AdView:

<RelativeLayout
    android:id="@+id/ad_holder"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="bottom" >

    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        ads:adSize="BANNER"
        ads:adUnitId="MY_AD_UNIT_ID"
        ads:loadAdOnCreate="true"
        ads:testDevices="TEST_EMULATOR" >
    </com.google.ads.AdView>

</RelativeLayout>
like image 52
Aleks G Avatar answered Sep 22 '22 11:09

Aleks G


Or just use android:layout_width="fill_parent" if you still want to work with LinearLayout.

like image 24
magicode118 Avatar answered Sep 18 '22 11:09

magicode118


Use android:layout_alignParentBottom="true" and android:layout_centerHorizontal="true"

like image 26
Comic Sans MS Lover Avatar answered Sep 22 '22 11:09

Comic Sans MS Lover