I've been working to update to the new AdMob that requires google services to function.
I'm using this code in onCreate()
AdView adView = (AdView)this.findViewById(R.id.adView);
//AdRequest
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
adView.loadAd(adRequest);
Log.d("ADMOB", "Successfully loaded");
and am using this code in XML
<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"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal"
ads:adSize="SMART_BANNER"
ads:adUnitId="ADMOB_ID"/>
However, the adMob ad does not display. The try/catch block fails here:
adView.loadAd(adRequest);
The AndroidManifest code looks like this:
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
What am I missing?
Referring to the AdMob Android Guides I quote
Next, create the onActivityCreated method. This is where you'll build and load the AdRequest. Reference the AdView, then build and load the AdRequest.
So, you must implement it like following :
#Override
public void onActivityCreated(Bundle bundle)
{
super.onActivityCreated(bundle);
AdView mAdView = (AdView) getView().findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
Related
I tested a banner ad with a test ID and it was showing fine, after that I procceeded to change the adUnitId to the actual Id AdMob gave me and I initialized MobileAds in application clas. I published my app on the play store, linked it to AdMob, and configured a payment profile. AdMob says the banner ad is active, but I downloaded my app and reinstalled it and the ads are not showing. Could i be missing some step?
Relevant Code:
this is the application class:
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
MobileAds.initialize(getApplicationContext(), "ca-app-pub-8983537837429131~3925299209");
}
The activity where the banner ad is supposed to show:
public class EntryActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
And it's xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/entryActivityLayout"
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"
xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context="com.bastardo.francisco.fitjournal.activities.EntryActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
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="My-banner-ad-Id"/>
</RelativeLayout>
As i said, test ads were showing just fine but the real ads are not showing. My app is linked with the play store in AdMob and i already configured a payment profile. Is there any other step? How long do I have to wait for apps to display?
Sometime the ads take few hours to show up, this is because there is not enough data to show relevant ads in your app.
If you app is current in the playstore try to download it in other device with diferent Google account, if you can se the ads so there is no problem.
My app just keeps crashing after I tryed to use banner ads with AdMob (without it everything works just fine). The SplashScreen loads but the app crashes when the game should start. Here are the steps that I followed to implemet this:
1) I made sure that I have installed Google Play Services and Google Repository
2) Then I connected my app to Firebase using Firebase Tool Assistant ( it just added google services.json under the app folder in my project )
3)Then I added the AdMob to my app, as shown here:
4)I added this codes in my layout:
xmlns:ads="http://schemas.android.com/apk/res-auto"
<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>
5)In my Main Activity class called "Game" I added this code to onCreate() method as you can see here:
public class Game extends Activity {
//ADMOB
private AdView mAdView;
MediaPlayer sound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//turn title off
requestWindowFeature(Window.FEATURE_NO_TITLE);
//set to full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GamePanel(this));
//ADMOB
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
int[] sounds={R.raw.chiptune, R.raw.chiptune1, R.raw.chiptune2, R.raw.chiptune3};
Random r = new Random();
int Low = 0;
int High = 4;
int rand = r.nextInt(High-Low) + Low;
sound = MediaPlayer.create(getApplicationContext(),sounds[rand]);
sound.start();
sound.setLooping(true);
}
6) Add this to my strings: <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
7) In my AndroidManifest.xml I added this pieces of codes:
<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--This meta-data tag is required to use Google Play services.-->
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
I wounder what's wrong, if anybody can help!
Here is the Crash Log:
findViewById(R.id.adView); returns a View if it exists in the layout you provided in setContentView(), otherwise it returns null and that's what happening to you.
mAdView is null so you're getting null pointer exception.
So create a layout.xml, embed GamePanel view and AdView both then pass that layout to setContentView(); method.
I have memory leaks regarding Interstitial ads of AdMob with AdActivity object. Whenever an ad is shown, AdActivity object count in the memory increments by 1. I inspect all changes via MAT after explicit GC calls. I use the latest versions of everything.
At first, I thought that this is related to how I implemented my UI or project, but creating a fresh and empty project shows the same leak.
This leak has also existed in the previous Admob SDK (Google Play Services) and now it exists in version 7.0 too.
I see that people try to solve these kinds of issues by creating a SingleInstance empty activity just to show and set as the context of the interstitial ads. I tried them all and they did not work for my case. Some did help but I even couldn't use it because of the flow of my app. launchMode in Android has limitations and it does not help me on my case.
I already notified the AdMob team but even they fix it, it doesn't seem to happen in a short time as they have just released the version 7.0 SDK.
I do not understand how others do not report leaks like this. It cannot be a special case just for me as it happens on even samples or default templates. If anyone somehow solved this issue (including ugly reflection hacks) please share your experience. I have been working on this for months! Really!
AndroidManifest:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="MyApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</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" />
</application>
MainActivity:
package com.example.leaktest1;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
private InterstitialAd interstitial=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
interstitial= new InterstitialAd(getApplicationContext());
interstitial.setAdUnitId("YOUR-ADD-ID");
AdRequest adRequest2 = new AdRequest.Builder()
// .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
if(interstitial.isLoaded()){
interstitial.show();
}
}
});
interstitial.loadAd(adRequest2);
}
#Override
protected void onDestroy() {
if(interstitial!=null){
interstitial.setAdListener(null);
interstitial=null;
}
super.onDestroy();
}
}
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="com.example.leaktest1.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Start the app. When the ad is shown, close it with backpress and hit back button again to exit app.
App will still be in memory, but the activity is gone. Now touch the app icon to start the activity again, it will show the ad again, exit like you did before.
Cause GC multiple times and get heap dump. You will see that there are 2 AdActivity objects (and also many other related objects). It will continue to grow according to the number of shown ads.
The following did not work too (it still leaks):
/*
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
}
});*/
interstitial.loadAd(adRequest2);
Runnable r=new Runnable() {
#Override
public void run() {
if(interstitial.isLoaded()){
interstitial.show();
}
}};
new Handler().postDelayed(r,10000);
And putting code inside a button did not work too (it still leaks):
Button b = new Button(this);
b.setText("Touch me");
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(interstitial.isLoaded()){
interstitial.show();
}
}
});
ViewGroup v = (ViewGroup) this.findViewById(android.R.id.content);
v.addView(b);
Leak platform and exceptions:
This leak happens on various devices from Samsung and Asus with various un-modded (original firmware) Androis systems from 2.3 to 4.4. It also happens on any setup of android simulator.
(This leak does not show up on rooted Cyanogenmod (Galaxy S3) Android 4.4.4)
UPDATE
The leak does not vanish if I use Activity context instead of Application context. It also causes Activity leak too.
I have been trying to get ads running on my android app however they just wont appear. I searched numerous websites but none of them had the answer. Eventually I started using this tutorial http://www.smartlab.at/articles/admob_advertising_android_tutorial/. I downloaded the example and it worked perfectly so I copied the code. However ads don't show up for my app.
I know receiving a "onfailedtorecieve(ad request successful) due to lack of ad inventory"
error means that my code is fine, however the thing is that I am in test mode, and during my numerous hours of trying to figure how to add ads, I discovered that ads should always appear in testmode even if there is a lack of inventory. I entered my device id that I acquired from logcat and everything but still no ads appear. Here is my code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
xmlns:myapp="http://schemas.android.com/apk/res/com.example.test"
android:id="#+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:debuggable="true"
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=".MainActivity" >
<com.google.ads.AdView
android:id="#+id/adview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="AD UNIT ID"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
/>
I made sure to replace "TEST_DEVICE_ID" with my actual device id and the "AD UNIT ID" with the ad unit id I got from admob.
Here is my code in my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE); //hide title bar
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout=(RelativeLayout)findViewById(R.id.main_view);
adView = (AdView)this.findViewById(R.id.adview);
adView.loadAd(new AdRequest());
}
Like I said nothing shows up. However if I put my ad_unit_id in the example i downloaded from the tutorial website, it works perfectly.
Here is their code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<com.google.ads.AdView
android:id="#+id/adview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="a1501676c633fac" <!-- This is the adunitid in the tutorial i downloaded-->
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
/>
</LinearLayout>
Here is their activity code:
package at.smartlab.androidbook.AdMobTest;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.app.Activity;
import android.os.Bundle;
public class AdMobTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Look up the AdView as a resource and load a request.
AdView adView = (AdView)this.findViewById(R.id.adview);
adView.loadAd(new AdRequest());
}
}
Interestingly, I don't have to put my actual device id in the xml code of the example I downloaded, it works fine whether I do put my id or I don't. Also, I can put either my adunitid or use theirs, it works fine either way.
I made sure to add the proper permissions in my androidmanifest.xml file and the proper tags. Honestly, I don't see why ads don't show up on my app but do in the example I downloaded. Any help would be appreciated. Thank you.
First off you're using an old tutorial.
The tags should now be
Com.Google.android.gms.adView.. Don't mind the random capital letters I'm on a mobile phone. You also need to make sure you have an SDK for google play services
XML
//at the top
xmlns:ads="http://schemas.android.com/apk/res-auto
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="your new pub Id goes here ca-app-something">
</com.google.android.gms.ads.AdView>
java
//calling ads
adView=(AdView)findViewById(R.id.adView);
AdRequest adRequest=new. AdRequest.Builder().build();
adView.loadAd(adRequest);
manifest
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
</activity>
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
code to test the adview
adView=(AdView)findViewById(R.id.MyAdView);
AdRequest adRequest=new.AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("7XXXXXXXXXXXXXXXXXXXXX").build();
adView.loadAd(adRequest);
I have a simple interstitial DFP (DoubleClick for Publishers) ad in my Android app. When I touch the cross to comeback to the app, the app closes itself.
The code is ok because it works in another app. Does anyone have an idea?
Here is the code in the manifest :
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize"/>
In the activity where I want the pub:
...extends Activity implements OnTouchListener, OnClickListener, OnErrorListener, AdListener, OnCompletionListener...
and the pub code:
private DfpInterstitialAd interstitial;
interstitial = new DfpInterstitialAd(this, "THE_PUB_CODE");
AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
interstitial.setAdListener(this);
I found the solution
There was a finish() in the method onUserLeaveHint()