Ad baner without GoogleAdMobAdsSdkAndroid-6.4.1 Android example

First: AdView is in XML file

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adUnitId="MY_AD_UNIT_ID"
    ads:adSize="BANNER"/>
 
// onResume
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("TEST_DEVICE_ID")
    .build();
adView.loadAd(adRequest);

Second: Using AdView in Fragment with LinearLayout
Resolve error in ADT Graphical layout editor:
The following classes could not be instantiated:
– com.google.android.gms.ads.AdView

// layout in xml file
    <LinearLayout
        android:id="@+id/layout"
              android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
 
// java class with fragment in Fragment class or in Activity class
private static AdView adView;
        @Override
		public void onResume(){  
                super.onResume();
                 try {
                	 // in xml is empty layout
					adView = new AdView(getActivity());
					adView.setAdUnitId("ca-app-pub-626/638103xxxxxxx");
					adView.setAdSize(AdSize.BANNER);  
 
                	                LinearLayout layout = (LinearLayout)getView() .findViewById(R.id.layout);
					layout.addView(adView);
 
					AdRequest adRequest = new AdRequest.Builder().build();   
					adView.loadAd(adRequest);
 
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        }

More about:
https://developers.google.com/mobile-ads-sdk/docs/admob/android/play-migration?hl=it

This entry was posted in Android Examples Code and tagged , , , . Bookmark the permalink.


Comments are closed.



3,591 views