Skip to main content

How to integrate Admob interstitial ads using​ AIDE?

AIDE can be used to integrate Admob Ads to a Sketchware project. Follow the steps below to integrate Admob interstitial ads to a sketchware project using AIDE.

Prerequisites
  • An Android project (Sketchware project)
  • AIDE with pro account key purchased
  • Account in Google developer console
  • Account in Admob
Before placing admob ads in an app, the app needs to be uploaded to google play store, however test ads can be tried in any app.

Always place the test ad ID before placing your ad unit ID. App ID and ad unit ID can be obtained by registering the app on Admob. But for using test ads no registration is required.

Do not click on your own Ads.

Export the Sketchware project

In Sketchware, under MY PROJECTS, go to project settings of the app to be exported, and click on Export to PC (Android Studio).
The exported file is a zip file. Create a new folder and decompress the contents of the zip file in it.

Check whether the res folder at location app/src/main/res contains only layout folder with all xml files or it contains all other folders like drawable, drawable-xhdpi, raw, values, values-v21. If all these folders are present then move to the next step of editing AndroidManifest.xml.

Edit the AndroidManifest.xml file

Open AIDE, browse to the AndroidManifest.xml file of the exported project and open it.

In manifest element, after package name, add following:
 android:versionCode="2"
 android:versionName="1.2"
Change version code and version name as per your project.
After that add the following permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

In application element add your APP ID by adding following meta-data:
<application>
<meta-data
android:name = "com.google.android.gms.ads.APPLICATION_ID"
android:value = "ca-app-pub-3940256099942544~3347511713"/>
</application>

Important: This step is required as of Google Mobile Ads SDK version 17.0.0. Failure to add this <meta-data> tag results in a crash with the message: "The Google Mobile Ads SDK was initialized incorrectly."

Also add the following activity:
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />

Save the file after editing.

Edit MainActivity.java file

Add the following code in imports:
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.AdListener;

After the code 'public class MainActivity extends Activity {' add the following code:
private InterstitialAd mInterstitialAd;

After that create and load ad by adding following code after line 'setContentView(R.layout.activity_main);' in onCreate event:
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());

After testing with test ads replace the adUnitId with your own adUnitId.

Then after the code to create and load ad, add code for adListener just below it:

mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
Log.i("Ads", "onAdLoaded");
}

@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
Log.i("Ads", "onAdFailedToLoad");
}

@Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
Log.i("Ads", "onAdOpened");
}

@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
Log.i("Ads", "onAdLeftApplication");
}

@Override
public void onAdClosed() {
// Code to be executed when when the interstitial ad is closed.
Log.i("Ads", "onAdClosed");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});

Add code to show the ad

The code to display interstitial ads has to be added to the event or activity when the ad is intended to be displayed. The code for displaying ads is:
if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
           Log.d("TAG", "The interstitial wasn't loaded yet.");

        }

If you want to display ads on page start then it should look like this:

@Override
public void onStart() {
super.onStart();
if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
}

Changes to build.gradle file

Navigate to app level build.gradle and Add following to project:
'com.google.android.gms:play-services-ads:+'

Also, in build.gradle file change version code and version name to same as in AndroidManifest.xml.
Save the file.

After this if you find no errors, then run the project. If you find the error 'R cannot be resolved', then go to AndroidManifest.xml and make any small change like removing a space and then save the file again. If you find any other error, try to resolve as per the error message.

Watch the video below to understand better:


This post is only regarding integration of Admob interstitial ads. The App ID and Ad unit ID used here are those for test ads and can be used in any android project. The ad unit ID for interstitial ads and banner ads are different.

Comments

Popular posts from this blog

Create Music Player app in Sketchware

To create a Music Player app in sketchware, follow the steps given below. 1. Create a new project in Sketchware. 2. In VIEW area on main.xml, add a ListView  listview1  with height wrap_content, and weight 1. Add a SeekBar  seekbar1 , an ImageView  imageview1 , and three TextViews  textview1 ,  textview2 , and  textview3 , as shown in the image below. 3. Create a CustomView  mycustom.xml  and add a TextView  textview1 , and an ImageView  imageview1  in it. For listview1 select mycustom.xml as it's customView. 4. Add a MediaPlayer component  mp , a Shared Preferences component  sp:sp  and a Timer component  timer . 5. Create six More Blocks  MPcreate(pos), MPstart , and  MPpause  for the MediaPlayer and  searchFolders ,  getFileList in [filePath]  and  extra . 6. Add three number variables  n, r  and  songPosition , and five String variables  currentfile ...

Admob rewarded video Ads in Sketchware

To integrate Admob Rewarded Video Ads to a Sketchware project, follow the steps given below. Prerequisites An Android project (Sketchware project) Account in Admob Always place the test ad unit ID before placing your ad unit ID. App ID and ad unit ID can be obtained by registering the app on Admob. But for using test ads no registration is required. Do not click on your own Ads. 1. Create an android project in Sketchware. 2. Add the app to your Admob account. For your app, generate an ad unit ID for Rewarded Video Ads. 3. In Sketchware project, navigate to  Library Manager  and open Admob settings. i. Click on Add manually and add the ad unit ID generated on Admob website, or use test ad unit ID ca-app-pub-3940256099942544/5224354917. ii. If you are not using any banner or interstitial ad units, select the same ad unit ID for both banner and interstitial ads. iii. Add Test Device automatically shown by Sketchware. iv. Save it. v. Switch ON Admob and AppCompat and design. ...

Firebase auth in Sketchware for Login Activity

To create a login activity using Firebase Auth in Sketchware, follow the steps given below. This method uses Firebase authentication service for creating login. 1. In your Firebase account, go to Firebase authentication. 2. In Firebase authentication web set-up, go to SIGN-IN method, and enable Email/password and Anonymous. 3. Go to Project settings in your Firebase project and copy the Web API Key, Project ID, and App ID. 4. Paste the Project ID, App ID, and Web API Key in your project in Sketchware, in the Firebase settings. 5. On the  MainActivity  page add a File Shared preferences component  user:user  and an Intent  i . Also add a Timer  t  and a Firebase Auth  testlogin . 6. Create a new page  login.xml  with Activity called  LoginActivity . 7. In  onCreate  event of MainActivity use blocks as shown in image below. It identifies main page with  File user key page . It also checks if user is logged in to  F...