12

Using this package com.google.android.gms.ads.AdSize; I can set my adsize like this

adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);

But this gms.ads is old one. Is there any alternative way in which we can achieve the same thing in the new package ie, com.google.ads.AdSize; Thanks in Advance

Sunil Sunny
  • 3,949
  • 4
  • 23
  • 53

5 Answers5

8

You can set custom ads size by this way,

Custom Size

AdSize customAdSize = new AdSize(250, 250);
PublisherAdView adView = new PublisherAdView(this);
adView.setAdSizes(customAdSize);

you can set multiple AdSize parameters into setAdSizes()

Multiple ads sizes

adView = new AdView(this);
// adView.setAdSizes(AdSize.BANNER);
adView.setAdSizes(AdSize.BANNER, new AdSize(120, 20), new AdSize(250, 250));

you can check AdSize() Documentation

Hope this help you!

Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
  • i think you have to help last line of code to set adsize `setAdSizes(AdSize.BANNER, new AdSize(120, 20), new AdSize(250, 250));` – Jaykumar Patel Apr 04 '14 at 04:58
  • http://developer.android.com/reference/com/google/android/gms/ads/doubleclick/PublisherAdView.html – Sunil Sunny Apr 04 '14 at 04:59
  • i think this publisheradview is also from the old package that is com.gms – Sunil Sunny Apr 04 '14 at 05:00
  • @sunilsunny r u sure to import this packege `import com.google.android.gms.ads.*;` and `import com.google.android.gms.ads.doubleclick.*;` – Jaykumar Patel Apr 04 '14 at 05:02
  • no i don't want to import com.google.android.gms.ads I want to use the new package com.google.android.ads – Sunil Sunny Apr 04 '14 at 05:03
  • you can first import this 2 package import `com.google.android.gms.ads.AdSize;` or `import com.google.android.gms.ads.AdView;`. Now you can use adView.setAdSizes(AdSize.BANNER, new AdSize(120, 20), new AdSize(250, 250)); to set adsize. reply me – Jaykumar Patel Apr 04 '14 at 05:12
  • and i m update last 3 line code check and back on here i m wait – Jaykumar Patel Apr 04 '14 at 05:20
  • Is there a way to set this on the setAdListener – Sunil Sunny Apr 04 '14 at 05:25
  • check this 2 link help to you. https://developers.google.com/mobile-ads-sdk/docs/admob/intermediate#android or https://developers.google.com/mobile-ads-sdk/docs/admob/advanced#android – Jaykumar Patel Apr 04 '14 at 05:27
4

You can create a custom Ad dynamically as following

 View adContainer = view.findViewById(R.id.adMobView);
 AdSize customAdSize = new AdSize(150, 150);
        AdView mAdView = new AdView(context);
        mAdView.setAdSize(customAdSize);
        mAdView.setAdUnitId(context.getResources().getString(R.string.banner_ad_unit_id));
        ((RelativeLayout)adContainer).addView(mAdView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

in your xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:background="#000"
android:layout_height="wrap_content">

<RelativeLayout
    android:id="@+id/adMobView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   />

Amrutha Saj
  • 1,408
  • 16
  • 35
1

For Changing adsize you can change these lines in new Google Ads..

change this line accordingly..

adView.setAdSize(AdSize.BANNER);

You can see the details here..

https://developers.google.com/mobile-ads-sdk/docs/admob/intermediate

Banner Sizes

Google Mobile Ads supports the following ad formats:

Size (WxH) Description Availability AdSize Constant 320x50 Standard Banner Phones and Tablets BANNER 300x250 IAB Medium Rectangle Tablets MEDIUM_RECTANGLE 468x60 IAB Full-Size Banner Tablets FULL_BANNER 728x90 IAB Leaderboard Tablets LEADERBOARD See table Smart Banner Phones and Tablets SMART_BANNER

Hope you find the solution here..

PankajSharma
  • 1,529
  • 14
  • 27
1

you can easily set a custom size like this in XML.

ads:adSize="400x400"
zia Shahid
  • 51
  • 1
-1
// Define custom AdSize of 250x250 for PublisherAdView

    AdSize customAdSize = new AdSize(250, 250);

    PublisherAdView adView = new PublisherAdView(this);

    adView.setAdSizes(customAdSize);
Logic
  • 2,230
  • 2
  • 24
  • 41
Umer Afzal
  • 371
  • 1
  • 2
  • 18