AndreasWT
AndreasWT

Reputation: 183

Resize admob banner to screen size in android studio

I recently started using admob banner ads in my android apps. It's working very well, but I have some problems. As for now, I am using BANNER, and not SMART_BANNER. My banners works on most phones, except for a little group. On my emulator, I can see the problem occurs when the banner is too big for the screen.

I tried using smart banners, but I still get the same message in logcat: "banner needs 340x50. Only got 288x460". My banners work on most 5" screens, except for some. But when I try the app on smaller screen sizes, the banner isn't visible.

So my question is: How do i resize my banner to fit all screen sizes?

Here's my code that i use to create ad banner:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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.support.percent.PercentRelativeLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:id="@+id/timer1background2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="127dp"
            android:id="@+id/TimerTextview1"
            android:textColor="@android:color/black"
            android:textStyle="normal|bold"
            app:layout_marginTopPercent="11%"
            android:text="APP NAME"
            android:textSize="45dp" />

        <Button
            android:text="Go To Timer 1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:id="@+id/StartB1"
            android:textColor="@android:color/background_light"
            android:background="@color/colorPrimaryDark"
            android:textStyle="normal|bold"
            android:textSize="35sp"
            app:layout_marginTopPercent="33%"
            app:layout_heightPercent="15%"/>

        <Button
            android:text="Go To Timer 2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:id="@+id/StartB2"
            android:textColor="@android:color/background_light"
            android:background="@color/colorPrimaryDark"
            android:textStyle="normal|bold"
            android:textSize="35sp"
            app:layout_marginTopPercent="58%"
            app:layout_heightPercent="15%"/>

    </android.support.percent.PercentRelativeLayout>

    <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="MY KEY">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

And:

//Load ads
MobileAds.initialize(getActivity().getApplicationContext(), "MY KEY");

AdView adView = (AdView) myView.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

Upvotes: 2

Views: 8236

Answers (3)

abir-cse
abir-cse

Reputation: 546

Use SMART_BANNER which has dynamic ad sizes (Screen width x 32|50|90)

Replace ads:adSize="BANNER" by ads:adSize="SMART_BANNER"

Read more from here Banner Ads|Mobile Ads SDK (Android)

Upvotes: 3

Michael A
Michael A

Reputation: 5850

Regular banners come in fixed size and you cant extend them

What you need is smart banners which fill the width of the screen

Look at this

Upvotes: 0

SpiritCrusher
SpiritCrusher

Reputation: 21063

from the google documentation for ads, banner ads comes in particular sizes,see here, for full screen ad i think you should use Interstitial ads for it . Documented here.

Upvotes: 0

Related Questions