This question already has answers here:
AdMob AdView and ViewPager?
(2 answers)
Closed 9 years ago.
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>
</RelativeLayout>
First, there is no android:layout_weight for a RelativeLayout.
Second, there is no android:orientation for a RelativeLayout.
If you want to use those, use a LinearLayout, not a RelativeLayout. If you do that, get rid of the three android:layout_alignParent* attributes from the AdView, along with android:layout_below, as those are for RelativeLayout, not LinearLayout.
The result should work, assuming that AdView works with android:layout_height="wrap_content".
<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="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="#string/admob_publisher_id"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
</LinearLayout>
Related
I have an android activity with a xml layout as follows:
<RelativeLayout>
...
<GridLayout>
...
</GridLayout>
....
</RelativeLayout>
to which I would like to add the ads-thingy. I have tried the following setup:
<LinearLayout>
<RelativeLayout>
...
<GridLayout>
...
</GridLayout>
....
</RelativeLayout>
<com.google.android.gms.ads.AdView>
...
</com.google.android.gms.ads.AdView>
</LinearLayout>
but it does not show the ads (the dots represent other content I believe is not important for this question). Is there some special trick to use to be able to show the ads-thingy? Make the layout even more complex? Is there a special layout-things I have to use?
My main activity does show the ads; the structure here is as follows:
<LinearLayout>
...
<android.support.design.widget.CoordinatorLayout>
<LinearLayout>
...
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
<com.google.android.gms.ads.AdView>
....
</com.google.android.gms.ads.AdView>
<LinearLayout
A more complete example of the real layout xml file (stripped off the really uneccessary parts...)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
>
<GridLayout
android:id="#+id/grid"
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/new_cancel"
android:alignmentMode="alignBounds"
android:columnCount="10"
android:columnOrderPreserved="false"
android:useDefaultMargins="true">
// REMOVED TEXTVIEW, EDITTEXT, IMAGEBUTTONS etc HERE
</GridLayout>
<Button
android:id="#+id/new_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#color/colorPrimaryDark"
android:text="Cancel"
android:layout_margin="5dp"
/>
<Button
android:id="#+id/new_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="#color/colorPrimaryLight"
android:text="Ok"
android:layout_margin="5dp"
/>
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</LinearLayout>
P.S. What is the general name of things like LinearLayout or RelativeLayout?
As far as I know you have to initialize your ad in your Activity code using an AdRequest object:
AdView adView = (AdView) findViewById( R.id.adView );
AdRequest request = new AdRequest.Builder()
.build();
adView.loadAd( request );
Discarding a problem with your ADS initialization, as you say it works in another activity, and focusing on layout problems, I'd say the problem is the following:
You have a Vertical LinearLayout that covers the whole screen (match_parent on a top level) with two childs
The first child is a RelativeLayout that covers the whole parent (match_parent)
The second child is the Ad View that is probably being displayed, but you can't see it because it starts at the first pixel after the screen height.
So to correct this, I'd suggest you to try:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#cccccc"
>
.
.
.
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
I'd also suggest to give the proper dimensions to the Ad View as you know them (https://developers.google.com/android/reference/com/google/android/gms/ads/AdSize#constant-summary) . Then, I'd suggest to use a FrameLayout as is way lighter than a linearlayout. You would do it like this:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
android:layout_marginBottom = "50dp">
.
.
.
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="bottom|center_horizontal"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</FrameLayout>
Make sure that your Admob layout is displaying in xml view. Put your admob view inside RelativeLayout and try to use android:alignparentBottom:true
You can check this answer.
https://stackoverflow.com/a/28578320/2022000
This is what I am trying to implement:
The visibility of the adview can be toggled to either visible or gone. This is a layout which I used and is working without the two buttons.
This is the xml for with the scrollview and the adview, without the buttons. (scrollview works)
<RelativeLayout
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"
android:visibility="visible" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/adView"
android:paddingBottom="12dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="12dp">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:layout_marginTop="0dp"
android:stretchColumns="0">
<TableRow>
//scrollable content here
</TableRow
</TableLayout>
</ScrollView>
I try to implement scenario A by encapsulating the adView element inside a TableLayout, so I can attempt to use TableRows to implement the buttons.
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tableLayout">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"
android:visibility="visible" />
</TableLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/tableLayout"
.....
When I use this approach, my scrollview disappears. Where am I going wrong and how can I resolve this and get to scenario A?
You are missing the attribute 'android:layout_alignParentBottom="true"' from TableLayout that you had in AdView. This aligns the table in top of the parent relative layout although your intention was to put it in the bottom.
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();
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
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.