Admob Banner does not appear in my mainActivity - android

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")

Related

Admob interstitial ad latency issues when loading ad

I am trying to implement interstitial ad in a fragment, when button is clicked the fragment doesn't open other activity , but loads data in same fragment so i can't use the they way people say interstisial ad should be used when navigating between activities. instead i am using the ad for only once when button is clicked .it works but takes sometime .how to deal with it?
it only works if user pressed the button after few seconds of launching the app.
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_home, container, false);
Button btn=root.findViewById(R.id.btn);
initAds();
btn.setOnClickListener(this);
return root;
}
#Override
public void onClick(View view){
initAdsCallBack();
}
private void initAds(){
MobileAds.initialize(requireActivity(), new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(#NonNull InitializationStatus initializationStatus) {
createNonPersonalisedAd();
}
});
}
}
private void createNonPersonalisedAd() {
Bundle networkExtrasBundle = new Bundle();
networkExtrasBundle.putInt("rdp", 1);
AdManagerAdRequest adRequest = (AdManagerAdRequest) new AdManagerAdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, networkExtrasBundle)
.build();
createInterstitialAd(adRequest);
}
private void createInterstitialAd(AdRequest adRequest){
AdManagerInterstitialAd.load(requireActivity(),getResources().getString(R.string.str_iterstitial), (AdManagerAdRequest) adRequest,
new AdManagerInterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull AdManagerInterstitialAd interstitialAd) {
// The mAdManagerInterstitialAd reference will be null until
// an ad is loaded.
mAdManagerInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
///// best place to callback is here coz its successfully loaded here
mAdManagerInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
#Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d("TAG", "The ad was dismissed.");
createInterstitialAd(adRequest);
}
#Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d("TAG", "The ad failed to show.");
}
#Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
mAdManagerInterstitialAd = null;
Log.d("TAG", "The ad was shown.");
}
});
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
// Handle the error
Log.i(TAG, loadAdError.getMessage());
mAdManagerInterstitialAd = null;
}
});
}
private void initAdsCallBack(){
if (mAdManagerInterstitialAd != null) {
mAdManagerInterstitialAd.show(requireActivity());
debugToast("ad shown");
} else {
Log.e("Tad didn't show");
}
}
Consider pre-loading your interstitial ads to reduce latency when displaying them to your users. For more information about pre-loading your interstitial ads refer to the AdMob Interstitial Ad developer guidelines for apps developed for Android

Ad finished loading,but banner is not shown (AdMob banner)

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

Android InterstitalAd gray small bar bevor the ad

Im trying to get a InterstitialAd into my application.
It works now,
However:
1) I always get a small gray bar displayed on top of the screen during the ad is loading
2) The ad takes really long to load
any ideas how to fix these two problems?
BannerActivity:
public class BannerActivity extends Activity{
private InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-XXXXXX");
requestNewInterstitial();
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded(){
mInterstitialAd.show();
}
#Override
public void onAdClosed() {
finish();
}
});
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().addTestDevice("XXXXXXXXXXXX").build();
mInterstitialAd.loadAd(adRequest);
}
}
How i load the banner:
Intent bannerIntent = new Intent(myContext, BannerActivity.class);
myContext.startActivity(bannerIntent);
xml:
android:name="kletzgames.crazyballs.BannerActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
I fixed it:
I now call the Ad during another View so it can load, and ones it loaded it will be displayed during screenTransaction.
AdRequest adRequest = new AdRequest.Builder().addTestDevice("XXXX").build();
mInterstitialAd.loadAd(adRequest);
** some if's **
mInterstitialAd.show();

admob on other android appstores, impressions count does not change

I have the following problem with AdMob.
My app is published on Google Play, Opera Mobile Store and GetJar. It has 30 downloads from Opera Store, 120 from GetJar but AdMob is showing and counting only the impressions and clicks made from Google Play downloads. The problem is only with the other app stores 150 downloads for the last 1 week and not a single impression counted.
Any idea is something wrong with my code or it's something else?
I have removed the addTestDevice method calls.
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
...
}
#Override
protected void onResume() {
...
if (adView != null) {
adView.resume();
}
super.onResume();
}
#Override
protected void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
#Override
protected void onDestroy() {
super.onDestroy();
if (adView != null) {
adView.destroy();
}
}
<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:adUnitId="ca-app-pub-*******/******"
ads:adSize="BANNER"/>

Ad request successful, but no ad returned due to lack of ad inventory

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

Categories

Resources