How to add Google Admob Ads in Android App

In this tutorial, we are going to learn how we can integrate Google AdMob ads without violating Google policies of use. Some developers may just want to make money.

In this tutorial, we are going to learn how we can integrate Google AdMob ads without violating Google policies of use.
Some developers may just want to make money.Google advertising is one of the methods that you can make money by showing Google ads that are family friendly.Normally Google does not support sexual content.If you trying adding google ads in applications that have sexual content you eventually end up being burned.

Today we will focus on just google Interstitial Ads. The Banner, Native Ads, and Rewarded Video are for another day.
You can read more about Google AdMob here 

Interstitial Ads are the one that occupies the whole device screen and they normally require the user to click them so that they can close.

Banner Ads are the one that appears at the bottom of the screen.

NB:
If Ads are placed wrongly they can sometimes be nagging and they will make your application users give bad reviews.So make money wisely and let users interact with the application.

Let get started:

Open your IDE and create a new project:
Name:AndroidInterstitialAds

Package Name:package net.techoverload.www.androidinterstitialads

After creating the project, we need to add google dependancies to our buid.grandle (Module:app )  so that we can be able to use ads imports.

implementation 'com.google.android.gms:play-services-ads:11.8.0'

If your using Android Studio is pretty much easy adding com.google dependency, Click on project structure, the Ads, check on Admob check box.

After adding the dependency build.gradle(Module:app) should look like below

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
defaultConfig {
applicationId "net.techoverload.www.androidinterstitialads"
minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.android.gms:play-services-ads:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}


Generally, Interstitial Ads loads when the user clicks on application buttons or other views.In this example, we are going to load the Ad when the user clicks a button inside the application.Lets now create the activity_main  layout.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.techoverload.www.androidinterstitialads.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Google Ads Example"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/click_me"
android:text="Show Ad"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>


MainActivity.java

package net.techoverload.www.androidinterstitialads;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;

public class MainActivity extends AppCompatActivity {

private InterstitialAd interstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
MobileAds.initialize(this, "YOUR APPLICATION ID");

interstitialAd = new InterstitialAd(this);
//Load google test Ads
interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
interstitialAd.loadAd(new AdRequest.Builder().build());
Button button=(Button)findViewById(R.id.click_me);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(interstitialAd.isLoaded())
{
interstitialAd.show();
}
else
{
Toast.makeText(getBaseContext(),"Ad not loaded",Toast.LENGTH_SHORT).show();
Log.d("TAG","The interstial Ad was not loaded");

}
}
});



}

}

In above code, I have used google test Ads to load the ads unit

interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

Google has set aside tests ads to allow developers to test their ads without using live ads they created in their AdMob account.


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.techoverload.www.androidinterstitialads">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Run the application and you should see below the screen in your Android Emulator.


Download Project Source code 



How to add Google Admob Ads in Android App


Adding Listener to the Ad
 We can add a listener to the Ad by implementing AdListener interface.



interstitialAd.setAdListener(new AdListener()
        {
  public  void onAdClosed()
  {
  interstitialAd.loadAd(new AdRequest.Builder().build());

  }

        });

COMMENTS

Name

android android Apps android emulators android.app antivirus arrays blogger bluestack Break Statement in Java constructors download downloader drivers emulators fragment java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql node.js php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: How to add Google Admob Ads in Android App
How to add Google Admob Ads in Android App
In this tutorial, we are going to learn how we can integrate Google AdMob ads without violating Google policies of use. Some developers may just want to make money.
https://3.bp.blogspot.com/-_WSI9VwpiyQ/WqGt68BAr4I/AAAAAAAAKcA/azVRVT2V7QAe0XUP-D1i1Ge8KzZNGKDSgCLcBGAs/s320/android-interstitial-ads.PNG
https://3.bp.blogspot.com/-_WSI9VwpiyQ/WqGt68BAr4I/AAAAAAAAKcA/azVRVT2V7QAe0XUP-D1i1Ge8KzZNGKDSgCLcBGAs/s72-c/android-interstitial-ads.PNG
Java Codes
https://www.download-all.net/2018/03/how-to-add-google-admob-ads-in-android.html
https://www.download-all.net/
https://www.download-all.net/
https://www.download-all.net/2018/03/how-to-add-google-admob-ads-in-android.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy