Google AdMob does not show ads
I'm trying to put advertisements on my app, I put my app on amazon app store and then approved by google admob I set up Ad Unit ID (next level promotion) and put in both the xml and java part the appropriate code for it to work however I still don't get any Ad bar on the home of my app. Does anyone have any idea why it doesn't come up?
Java:
` MobileAds.initialize(this, initializationStatus -> {}); // Create an AdView and set the ad unit ID and ad size adView = new AdView(this); adView.setAdUnitId("*******"); adView.setAdSize(AdSize.BANNER); // Add the AdView to your layout LinearLayout adContainer = findViewById(R.id.ad_container); if (adContainer != null) { adContainer.addView(adView); // Load an ad AdRequest adRequest = new AdRequest.Builder().build(); adView.loadAd(adRequest); }`
XML:
`<com.google.android.gms.ads.AdView android:id="@+id/ad_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" app:adSize="BANNER" app:adUnitId="***" />`
First, I would recommend to follow the official guidelines to implement banner ads properly in your Android app.
Second, there could be various reasons for not getting ads, for example a recently created AdMob account or a newly created ad unit ID can cause problems.
Third, in your code, you implemented banner ads incorrectly. AdMob recommends that you set the ad size and ad unit ID in the same way (eg set in XML or both programmatically), but not both. When you set app:adSize and app:adSize in xml, there is no need to set the same properties programmatically.
Fourth, In xml you set the id of AdView as android:id="@+id/ad_container". But in code you are accessing AdView as LinearLayout like below:
LinearLayout adContainer = findViewById(R.id.ad_container); adContainer.addView(adView);And you are attaching the newly created AdView to an another AdView.
So what is the next step?
I am assuming your activity is MainActivity. Then change your code of implementation as below:
public class MainActivity extends AppCompatActivity{ private AdView mAdView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } }); //We will use R.id.ad_container here as per your XML mAdView = findViewById(R.id.ad_container); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); } }The above example is copied from Admob Documentation.
AND please don't use real ads for debugging, you should use Admob's Demo AdUnit Ids to avoid policy issues.
That's all I know. Hope it will help.