tried few different methods,including Listeners/Creating Linear Layout,but banner is not shown at all (tried also with test device and emulator).
anyone has a solution for this ??
this is the MainActivity code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private GoogleApiClient client;
private AdView adView;
#Override
public void onBackPressed() {
getSupportFragmentManager().popBackStackImmediate();
super.onBackPressed();
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, "pub-6338964718220230");
AdView adView = (AdView) findViewById(R.id.adView);
adView.setAdUnitId("ca-app-pub-6338964718220230/1764788102");
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Log.d("Ad", "onAdLoaded: ");
}
#Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
}
});
AdRequest adRequest = new AdRequest.Builder().addTestDevice("E1CDC038C238379BFAB16A576EC21D17").build();
adView.loadAd(adRequest);
this is the fragment_main xml code:
fragment_main.xml
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/adView"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-6338964718220230/1764788102"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
</com.google.android.gms.ads.AdView>
this is the activity_main xml code:
activity_main.xml
<com.google.android.gms.ads.AdView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/adView"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
</com.google.android.gms.ads.AdView>
try to change size of ad display to a fixed one to see if the problem is the adSize, i got a similar problem too and was SMART_BANNER the problem
Related
I am trying to implement Admob ads in android activity but after everything done as per instruction given on firebase-Admob ads integration guide there is problem in showing ads in activity.What I have done so far is:
AndroidManifest.XML
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="MY_APP_ID" />
XML
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/ad_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="MY_ADUNIT_ID">
</com.google.android.gms.ads.AdView>
Java
public class Ad extends AppCompatActivity {
AdView ad_View;
AdRequest adRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ad);
MobileAds.initialize(this,#string/appid);
ad_View = findViewById(R.id.adView);
ad_View = new AdView(this);
ad_View.setAdSize(AdSize.SMART_BANNER);
ad_View.setAdUnitId(#string/adunit);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
adRequest = new AdRequest.Builder().build();
ad_View.loadAd(adRequest);
}
}
Someone please let me know what I am doing. Any help would be appreciated.
THANKS
Please try to remove this lines from your code,
ad_View = new AdView(this);
ad_View.setAdSize(AdSize.SMART_BANNER);
ad_View.setAdUnitId(#string/adunit);
because you have already set in XML
So try this,
public class Ad extends AppCompatActivity {
AdView ad_View;
AdRequest adRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ad);
MobileAds.initialize(this,#string/appid);
ad_View = findViewById(R.id.adView);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
adRequest = new AdRequest.Builder().build();
ad_View.loadAd(adRequest);
}
}
Note : Please make sure to add Internet Permission in AndroidManifest.xml
java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)'
on a null object r
public class Activity1 extends Activity {
Button btnrateapp, btnnext,btnshareapp;
int share,rate;
private AdView adView;
private InterstitialAd mInterstitialAd;
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
// MobileAds.initialize(this, "ca-app-pub-3297215203354227~5525786209");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-9817937406815779/5109921994");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
adView =findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
//mAdView.loadAd(new AdRequest.Builder().build());
adView.loadAd(adRequest);
You must add this code in your layout activity1.xml:
<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"
ads:adSize="BANNER"
ads:adUnitId="ad_unit_id_for_banner_here"/>
I follow the official documentation:
https://developers.google.com/admob/android/banner
But I can't find a mistake in my code. I tried to change
AdRequest adRequest = new AdRequest.Builder().build();
For the test AdRequest but it's still not working.
This is my source code:
https://github.com/sssolammm/HelloWordTestBanner
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, "ca-app-pub-5676983055182772~7282283135");
mAdView = (AdView) findViewById(R.id.ad_View);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.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 an ad opens an overlay that
// covers the screen.
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 user is about to return
// to the app after tapping on an ad.
Log.i("Ads", "onAdClosed");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** Called when leaving the activity */
#Override
public void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}
/** Called when returning to the activity */
#Override
public void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}
/** Called before the activity is destroyed */
#Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}
}
Xml layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:text="#string/hello_world" />
<com.google.android.gms.ads.AdView
android:id="#+id/ad_View"
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>
</RelativeLayout>
You should add your test device id to builder.
If it works with test id, it will be working on production.
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
.addTestDevice("your id") // Put here your test device id
.build();
For test device id, go in Logcat after run app, put in Verbose put in the search field AdRequest, so the id device shows down.
Well, sometimes ad is not shown...then i uninstall my application and change build variants from debug to release.
After then i install my app again...banner ad will be shown successfully.
even if banner ad is not showing..then i check the log info...and get the test device id..add this test device id to AdRequest object...and i am sure banner ad will be shown.
adRequest.addTestDevice("your id")
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
I have admob implemented into my game, but my game uses a surfaceview to display the graphics.
How would I "access" The AdMob View from within the Surface view?
EDIT 2:
Tried to implement callbacks:
MainActivity.class
interface AdMobInterface {
public void HideAd();
public void ShowAd();
}
public class MainActivity extends Activity implements AdListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new RelativeLayout(this);
engine = new Engine(this);
layout.addView(engine);
if(engine.IsDemoVersion) {
SetupAdMob();
}
setContentView(layout);
}
public void ShowAd() {
///Execute this from Engine.class
}
public void HideAd() {
///Execute this from Engine.class
}
private void SetupAdMob() {
String AdMobPublisherID = "XXXXXXXXXX";
adView = new AdView(this, AdSize.BANNER, AdMobPublisherID);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, params);
layout.bringChildToFront(layout.getChildAt(1));
adView.loadAd(new AdRequest());
}
}
Engine.class
public class Engine extends SurfaceView implements
SurfaceHolder.Callback, SensorEventListener, AdMobInterface {
AdMobInterface AdMob;
public Engine(Context context, AdMobInterface admob) {
super(context);
AdMob = admob;
}
}
Use FrameLayout and show the the add on top of the surface view. Something like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<AdView id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</FrameLayout>
Then all you need to do is find the AdView by ID and remove it from the parent layout.
View adView = findViewById(R.id.ad);
FrameLayout root = (FrameLayout)findViewById(R.id.root);
root.removeView(adView);
It is best to do this from inside the activity, there is no need for your 'Engine' to know about ads or layouts.