Search code examples
androidwebviewadmobandroid-webviewadview

Align Webview at the bottom end of Adview


I have an AdView and a WebView in my Constraintlayout. The ads appearing on testing are 90dp height but in real situations might be 50dp or 30dp depending on the resolution screen. I use android:layout_marginTop="90dp" for my WebView to align it under the AdView.

Is there a way to change the marginTop value programmatically and align the WebView vertically under the AdViewbased on the height of the ad it shows?

Here is my activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

  <com.google.android.gms.ads.AdView
        android:id="@+id/adView1"
    
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/adview_border"
        app:adSize="SMART_BANNER"
        app:adUnitId="1232131231"
        app:layout_constraintTop_toTopOf="parent">

    <WebView
        android:id="@+id/webview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    android:layout_marginTop="90dp"
    android:layout_marginBottom="1dp"
    >
</WebView>

Any help appreciated.


Solution

  • You need to use layout_constraintTop_toBottomOf on the WebView and align it according to the AdMob. Below is a sample code you can try:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
    
        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"
            xmlns:app="http://schemas.android.com/apk/res-auto">
        
            <com.google.android.gms.ads.AdView
                android:id="@+id/adView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/adview_border"
                app:adSize="SMART_BANNER"
                app:adUnitId="1232131231"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>
        
            <WebView
                android:id="@+id/webview1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/adView1" />
        </androidx.constraintlayout.widget.ConstraintLayout>