Mauro Stancato
Mauro Stancato

Reputation: 577

How can I put the banner Adview on bottom programmatically?

I'm trying to put the banners and interstitial from adMob to my app but the banner shows on top. I want to put on bottom of screen programatically. I have tried doing this in the xml file but stills showing on top.

Java Class

    relativelayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
        RelativeLayout.LayoutParams params = new    RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);

    AdView mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.BANNER);
    mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
    mAdView.setPadding(0,30, 0, 0);
    relativelayout.addView(mAdView, params);

XML

    <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:id="@+id/RelativeLayout"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/background_light"
tools:context=".MainActivity">

<TextView
    android:id="@+id/tvcifra"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/gridlayout"
    android:layout_alignTop="@id/tvcifra"
    android:layout_marginBottom="2dp"
    android:textColor="@android:color/holo_blue_light"
    android:textSize="48sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.gridlayout.widget.GridLayout
    android:id="@+id/gridlayout"
    android:layout_width="match_parent"
    android:layout_height="121dp"
    android:layout_above="@id/adView4"
    android:layout_alignParentTop="true"
    android:layout_marginTop="8dp"
    app:columnCount="6"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tvcifra"
    app:orientation="vertical"
    app:rowCount="6">

    ...
    <button>1</button>
    <button>2</button>
    <button>3</button>
    <button>4</button>
    ...


</androidx.gridlayout.widget.GridLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView4"
    android:layout_width="660dp"
    android:layout_height="14dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-2703074771097768/1520884519"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/gridlayout" />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Views: 397

Answers (1)

Mohammed Abid Nafi
Mohammed Abid Nafi

Reputation: 519

Try adding this oh and don't forget to change ad unit id and also for width and height I don't know why you added some values but I would recommend keeping it as wrap_content

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="420dp"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id6"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"/>

Upvotes: 1

Related Questions