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
Related
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>
I am pretty new to android. I am trying to add an admob banner at the bottom of an app, actually in two of the activities. I think Linearlayout is not the right way of doing it.
So please help me Convert Linearlayout to RelativeLayout.
Also while running the app, only the activity_entry.xml sample-google-banner shows up. The view_home.xml banner just shows up static white space.
And how do I make the banner smart? I mean I want it to show up only when theres a banner to be displayed, and not as a static blank space.
activity_entry.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/view_toolbar"/>
<fragment
android:id="#+id/entry_fragment"
class="net.etuldan.sparss.fragment.EntryFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
android:layout_gravity="center_horizontal">
</com.google.android.gms.ads.AdView>
view_home.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:showIn="#layout/activity_home"
android:weightSum="1">
<include layout="#layout/view_toolbar"/>
<fragment
android:id="#+id/entries_list_fragment"
android:name="net.etuldan.sparss.fragment.EntriesListFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
android:layout_gravity="center_horizontal">
</com.google.android.gms.ads.AdView>
Here are some tips for getting what you want:
look here for making it into relativelayout: adMob | Smart banner at bottom?
basically, the key code is that you should have
android:layout_alignParentBottom="true"
You can nest your layout if it helps... or not.
Second part: consider using adListener and use onReceiveAd() and onFailedToReceiveAd() to dynamically format your layout.
Look here for some points on adListener:
How to know if AdMob ad has been loaded
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 am trying to make the ads look as part of the app by placing them inside a view with a custom background. This is the result that I want to achieve - actually it's a preview from the layout editor:
When I run the app, however, the background and the "Sponsorized" text are not shown. How can I make it work?
This is my current layout for the view:
<LinearLayout 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="wrap_content"
android:orientation="vertical">
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="#string/admob_id"
ads:adSize="MEDIUM_RECTANGLE"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sponsorized"
/>
</LinearLayout>
Create an another inner LinearLayout as parent to AdView . Use this
<LinearLayout 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="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="#string/admob_id"
ads:adSize="MEDIUM_RECTANGLE"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sponsorized"/>
</LinearLayout>
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