Reputation: 879
i am trying to add Ads to my current activity but the activity is not showing any Ad . i have also add internet permission to Android Manifest.xml
Here is my
.Gradle File
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "sms.and.call.alert.flashlight.lpa.com.myapplication"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services:9.6.0'
compile 'com.google.firebase:firebase-ads:9.6.0'
}
This is MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
// An example device ID
.build();
mAdView.loadAd(request);
}
}
This is MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="sms.and.call.alert.flashlight.lpa.com.myapplication.MainActivity">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
Upvotes: 4
Views: 16030
Reputation: 85
Maybe you forgot to load the Ad by calling:
AdView mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(request);
Upvotes: 0
Reputation: 44793
updated 10/04/2016 - MM/dd/yyyy
Step 1 : Be sure to have the latest version of Google Play Services and Google Repository
Step 2 : Put the google-services.json in the app folder after you had created the project on Firebase console
More Info:https://firebase.google.com/docs/android/setup#add_firebase_to_your_app
Step 3 : Set up your project level build.gradle as below.
More Info: https://firebase.google.com/docs/android/setup#add_the_sdk
buildscript {
// ...
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
// ...
classpath 'com.google.gms:google-services:3.0.0'
}
}
Step 4 : Setup your app level build.gradle as below:
More info: https://firebase.google.com/docs/admob/android/quick-start#integrate_firebase_and_the_mobile_ads_sdk
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "your.package.app"
minSdkVersion 9
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.google.firebase:firebase-ads:9.0.0'
}
apply plugin: 'com.google.gms.google-services'
Step 5 : Add those permissions to the AndroidManifest if they are not present yet
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Moreover be sure that your package name in the AndroidManifest match the applicationId of you app build.gradle or you will get a compilation error.
Step 6 : Continue to follow the Quick-Start tutorial and learn how to create Ad-Units to show ads on your views: https://firebase.google.com/docs/admob/android/quick-start
Step 7 : If you still have compile time problems, check those really usefull and common-case threads and examples:
Manifest and applicationId problems: https://stackoverflow.com/a/37177173/2910520,
Multiple flavors problems:
https://stackoverflow.com/a/34990999/2910520
Google Admob examples:https://github.com/googleads/googleads-mobile-android-examples
Upvotes: 7