How do I make sure my webview can scale with devices? - android

I'm trying to get my webview to sit just above my Google Ad Mob ad. However the web view always covers the entire screen unless I programatically try and adjust the height. I've tried setting the height of the webview to the heigh of the screen subtracting my ad height, but that doesn't seem to work accurately on all devices. What should my height of my webview be set to if I want my ad to sit just below the webview?
<?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"
android:id="#+id/linearLayout">
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="320dip"
android:layout_height="50dip"
ads:adUnitId="/serviceid/myadtagishere"
ads:adSize="BANNER"
android:layout_below="#+id/webView1"/>
</RelativeLayout>

Try this :
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="320dip"
android:layout_height="50dip"
ads:adUnitId="/serviceid/myadtagishere"
ads:adSize="BANNER"
android:layout_alignParentBottom="true"/>
The add view should be above the webview and the webview fullscreen.

I was able to fix the problem using a linear layout. This seems to be the easiest and most effective way of getting a google ad mob ad to show up below a webview.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout"
android:orientation="vertical">
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="50dip"
ads:adUnitId="/5773/sci.app.hoosierscoop"
ads:adSize="BANNER"/>
</LinearLayout>

Related

AdMob banner at bottom - Converting Linearlayout to RelativeLayout

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

Display admob banner in linearlayout

I'm trying to display an admob banner in my listactivity but I'm running into problems. I followed the admob quickstart tutorial on the google developers website which uses relativelayout whilst I'm using linearlayout. I've tried tweaking my main xml according to previous answers on SO with varied results, sometimes the app stops/closes on launch and other times the listactivity remains blank. My original xml is
<?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:id="#+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null" />
<WebView
android:id="#+id/webview"
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">
</com.google.android.gms.ads.AdView>
</LinearLayout>
With that, the app runs fine but logcat says there's not enough space to display the ad, and when I move the adview above the listview the apps stops on launch. I've tried a different implementation with relativelayout as follows but this causes the app to close on launch as well
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_layout"
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="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
android:layout_alignParentTop="true"
android:layout_above="#id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
To request for the ad
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Logcat for ads is
03-27 12:24:38.697: W/Ads(2094): Not enough space to show ad. Needs 320x50 dp, but only has 480x0 dp.
03-27 12:24:38.847: I/Ads(2094): Scheduling ad refresh 60000 milliseconds from now.
03-27 12:24:38.887: I/Ads(2094): Ad finished loading.
03-27 12:24:38.887: W/Ads(2094): Not enough space to show ad. Needs 320x50 dp, but only has 480x0 dp.
03-27 12:24:38.897: W/Ads(2094): Not enough space to show ad. Needs 320x50 dp, but only has 480x0 dp.
Please help. Thanks in advance
Use layout_weight, try something like this:
<?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:id="#+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:layout_weight="1"
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null" />
<WebView
android:id="#+id/webview"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<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>

Banner doesnt show on the screen only graph

I have one problem if i use this code i cant see google AdView i dont know where is it :( Whatever i make it wasnt show but the banner in first page working good but this in second not :( Can anyoune suggest me
<?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:id="#+id/graph"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
ads:adSize="BANNER"
ads:adUnitId="#string/ad_unit_id" />
<com.calculator.grap.GraphPanel
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
</LinearLayout>
As it is a vertical layout it show on the top.Moreover you should not be using match_parent for both width and height of AdView.
You should use height as 0dp if you are using layout_weight.
Change the height of GraphPanel to 0dp.
Also rephrase your question in a better way.

AdMob ads taking up all screen space

After long hours of problems with getting ads to work it finally works.
But now my AdMob ads add a black background in its view/layout(I don't know)
So my game isn't visible anymore. The size of the ad itself is exactly how I want it,
but I need to get rid of this weird black background.
I suspect that I'm doing something wrong with layouts:
<?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:orientation="vertical" >
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-4118138803752321/8641047294"
android:orientation="vertical" />
</RelativeLayout>
What exactly is going wrong?
Because it's a RelativeLayout please assign the position of the AdView and that should take care of your issue.
Specify the position for your Adview within the RelativeLayout.
<?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"
>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-4118138803752321/8641047294"
/>
</RelativeLayout>

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