ListView on TOP of Ads - Android - android

I'm having troubles showing ads on bottom of a listview, on my main activity the ads works, the only difference there is that I use a viewpager instead of listview.
This is the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.ads.AdView
xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:loadAdOnCreate="true"
android:background="#313131"
app:adSize="SMART_BANNER"
app:adUnitId="MY_ID" />
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/adView" />
</RelativeLayout>
The listview shows on TOP of the ads and of course can't be clicked, but this same layout works with a viewpager.

Another way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.google.ads.AdView
xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:loadAdOnCreate="true"
android:background="#313131"
app:adSize="SMART_BANNER"
app:adUnitId="MY_ID" />
</LinearLayout >
When Ad - is loaded, get his height. Then ListView bottomMargin = adHeight and Ad topMargin = (-1)*adHeight. Try it.

Final layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.ads.AdView
xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:loadAdOnCreate="true"
android:background="#313131"
app:adSize="SMART_BANNER"
app:adUnitId="MY_ID" />
<LinearLayout
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/adView" />
</RelativeLayout>
And then replaced:
getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
With:
getSupportFragmentManager().beginTransaction().add(R.id.listview, list).commit();

<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#313131"
ads:loadAdOnCreate="true"
ads="SMART_BANNER"
ads="MY_ID" />
NB This is a layout question. It has nothing to do with Admob. If you are using a LinearLayout then just set layout_height="wrap+content" and layout_gravity="1" for the item that you want to expand to consume the unused space. In this case your ListView.
Your problem was really the layout_height="match_parent"
NB I also removed the unnecessary renaming of the ads namespace.

Related

Android Admob ad not shown in Activity with fragment. Why?

I have an activity as follows.
I launch it inside another activity using setContentView(R.layout.below); However, below.xmlcontains a pager (which is a fragment and has its own textview, webview etc.). My problem is even though the Google Ads block is shown in the graphical layout on Eclipse and NO error is thrown when I run the java code shown below; I CAN NOT see the ad. I don't understand what is happening! I've tried so many things, including changing this layout to a simple relative layout. The following is how I call this adView.
setContentView(R.layout.below);
AdView mAdView2 = (AdView) findViewById(R.id.adView2);
AdRequest adRequest2 = new
AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
mAdView2.loadAd(adRequest2);
This is the actual XML file that contains this adView.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<LinearLayout
android:id="#+id/home_layout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_above="#+id/footerLayout">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</LinearLayout>
<LinearLayout
android:id="#+id/footerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="bottom"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
>
<com.google.android.gms.ads.AdView
android:id="#+id/adView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</LinearLayout>
</RelativeLayout>
try removing the padding of RelativeLayout, also happens to my app, but got it working after removing the padding
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="5dp" <--- REMOVE
android:paddingRight="5dp" > <--- REMOVE
if you must use a padding, add it on parent layout of the adview, so it will be on LinearLayout
Give some heights to ad view it's height depends on device screen size it can be 50dp,120dp
use this if nothing happens then please put different size and try one more time
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/home_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_above="#+id/footerLayout">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</LinearLayout>
<LinearLayout
android:id="#+id/footerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
>
<com.google.android.gms.ads.AdView
android:id="#+id/adView2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginBottom="0dp"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</LinearLayout>

Android Admob banner on all preferencescreens

I'm having trouble placing an AdMob banner in settings. I have managed to place it in the first screen by using the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:baselineAligned="false">
<fragment
android:id="#+id/admob"
android:name="ro.infiniteloop.phonehancer.AdmobFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment
android:id="#+id/settings"
android:name="ro.infiniteloop.phonehancer.SettingsFragment"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</LinearLayout>
The AdmobFragment layout being:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/AdmobLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="ro.infiniteloop.phonehancer.AdmobFragment" >
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-5599997006266549/8381977716" />
</LinearLayout>
And the settings fragment layout:
<PreferenceCategory
android:key="pref_key_storage_settings"
android:title="#string/pref_sms_storage_title" >
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_key_auto_delete"
android:summary="#string/pref_summary_auto_delete"
android:title="#string/pref_title_auto_delete" />
.....
An image of the output
All good so far. But when I go inside a child preference screen the original layout is reset and the ad is no longer displayed
An image of the child preference screen
Is there a way to lay these elements inside a layout or to write some custom code that keeps the ad on all preference child screens?
Create a layout containing only admob
admob.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/AdmobLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-5599997006266549/8381977716" />
now add this view in settings fragment.
getActivity().setcontentView(R.layout.admob);
Your ad will be visible

Not being able to place Admob right at the bottom of App

I'm having some issues with placing admob right at the bottom of the screen. For some odd reason, the Ad just hovers above the bottom.
I would like Admob to be right at the bottom of the screen, any help would he much appreciated!
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_high"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".HodgeMain"
android:layout_above="#+id/ad" >
<ListView
android:id="#+id/BoardList"
android:layout_width="wrap_content"
android:layout_height="270dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="93dp"
android:listSelector="#drawable/listcolor" >
</ListView>
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="XXXXXXXXX"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
</RelativeLayout>
try this
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_high"
tools:context=".HodgeMain">
<ListView
android:id="#+id/BoardList"
android:layout_width="wrap_content"
android:layout_height="270dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="93dp"
android:listSelector="#drawable/listcolor" >
</ListView>
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="XXXXXXXXX"
android:layout_centerHorizontal="true"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
</RelativeLayout>
I just removed unnecessarily things and made it bottom and center.
Your Adview is taking padding from bottom. Because the container of your Adview i.e. RelativeLayout have android:paddingBottom="#dimen/activity_vertical_margin"
Just move android:layout_above="#+id/ad" from RelativeLayout to ListView.

android layout overlay issue

I am a beginner in android.I made a flash game android app using a webview and integrated admob ad banner but it is overlaying the webview.I need it to appear like this pic http://i46.tinypic.com/2cosrjp.png
this is my 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/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="a1513fab869115a"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" />
</RelativeLayout>
add android:layout_below="+id/lay" inside you webView view
<RelativeLayout
android:id="#+id/lay"
android:layout_below="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
add android:layout_alignParentTop="true" in ads view
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
ads:adSize="BANNER"
ads:adUnitId="a1513fab869115a"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" />
Even though RelativeLayout might be the first choice for many use cases, for this simple view I would use a LinearLayout, that's much easier.
i got the oppiste error, i want my app displayed as yours and i cant get it, i got the answer that u want.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1">
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="355dp"
android:layout_height="wrap_content"
ads:adUnitId="000000000000000000"
ads:adSize="BANNER"
android:layout_gravity="center_horizontal|bottom" />
<WebView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/tropas"
android:layout_weight="1.01" />
</LinearLayout>
this is my code and display Aadmob on top Not overlaying webview

Admob ads not showing - Android

My ads don't display at all, I think I've followed the documentation correctly but they still won't show. The program is basically a webview and I want the ad to display at the bottom.
Heres my layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/man.utd.headlines"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<com.admob.android.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
myapp:backgroundColor="#000000"
myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC" />
</LinearLayout>
Any ideas?
EDIT: this is what I now have but it still doesn't appear to be quite right:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/man.utd.headlines"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.admob.android.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
myapp:backgroundColor="#000000"
myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC" />
<WebView
android:id="#+id/webview"
android:layout_above="#id/ad"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Your Problem is that the WebView will take all the space on the screen and there is no space left for the ads.
A LinearLayout will distribute the space on a first come first serve rule. If the first View takes all the space the second view won't get any space..
I would use a RelativeLayout and add the adds first with a layout_alignParentBottom attribute and then add the webview with a layout_above="id for the adds". This will ensure that the adds are always on the bottom of the screen even if the webview wont take all the space at the moment and the webview will always be above the adds.
I had the same problem, i fixed it this way:
LinearLayout as main layout, inside it linearLayout(for the ad) and a webview, set wrap_content on the linearlayout for the ad, so, it will first show an ad and then the rest of the screen will be the webview.
example of mine:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/addmob"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></LinearLayout>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<?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="fill_parent" android:id="#+id/rltvLayout1"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/linearLayoutwebview"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical">
<WebView android:id="#+id/webView1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fitsSystemWindows="true" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:id="#+id/ad_layout" android:layout_height="wrap_content"
android:gravity="bottom" android:layout_alignParentBottom="true"
android:layout_alignBottom="#+id/home_layout">
<com.google.ads.AdView android:layout_width="wrap_content"
android:layout_height="wrap_content" ads:adUnitId="put here your ID"
ads:adSize="BANNER" android:id="#+id/adView" ads:refreshInterval="60" />
</LinearLayout>
</RelativeLayout>

Categories

Resources