We are going to
- implementation 'com.google.android.gms:play-services-ads:20.1.0'
- java compile with xlint to check deprecation api
- gradle 6.5+
Edit build.gradle project
...buildscript { repositories { jcenter() google() } dependencies { classpath 'com.android.tools.build:gradle:4.1.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }}...allprojects { repositories { jcenter() google() } gradle.projectsEvaluated { tasks.withType(JavaCompile) { options.compilerArgs << "-Xlint:deprecation" } }}...
Edit build.gradle(:app)
...android { compileSdkVersion 30 buildToolsVersion "30.0.3"...dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.3.0' implementation 'com.google.android.gms:play-services-ads:20.1.0'}...
Edit AndroidManifest.xml
<manifest>... <application> ... <!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 --> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>...# main_activity.xml... <!-- 20210220 --> <android.widget.FrameLayout android:id="@+id/adContainerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/scroll_view" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" />...
Edit MainActivity.java
... private FrameLayout adContainerView; // 20210220 private AdView adView;... private Handler myHandler = new Handler(Looper.getMainLooper()); // new Handler() deprecated 20210220 ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ... MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } }); adContainerView = findViewById(R.id.adContainerView); // Step 1 - Create an AdView and set the ad unit ID on it. adView = new AdView(this); // Banner test = ca-app-pub-3940256099942544/6300978111 adView.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"); // CHANGE IT adContainerView.addView(adView); loadBanner(); ... } // 20210220 start private void loadBanner() { // Create an ad request. Check your logcat output for the hashed device ID // to get test ads on a physical device, e.g., // "Use AdRequest.Builder.addTestDevice("ABCDE0123") to get test ads on this // device." //AdRequest adRequest = new AdRequest.Builder().addTestDevice("ABCDE0123").build(); AdRequest adRequest = new AdRequest.Builder().build(); AdSize adSize = getAdSize(); // Step 4 - Set the adaptive ad size on the ad view. adView.setAdSize(adSize); // Step 5 - Start loading the ad in the background. adView.loadAd(adRequest); } private AdSize getAdSize() { // Step 2 - Determine the screen width (less decorations) to use for the ad width. //Display display = getWindowManager().getDefaultDisplay(); // deprecated 20210220 DisplayMetrics outMetrics = new DisplayMetrics(); // display.getMetrics(outMetrics); // deprecated 20210220 outMetrics = getApplicationContext().getResources().getDisplayMetrics(); // float widthPixels = outMetrics.widthPixels; float density = outMetrics.density; int adWidth = (int) (widthPixels / density); // Step 3 - Get adaptive ad size and return for setting on the ad view. return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth); } // 20210220 end...