3

The layout is simple, One WebView and a Banner(AD).

I wrote an XML like below:

<LinearLayout
    android:id="@+id/banner_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:orientation="vertical">
    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>
</LinearLayout>

And here's a code to create a banner runtime.

// This code is copied & pasted from AudienceNetwork's guide page
adView = new AdView(this, "PLACEMENT_ID", AdSize.BANNER_HEIGHT_50);
LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);
adContainer.addView(adView);
adView.loadAd();

This code won't work, the banner is displayed outside of the screen. So, what I looking for is android:layout_height="match_parent - 50dp" just like calc in CSS.

mharti
  • 180
  • 8

2 Answers2

4

There is no such option of doing android:layout_height="match_parent - 50dp" in Android.

For integrating the Facebook audience network, you need to create an xml first with the banner_container. You can consider using a different layout structure like the following, using a RelativeLayout.

<?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">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/banner_container" />

    <LinearLayout
        android:id="@+id/banner_container"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" />
</RelativeLayout>
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
1

You can go with your existing layout just need to add a LinearLayout below Webview. Don't forgot to add android:layout_weight="1" in Webview

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"></WebView>

<LinearLayout
    android:id="@+id/banner_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" />

then use your existing code

adView = new AdView(this, "PLACEMENT_ID", AdSize.BANNER_HEIGHT_50);
LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);
adContainer.addView(adView);
adView.loadAd();
TN Yadav
  • 99
  • 2
  • 5