Skip to content Skip to sidebar Skip to footer

Android Admob Size On Dimensions Error: A 'type' Attribute Is Required For

I have a layout and the attribute: ads:adSize='LARGE_BANNER' I want to put it with ads:adSize='@dim... dim name' but it gives me error for example: FULL_BANNER it says something

Solution 1:

You can dynamically load the ad's size based on screen resolution, programatically.

In your activity's class onCreate():

AdSizeadSize= AdSize.SMART_BANNER;

        DisplayMetricsdm= getResources().getDisplayMetrics();

        doubledensity= dm.density * 160;
        doublex= Math.pow(dm.widthPixels / density, 2);
        doubley= Math.pow(dm.heightPixels / density, 2);
        doublescreenInches= Math.sqrt(x + y);

        if (screenInches > 8) { // > 728 X 90
            adSize = AdSize.IAB_LEADERBOARD;
        } elseif (screenInches > 6) { // > 468 X 60
            adSize = AdSize.IAB_BANNER;
        } else { // > 320 X 50
            adSize = AdSize.BANNER;
        }

        LinearLayoutadContainer= (LinearLayout) findViewById(R.id.your_parent_layout);
        adView = newAdView(this, adsize, "xxxxxxxxxx");
        AdRequestadRequest=newAdRequest();
        adView.loadAd(adRequest);

        // Place the ad view.
        LinearLayout.LayoutParamsparams=newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        adContainer.addView(adView, params);

If you prefer defining the adview in xml format and referencing the values from the values-sw600, values-sw720.. folders you can define the width and height in dimens.xml:

In values/dimens.xml

<resources><!-- Default screen margins, per the Android Design guidelines. --><dimenname="admob_width">320dp</dimen><dimenname="admob_height">50dp</dimen></resources>

Then, in your layout:

<com.google.ads.AdView 
    android:id="@+id/adView"
    android:layout_width="@dimen/admob_width"
    android:layout_height="@dimen/admob_height"
    ads:adUnitId="your_unit_id"
    ads:adSize="SMART_BANNER"
    ads:loadAdOnCreate="true"/>

Post a Comment for "Android Admob Size On Dimensions Error: A 'type' Attribute Is Required For "