Admob advertisement seems like background - android

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

Related

How to make the ads appear on an activity in android?

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

Display adview in the bottom in linear layout

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

Android: admob ad at wrong place

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

ListView taking up all space, when trying to use an Admob Adview in Android

I am trying to get Google Admob advertisements to display on the main screen of an app that consists of a ListView. Unfortunately, the ListView is taking up all of the space on the screen, and then the advertisement appears over the top of it. I have tried putting the ListView at the top, followed by an AdView, and I've also tried putting the AdView at the top, followed by the ListView, but nothing works.
If I set the size of the LinearLayout enclosing the ListView to a fixed height (eg, 200px) then it it limits the size and fixes the problem, but I don't want to do this, because screen heights of Android devices vary so much. Is there no way to tell the ListView to not take up all the space without setting a fixed size?
My code is below, I'd be grateful for any help:
<?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:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_alignParentTop="true"
android:id="#+id/ad_layout"
android:orientation="vertical"
android:gravity="top"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="AD_UNIT_ID"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentBottom="true"
android:layout_below="#+id/ad_layout"
android:orientation="vertical"
android:gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/no_hosts"/>
</LinearLayout>
</RelativeLayout>
Both of your LinearLayouts have height of fill_parent. That means that they'll both be the full height of their parent. Clearly not what you want. Try this:
<?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">
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="AD_UNIT_ID"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/no_hosts"/>
</LinearLayout>
You have a relative layout. you need to set the listview property to layout_below="yourAdLayout" or rename the parent layout to linear layout. the orientation flag is not used in a relative layout, only in linear.

Adding Admob into ScrollView -> AdBanner disappears

Hey folks
I managed to implement Admob into a normal linear layout so far.
Now I added a additional scrollview and the adbanner disappeared. I don´t know what I can do against it.
Follows the code from the .xml where I added the scrollview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="match_parent">
<ScrollView android:id="#+id/scrollView1" android:layout_height="wrap_content" android:layout_width="match_parent">
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
[whole bunch of layout elements whoch shouldn´t affect the adbanner]
</LinearLayout>
</ScrollView>
In my linear layout ,where the adbanner still works, the whole adbanner position was done in the main activitiy.java file (did this with help of the tutorial at taiic.com)
// Lookup R.layout.main
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
// Create the adView
// Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
String pubID = "xxxxxxxxxxxxxxxxxx";
AdView adView = new AdView(this, AdSize.BANNER, pubID);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
Can anybody tell me what to change or what code to add when implementing an admob banner into a scrollview?
edit:
i tried to add
<com.admob.android.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
myapp:backgroundColor="#000000"
myapprimaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC"
android:alignParentBottom="true"/>
between last two lines in the .xml
</LinearLayout>
[here]
</ScrollView>
but then im getting the error "error: Error parsing XML: unbound prefix"
cheers
About the parsing error:
Is this a typo in the question? myapprimaryTextColor="#FFFFFF" instead of myapp:primaryTextColor="#FFFFFF" . This would give you the xml parse error.
About the layouts:
Use a RelativeLayout. The working code is at the end of the post. First, some theory :)
Your ScrollView is taking the whole screen, that's why you don't see the admob view. When the scrollview is defined, all the screen is availabel to it, so it takes it. The admob view is actually drawn below your screen. It can be reproduced in this example:
non-working layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:id="#+id/scrollView1"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="300dp"
android:text="Test1"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="300dp"
android:text="Test2"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="300dp"
android:text="Test3"
/>
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test4"
/>
</LinearLayout>
If you use a RelativeLayout instead, you can set it up so the admob is aligned to the bottom of the screen, and the scrollview above it, taking the rest of the available space.
working layout
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test4"
android:id="#+id/test4"
android:layout_alignParentBottom="true"
/>
<ScrollView android:id="#+id/scrollView1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_above="#id/test4"
>
<LinearLayout android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="300dp"
android:text="Test1"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="300dp"
android:text="Test2"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="300dp"
android:text="Test3"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I used the admob xml version and that is what I use and it works. The ad is at the top. Just copy and paste and you will be scrolling along shortly.
<?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/com.yourproject.here"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="a14dc1c9d6xxxxx"
ads:adSize="BANNER" />
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
[whole bunch of layout elements whoch shouldn´t affect the adbanner]
</ScrollView>
</LinearLayout>

Categories

Resources