I am trying to add adview in my application. The problem is adview is getting displayed next to the buttons and not in the bottom end of the screen, i tried android:layout_gravity="bottom" but it has no effect, the parent layout is linear layout.
<com.google.ads.AdView android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
android:layout_gravity="bottom"
ads:adUnitId="xxxxxxxxxxx"
ads:testDevices="TEST_EMULATOR"
ads:loadAdOnCreate="true"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:layout_height="wrap_content">
You can simple set :
<com.google.ads.AdView android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
android:gravity="bottom"
ads:adUnitId="xxxxxxxxxxx"
ads:testDevices="TEST_EMULATOR"
ads:loadAdOnCreate="true"/>
or You can set in LinearLayout:
<LinearLayout android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
Try putting an imageView in the gap, which is ofcourse transparent. As you want the ad to be placed at the end, and in linearLayout you cannot leave empty space, so use an imageview for the extra place and below the imageView put the Adview
Related
i have list view and i want to show ad banner in the bottom of this list view and also i have created space for this banner i.e android:paddingBottom="50dp", so i want to show ad on this 50dp space but now my ad showing on the above of this 50dp space i have 1 MainActivity.java and 2 xml file, first is activity_main.xml and second is single_row.xml. We know that there is no need to show java class because we set ad adjustment by xml.So i share my both xmls here and remember that i have relative layout not linear so there is no layout_weight option.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg"
android:paddingBottom="50dp"
tools:context=".MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="aaaa111122223333"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
</com.google.ads.AdView>
</LinearLayout>
</RelativeLayout>
My sigle_row.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:src="#drawable/img0"
android:paddingBottom="10dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/imageView1"
android:layout_toLeftOf="#+id/imageView1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFF00"
android:textStyle="bold"
/>
</RelativeLayout>
You need to set layout_above for the ListView to place above the AdView Layout, so that you can see all the list items
android:layout_above="#+id/layout"
Also, remove the bottom padding from the parent RelativeLayout
android:paddingBottom is used to leave some space at the bottom inside the View not to specify some space for other Views.
Now, remove the following attribute from parent RelativeLayout
android:paddingBottom="50dp"
And set 50dp to android:layout_height attribute for com.google.ads.AdView as below
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="aaaa111122223333"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
</com.google.ads.AdView>
</LinearLayout>
Use a relative Container, inside that use layout_above="#id/your_adview_id" on your listview.
Add your adView below ListView and set its visibility="gone".
In your java, when the add is loaded, set the visibility to visible as adView.setVisibility(View.VISIBLE);
I hope this helps :)
I have Android application with ViewPager implemented like this :
http://www.youtube.com/watch?v=C6_1KznO95A
I don't know how to implement AdView below the ViewPager.
I have tried to put AdView below the ViewPager programmatically and in xml.
There is no errors but ads are not showing.
My xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_alignParentBottom="top"
android:layout_height="0dp"
android:layout_weight="1"/>
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_below="#+id/pager"
ads:adSize="BANNER"
ads:adUnitId="#string/admob_publisher_id"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
OK, you have a couple of problems.
orientation is only relevant for a LinearLayout.
layout_alignParentBottom needs a boolean value not "top"
layout_weight is only relevant for a LinearLayout nor RelativeLayout
You have 2 options. Use a RelativeLayout but define the AdView first with the ViewPager being defined as above it OR use a LinearLayout and have the ViewPager fill the unused space using layout_weight.
As RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/admob_publisher_id"
ads:loadAdOnCreate="true" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_above="#+id/ad"
/>
</RelativeLayout>
OR as LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="#string/admob_publisher_id"
ads:loadAdOnCreate="true" />
</LinearLayout>
Personally I'd go with the LinearLayout it feels simpler.
This worked for me.
AdView adView = (AdView) findViewById(R.id.ad);
adView.bringToFront();
The advertisement is visible but it is not clickable and it looks as background image.
What is the problem ? is this about xml or java code ?
Here is xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background1"
tools:context=".Search">
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="MY - ID"
ads:loadAdOnCreate="true"/>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scrollingCache="false">
</ListView>
</RelativeLayout>
</RelativeLayout>
Here is java
RelativeLayout RelativeLayout1 = (RelativeLayout)findViewById(R.id.RelativeLayout1);
AdView adView=new AdView(this,AdSize.SMART_BANNER,"MY - ID");
RelativeLayout1.addView(adView);
AdRequest request = new AdRequest();
adView.loadAd(request);
The problem is on your XML Layout, you are using a RelativeLayout and adding two components on the same RelativeLayout, if you want to prevent the content to be overlap you need to set that on the layout adding something like:
android:layout_below="#+id/adView"
to the ListView , or you can use a LinearLayout, but I don't know how you want your layout...if you draw the layout I can try to help
I am trying to position my ad banner at bottom of the screen. However, part of the ad is appearing at the top and other part at the bottom. Please take a look at the image below
wrong ad http://img513.imageshack.us/img513/4390/shot000025.png
Below is the code if that helps,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/maingradient"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativelayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/relativeLayout1" >
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:divider="#00ff00"
android:fastScrollEnabled="true"
android:focusable="false"
android:listSelector="#000000"
android:paddingTop="5dp"
android:saveEnabled="true" >
</ListView>
</RelativeLayout>
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
android:layout_alignParentBottom="true"
ads:adUnitId="a14fd64cddd4168"
ads:loadAdOnCreate="true" />
</RelativeLayout>
Can someone help please?
Thx!
Rahul.
Not sure what your exact problem is, but I see a few issues with your layout. First, you have a RelativeLayout nested inside another RelativeLayout, but are trying to place it below a third relative layout not contained in the parent? Also, the root RelativeLayout in your file has the android:orientation attribute, which is for LinearLayouts. It's all a bit confusing.
As for a solution, try making the root RelativeLayout a LinearLayout and keeping the vertical orientation. You can give your nested RelativeLayout a android:layout_height of 0dip and android:layout_weight of 1 (and get rid of the layout_below attribute, I'm not sure why it's there). This will put your ListView above the ad, and cause it to fill all the space not taken up by the Ad. Hope this helps.
try the following code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/black" >
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="50dp"
ads:adSize="SMART_BANNER"
android:layout_alignParentBottom="true"
ads:adUnitId="a14fd64cddd4168"
ads:loadAdOnCreate="true" />
<ListView
android:id="#+id/show_locations_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_above="#+id/adView"
android:scrollbars="none"
android:scrollingCache="false" >
</ListView>
Hope it helps
http://i.stack.imgur.com/lbs7l.png
I'm trying to setup admob below the background, but I keep getting admob on top of the background like this .
http://i.stack.imgur.com/YJXU2.png
So what happened is I have a button alignedParentBottom="true"
but the ad is keep covering the button.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/backButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/back"
android:textSize="40sp"
android:layout_alignParentBottom="true"
></Button>
<ScrollView
android:id="#+id/ScrollView1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="#id/backButton"
>
<TextView
android:id="#+id/TextView1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="20sp"
android:text="#string/helphelp"></TextView>
</ScrollView>
<com.google.ads.AdView android:id="#+id/adView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_UNIT_ID"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"
android:layout_below="#id/backButton"></com.google.ads.AdView>
You can create a second RelativeLayout - the inner one will have all of your main layout content, and the outer one will hold your layout content and the AdView. Simply set the AdView to the bottom, and set your RelativeLayout content above the AdView, and it will force the back button up when the ad comes back.
I have provided the code here for your convenience.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_above="#+id/adView2">
<Button
android:id="#+id/backButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/back"
android:textSize="40sp"
android:layout_alignParentBottom="true">
</Button>
<ScrollView
android:id="#+id/ScrollView1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="#id/backButton">
<TextView
android:id="#+id/TextView1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="20sp"
android:text="#string/helphelp">
</TextView>
</ScrollView>
</RelativeLayout>
<com.google.ads.AdView android:id="#id/adView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adUnitId="MY_UNIT_ID"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true">
</com.google.ads.AdView>
</RelativeLayout>
Side note: Make sure your UI elements aren't too close to the ads; that may encourage accidental clicks, and is against AdMob's ToS.
Use a FrameLayout:
Child views are drawn in a stack, with the most recently added child on top.
There's nothing to it. If you want to ensure that the Button is drawn over the AdView, then place the AdView earlier in a common FrameLayout parent. E.g.,
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.ads.AdView android:id="#+id/adView2" ... />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
... Buttons, etc. ...
</RelativeLayout>
</FrameLayout>
Although you may want just the Button and AdView in a FrameLayout child of the RelativeLayout. Or you may want a RelativeLayout parent of the AdView in the example above, to position the ad.