How to integrate admob into my Android app?
I have use this link: google code
but I think there are not all the information needed.
I'm not too certain about the tutorial, as I just worked through the API to get it to work. Here's my code:
AdView adView = new AdView(OOKL.this, AdSize.BANNER, MY_AD_UNIT_ID);
LinearLayout al = (LinearLayout) findViewById(R.id.AdLayout);
al.addView(adView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
adReq = new AdRequest();
adReq.setTesting(IS_DEBUG);
adView.loadAd(adReq);
adView.setAdListener(new AdListener() {
public void onReceiveAd(Ad ad) {
setAdVisible(true);
}
public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode error) {
setAdVisible(false);
}
public void onPresentScreen(Ad ad) {
}
public void onDismissScreen(Ad ad) {
}
public void onLeaveApplication(Ad ad) {
}
});
private void setAdVisible(boolean show) {
View v = this.findViewById(R.id.AdLayout);
v.setVisibility(show ? View.VISIBLE : View.GONE);
}
This is pretty much all you need. Here IS_DEBUG is my private variable controlling whether I'm running in test mode or live production. MY_AD_UNIT_ID is the ad unit ID you get when you register the app with AdMob. AdLayout is a LinearLayout that would contain the AdView - it's the first child in a vertical LinearLayout.
Related
I'm working in my Android application and i integrated native ads from Google Admob and it works well as shown in the picture below. But my problem is i want it refresh itself automatically. And this is my code used to refresh the ad. Any ideas? Thanks.
private void refreshAd() {
AdLoader.Builder builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID);
builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
// OnUnifiedNativeAdLoadedListener implementation.
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
if (nativeAd != null) {
nativeAd.destroy();
}
nativeAd = unifiedNativeAd;
FrameLayout frameLayout =
findViewById(R.id.fl_adplaceholder);
UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
.inflate(R.layout.activity_ads, null);
populateUnifiedNativeAdView(unifiedNativeAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
NativeAdOptions adOptions = new NativeAdOptions.Builder()
.build();
builder.withNativeAdOptions(adOptions);
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(MainActivity.this, "Failed to load native ad: "
+ errorCode, Toast.LENGTH_SHORT).show();
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
}
I'm using the basic example to add ads interstitial ads in my app with admob and firebase:
public class MainActivity extends ActionBarActivity {
InterstitialAd mInterstitialAd;
Button mNewGameButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNewGameButton = (Button) findViewById(R.id.newgame_button);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
beginPlayingGame();
}
});
requestNewInterstitial();
mNewGameButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginPlayingGame();
}
}
});
beginPlayingGame();
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
mInterstitialAd.loadAd(adRequest);
}
private void beginPlayingGame() {
// Play for a while, then display the New Game Button
}
}
Now, I have two activities (A and B) . I want when Activity A is open, load the ads, and when Activity B is open, show the same ads loaded in A, Please help me and sorry for my bad English! I ask in this language because the English community is more effective
It seems ok for me. The ad is displayed after buttton click, if its loaded. So in case you start B activity in beginPlayingGame all will be ok. Fact that ad is started from A activity not B is detail.
Did you want somethink else?
Of course, it is possible.
You have to store interstitial instance somewhere outside the Activity class (e.g. in Application class static variable) and implement listener outside this Activity.
That's it!
I am trying to add interstitial ads to my game. It is built on a framework from a book. The constructor is:
public GameScreen(Game game) {
super(game);
world = new World();
clueLetters = new String[10];
levelNo = SettingsObject.levelUnlock;
getWord();
interstitialAd = new InterstitialAd(AndroidGame.context);
interstitialAd.setAdUnitId("ca-app-pub-1861496496821617/");
//Create an ad request
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
AndroidGame.activityReference.runOnUiThread(new Runnable() {
#Override
public void run()
{
// Set AdListener
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
super.onAdClosed();
}
});
}
});
Looper.prepare();
interstitialAd.loadAd(adRequestBuilder.build());
}
However, I get the error:
java.lang.IllegalStateException: setAdListener must be called on the main UI thread.
Can you tell me why I am getting that error and how to fix it, please?
I use the a similar/same framework. The best way is to set up the interstitial ads in the AndroidGame activity class and use an interface to show the ads in your game screen:
First create the interface class:
public interface MyActivityListener {
public void showInterstitial();
}
Then in your AndroidGame class, implement the interface:
public abstract class AndroidGame extends Activity implements Game, MyActivityListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
interstitialAd = new InterstitialAd(AndroidGame.context);
interstitialAd.setAdUnitId("ca-app-pub-1861496496821617/");
//Create an ad request
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
interstitialAd.loadAd(adRequestBuilder.build());
// Set AdListener
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
super.onAdClosed();
}
});
}
#Override
public void showInterstitial() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if(interstitialAd != null && interstitialAd.isLoaded())
interstitialAd.show();
}
});
}
}
With this, any time you want to show ads in any screen, you just get call the showInterstitial() method. Example in your game screen:
public class GameScreen extends Screen{
MYActivityListener mL;
public GameScreen(Game game) {
super(game);
mL = (MyActivityListener)game;
world = new World();
clueLetters = new String[10];
levelNo = SettingsObject.levelUnlock;
getWord();
mL.showInterstitial();
}
}
I use this implementation to do all work that requires running on the UI thread (hide and show banner ads, rate/share app, storing scores etc. Hope it helps :)
I am using andengine and I am switching between two scenes. My intention is to put an interstitial ad between the transition. This is my code so far:
public void showGameOver(){
mainactivity=new MainActivity();
mainactivity.runOnUiThread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
interstitial = new InterstitialAd(mainactivity);
interstitial.setAdUnitId("MY AD UNIT ID");
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
if (interstitial.isLoaded()) {
interstitial.show();
}
}
});
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
}
#Override
public void onAdClosed() {
// Proceed to the next level.
setCurrentScene(gameover);
res.engine.setScene(gameover);
gameover.loadResources();
gameover.create();
}
});
}
The above code( without the interstitial ad code) switches to my gameover scene. When I put the interstitial ad code, my app crashes. The interstitial ad is supposed to show the ad, and when it closes, my gameover scene should show. I put in the correct ad unit id.
Logcat says: cannot create handler inside thread that has not called looper.prepare().
I searched this up and solutions state that I must put it on the runonuithread method, which I did.
Any help will be appreciated.
You can't instantiate a new activity via mainactivity=new MainActivity(); and use that. Instead you should pass the game's underlying activity to your gameOver() method and use that:
public void showGameOver(Activity yourActivity){
yourActivity.runOnUiThread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
interstitial = new InterstitialAd(mainactivity);
interstitial.setAdUnitId("MY AD UNIT ID");
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
if (interstitial.isLoaded()) {
interstitial.show();
}
}
});
I new in AdMob. At first I'm trying to create a test app with different types of ad.
I use real ad_unit_id and nexus 4 for testing.
I use 1 layout xml for all activity:
<?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:id="#+id/empty_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
When i create simple activity with banner ad it's work fine. Activity code:
public class AdBannerActivity extends Activity {
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ad_empty);
adView = new AdView(this, AdSize.SMART_BANNER, getString(R.string.adUnitId));
adView.setAdListener(this);
LinearLayout layout = (LinearLayout) findViewById(R.id.empty_layout);
layout.addView(adView);
adView.loadAd(new AdRequest());
}
#Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
When i try to create activity with full screen ad, i have error message:
Ad request successful, but no ad returned due to lack of ad inventory.
Activity code:
public class AdInterstitialActivity extends Activity implements AdListener {
private InterstitialAd interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ad_empty);
interstitial = new InterstitialAd(this, getString(R.string.adUnitId));
interstitial.setAdListener(this);
AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
#Override
public void onReceiveAd(Ad ad) {
if (ad == interstitial) {
interstitial.show();
}
}
#Override
public void onDismissScreen(Ad ad) {
}
#Override
public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
Log.e("AdTest", "Error code: " + error);
}
#Override
public void onLeaveApplication(Ad ad) {
}
#Override
public void onPresentScreen(Ad ad) {
}
}
What am I doing wrong?
How to solve this problem?
Solution: Create new ad_unit_id for interstitial ads in AdMob account.
If your have that error, then your code is correct. Admob just doesn't have any ads to display for your app (your country, your type of ads are determinants).
If you want to test the behavior of your ads in test mode, you should take a look at this link :
https://developers.google.com/mobile-ads-sdk/docs/admob/additional-controls#testmode
EDIT : If you have just created your Admob account, it may take some times and some requests to deliver the first ads.
I saw somewhere that people had this error too by using custom size for banner, so be sure to check :
https://developers.google.com/mobile-ads-sdk/docs/admob/intermediate#play