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 :)
Related
For those wanting to have an adView in their app that is comprised of Fragment layouts, hope the below helps you!
Basically, I was originally trying to place the adView inside the Layout XML for each fragment. This caused the adView to either be pushed off the screen or not play nice with the relative layout commands (ex. alignParentBottom).
The solution was to move the adView into the Main Activity Layout outside of the the Coordinator Layout used for my fragments. Then I wrapped the Coordinator Layout and adView in a Relative Layout.
This then allowed me to fully control the adView and present on each fragment pinned to the bottom of the screen.
I have found that when I change the background of your ScrollView, it paints OVER the AdView. You MUST place your AdView below the ScrollView in code. That way, it is drawn AFTER the ScrollView and is therefore on top.
Here is your edited code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<!-- Dummy item to prevent EditTextView from receiving focus -->
<LinearLayout
android:id="#+id/dummyLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal" />
<!-- Dummy item to prevent EditTextView from receiving focus -->
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="4"
android:hint="#string/hint"
android:nextFocusLeft="#id/editText1"
android:nextFocusUp="#id/editText1"
android:singleLine="true"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/linearLayout1"/>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView4"/>
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/button1"
android:background="#color/colorAccent0"
android:stretchColumns="*"/>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tableLayout1"
android:layout_above="#+id/adView"
android:background="#color/colorGray">
<TableLayout
android:id="#+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"/>
</ScrollView>
<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_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
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.
In my andriod app I have an image view and back and next button as shown below. It works fine. Now I want to draw an admob ad below the buttons or above the buttons but below the image. In any case I don’t want image to be cropped by ad or buttons.
Here is layout which have image and two buttons:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/Linear1">
<RelativeLayout android:id="#+id/relativeLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView android:id="#+id/katha_image"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_marginBottom="#dimen/menu_item_size"></ImageView>
<LinearLayout android:layout_width="fill_parent"
android:orientation="horizontal" android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_gravity="bottom|center">
<Button android:id="#+id/Button_Back" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="50"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" android:text="#string/back"
android:textSize="#dimen/button_size" android:minWidth="75px"></Button>
<Button android:text="#string/next" android:textSize="#dimen/button_size"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="50" android:id="#+id/Button_Next"></Button>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Please suggest.
PC
You'll want to use android:layout_above or android:layout_below with your elements in a RelativeLayout so that they don't overlap.
<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:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
android:layout_above="#id/linearLayout1"/>
<!-- Assumes your LinearLayout with buttons have id "linearLayout1" -->
Then add android:layout_above="#id/adView" on your ImageView to make it lay out above the ad.
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.
Well before my ads were working fine and were always at the bottom of the screen, but now I added a scrollview and the ads move up a little bit to be right under my scrollbar.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/pattern_carbon_fiber_dark">
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content" android:layout_height="339px">
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:text="Silent Mode" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/silent_textview"></TextView>
<ToggleButton android:layout_height="wrap_content" android:text="Silent Mode" android:id="#+id/silentbutton" android:layout_width="wrap_content" android:textOff="OFF" android:textOn="ON"></ToggleButton>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Wifi " android:id="#+id/wifi_textview"></TextView>
<ToggleButton android:layout_width="wrap_content" android:text="ToggleButton" android:layout_height="wrap_content" android:id="#+id/wifibutton"></ToggleButton>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/gps" android:id="#+id/gps_textview"></TextView>
<Button android:text="#string/gpsbutton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/gps"></Button>
<TextView android:layout_width="wrap_content" android:text="#string/bluetooth" android:layout_height="wrap_content" android:id="#+id/bluetooth_textview"></TextView>
<ToggleButton android:layout_width="wrap_content" android:text="ToggleButton" android:id="#+id/bluetooth" android:layout_height="wrap_content"></ToggleButton>
<TextView android:text="#string/brightness" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/screenbrightness_textview"></TextView>
<SeekBar android:layout_height="wrap_content" android:id="#+id/SbBar" android:layout_width="fill_parent" android:max="255"></SeekBar>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="wrap_content">
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/adView"
android:layout_width="fill_parent"
ads:adSize="BANNER"
ads:adUnitId="a14ddb1a61b9fb4"
ads:loadAdOnCreate="true"
android:visibility="visible"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_height="wrap_content"
android:layout_alignParentTop="false">
</com.google.ads.AdView>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Please help, thanks!
EDIT: Picture
UI pic http://img52.imageshack.us/img52/9397/unledyv.png
1) You will probably leave the ads outside the scollview so they are always visible (as opposed to scroll out of the screen)
2) What is the purpose of the relativelayout surrounding the adview? That is the only view inside it: just remove it and you will get the same layout, but better performance.
Based on the update, I suggest you to try this layout (you will need to fill many blanks):
<RelativeLayout
...
>
<AdView
android:id="#+id/adView"
android:layout_alignParentBottom="true"
...
/>
<ScrollView
android:layout_alignParentTop="true"
android:layout_above="#id/adView"
...
>
<LinearLayout
...
>
...
...
</LinearLayout>
</ScrollView>
</RelativeLayout>
The basic idea is to put the AdView ad the bottom of the screen using a RelativeLayout, and then setting the ScollView to be placed above the ads (so it does not block them) and aligned to the top of the screen.
Why don't you try using a relative layout as the root layout.
<RelativeLayout>
<LinearLayout
android:layout_alignParentTop="true">
ETC
</LinearLayout>
<com.google.ads.AdView
android:layout_alignParentBottom="true">
</com.google.ads.AdView>
</RelativeLayout>
To improve the user experience, we can move the ad outside the ScrollView and use a RelativeLayout to pin the ad to the bottom of the screen. This is the needed XML markup:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:id="#+id/scrollLayout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_above="#+id/adView">
<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Place very long string here..."/>
</LinearLayout>
</ScrollView>
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR"
ads:loadAdOnCreate="true"/>
</RelativeLayout>