I put some code of ads and AdMob but cant watch them - android

I am creating an app and want to add some Google Ads to the main page. I followed all the steps that Google says and don't have any warnings or error, but when I run the app, there isn't any ads.
This is the MainActivity code:
public class MainActivity extends AppCompatActivity {
private FragmentTabHost tabHost;
private AdView mAdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//adds
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
This is the xml code of the mainActivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="35dp"
android:orientation="vertical"
android:gravity="center"
android:background="#drawable/mix">
<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="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
<Button
android:id="#+id/natacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_bg_rounded_corners"
android:onClick="lanzarNatacion"
android:layout_marginTop="80dp"
android:textSize="20dp"
android:text="Natacion" />
<Button
android:id="#+id/carrera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="lanzarCarrera"
android:background="#drawable/button_bg_rounded_corners"
android:layout_marginTop="80dp"
android:textSize="20dp"
android:text="Carrera" />
</LinearLayout>
This is the mainifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="soloviev.perez.vicente.ritmos">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_mi_icono_"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:roundIcon="#mipmap/ic_mi_icono_"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".carrera" android:label="Calculadora ritmo de carrera" />
<activity android:name=".natacion" android:label="Calculadora ritmo de natacion" />
<activity android:name=".error" android:label="Error" android:theme="#android:style/Theme.Dialog" />
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-4666536989991556~5464124026"/>
</application>
</manifest>
And I added all dependencies and repositories I had to add.
When I run just appear a label which says "Test Ad", but should appear something more.
The codes that i am using are the example codes that Google provides.
Thanks

please add net work permission in manifests
<uses-permission android:name="android.permission.INTERNET"/>
change your code
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
with
mAdView = findViewById(R.id.adView)
AdRequest adRequest = AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build()
mAdView .loadAd(adRequest )
update
log the error code
mAdView.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
Log.d("Error code",errorCode.toString());
}
}
and check the error code here
https://developers.google.com/android/reference/com/google/android/gms/ads/AdRequest#ERROR_CODE_INTERNAL_ERROR
Update
in your gradle
implementation 'com.google.android.gms:play-services-ads:15.0.1'
Add end of your build.gradle(Module:APP)
repositories {
maven { url "https://jitpack.io" }
google()
jcenter()
}

try to update ads dependency
implementation 'com.google.android.gms:play-services-ads:16.0.0'

Related

how to implant admob in android studio

these are the code i am using in android studio latest release
using dependence com.google.android.gsm:google-play-service 15.0.5
app did't show ads on app.
i also tried firebase but whenever i use that after installing app in android phone app crash.
or like wouldn't open.
mainactivity.java
package com.animefantasy.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
Button btShow;
AdView adView1,adView2;
InterstitialAd interstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btShow = findViewById(R.id.bt_show);
adView1 = findViewById(R.id.ad_view1);
adView2 = findViewById(R.id.ad_view2);
MobileAds.initialize( this,"ca-app-pub-6273282658515300~1329562880");
AdRequest adRequest = new AdRequest.Builder().build();
adView1.loadAd(adRequest);
adView2.loadAd(adRequest);
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId("ca-app-pub-6273282658515300/7818594399");
interstitialAd.loadAd(new AdRequest.Builder().build());
btShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
interstitialAd.show();
}
});
}
}
androidmannifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.animefantasy.myapplication">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/bt_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Show Ad"
android:textAllCaps="false" />
<com.google.android.gms.ads.AdView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ad_view1"
android:layout_centerHorizontal="true"
xmlns:ads="http://schemas.android.com/apk/res-auto"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-6273282658515300/3583835917"/>
<com.google.android.gms.ads.AdView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ad_view2"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
xmlns:ads="http://schemas.android.com/apk/res-auto"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-6273282658515300/2972875090"/>
</RelativeLayout>
1. It is not recommended to use two banners in the same activity.
2. Add into your AndroidManifest:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="your_app_id_for_ads"/>
...

Admob Banner Ad not loading in Android

My Admob Banner ad in not loading.
Toast displays that "Banner add is loaded".
I tried many other solutions provided on stackoverflow but none seemed to work for me.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private AdView mBannerAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBannerAd = (AdView) findViewById(R.id.banner_ad);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("A5E3E2068BD88202CBC281AD7XXXXXXX")
.build();
mBannerAd.loadAd(adRequest);
mBannerAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
Toast.makeText(getApplicationContext(), "Closing the Banner Ad", Toast.LENGTH_LONG).show();
}
#Override
public void onAdLoaded() {
Toast.makeText(getApplicationContext(), "Banner Ad is loaded", Toast.LENGTH_LONG).show();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.akapoor.shayri.MainActivity">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp" />
<com.google.android.gms.ads.AdView
android:id="#+id/banner_ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3180111609438244/24179XXXXX" />
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akapoor.shayri">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ShayariText"></activity>
</application>
</manifest>
Assuming that:-
You have complete the steps mentioned here to display a test ad
successfully
This is not a test ad but an actual ad unit.
You have replaced test ad unit ID with the actual ad unit ID
If these steps are done, then It will take an hour or two to get your ad unit displayed in the app.
Your code looks fine. You should remove the test devices, addTestDevice, though:
Change:
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("A5E3E2068BD88202CBC281AD7XXXXXXX")
.build();
to:
AdRequest adRequest = new AdRequest.Builder().build();
Initialize MobileAds in onCreate() like this:
MobileAds.initialize(MainActivity.this,"YOUR_APP_ID));

why I can not see my InterstitialAd?

I am trying to make test ads both InterstitialAd and banner ad with hello world program
the banner add works fine but whe InterstitialAd does not appear when i open my decive on emulator
here is my code :
Androidmainfaist :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zaki.adstrial" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
</manifest>
MainActivity:
public class MainActivity extends ActionBarActivity {
private InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mInterstitialAd.setAdListener(new AdListener(){
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest2 = new AdRequest.Builder().build();
mAdView.loadAd(adRequest2);
}
activitymain.xml:
<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:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<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>
</RelativeLayout>
Where the problem
thanks in advance
i think you have just forgot to load the interstitial ad
before
AdRequest adRequest = new AdRequest.Builder().addTestDevice(
AdRequest.DEVICE_ID_EMULATOR).build();
after
AdRequest adRequest = new AdRequest.Builder().addTestDevice(
AdRequest.DEVICE_ID_EMULATOR).build();
mInterstitialAd.loadAd(adRequest);
You've correctly setup your InterstitialAd, but you didn't load anything to it. Your adRequest2 only loads the data for your banner ad, the mAdView.
That said, you seemingly has also correctly setup another AdRequest for your interstitial; you might just forgot to load it. So, all you need to do is to call this code:
mInterstitialAd.loadAd(adRequest);

Admob - Not able to show banner in android app

I am trying to check how admob works.
Till time I am done with including admob like this
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidbannertutorial"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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"/>
</application>
</manifest>
My XML File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- <com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adUnitId="ca-app-pub-4493208dfbf2258768/7749751738 "
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/> -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/ll"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
My main activity class
package com.example.androidbannertutorial;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
/**
* A simple {#link Activity} that embeds an AdView.
*/
public class MainActivity extends Activity {
/** The view to show the ad. */
private AdView adView;
/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "ca-app-pub-4493sdf352258768/7749751738";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.ll);
layout.addView(adView);
// Create an ad request. Check logcat output for the hashed device ID to get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
/*.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")*/
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
#Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
#Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
#Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
Ques: Why we use this .. ???????
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
I have included google_play_lib into my app.
I have my addUnitId : ca-app-pub-4493438885ds9958768/7749751738
Here my banner is not shown up within app.
Only error I am recieving is : The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
Even though I have configured with in app.
Can some one help me out what's I extra need to do ?
Thanks.
UPDATE
I am receiving ads after time lapse of 3-4 hours. Can't understand why this behavior occurs ??
Does your code request an Ad? If not then you won't get any ads.
And if you are requesting ads, then you will see evidence of that in the logs as well as the result of the request. Look at your log.
Note also that android:layout_alignParentBottom is not valid for a View within a LinearLayout
Add this in your code:
AdView adView = (AdView) view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);

admob does not show up in android

I'm a beginner,I'm trying to set an admob add in my app.But I have some errors witch I can't manage.
XmlFile
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.google.ads.AdView android:id="#+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
</RelativeLayout>
Manifest File.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admob"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application
android:allowBackup="true" android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:theme="#style/AppTheme" >
<activity
android:name="com.example.admob.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.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|scre enSize|smallestScreenSize" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
Java Code:
package com.example.admob;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends Activity
{
private static final String MY_AD_UNIT_ID = null;
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
RelativeLayout main = (RelativeLayout)findViewById(R.id.main);
main.addView(adView);
adView.loadAd(new AdRequest());
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adRequest.addTestDevice("TEST_DEVICE_ID");
}
#Override
public void onDestroy() {
adView.destroy();
super.onDestroy();
}
}
when Activity start this type error occur onFailedToReceivedAd(InvalidAdRequest).
I don't know what is the problem.
You are not loading the AdRequest which has the test devices registered. Change your code to:
//....
main.addView(adView);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adRequest.addTestDevice("TEST_DEVICE_ID");
adView.loadAd(adRequest);
In your relativelayout that contains the adview, are you appending the content layout after the adview. If so, what are the android:layout_width and android:layout_height values. If you are using fill_parent/match_parent for both, then your adview will never show.
The trick is to change the relativelayout to the linearlayout. Then for the content layout within this linearlayout, set its android:layout_width="fill_parent" and android:layout_height="0dp". Add a new attribute android:layout_weight="1" for the content layout. What these does is to ask the content layout to accomodate the remaining space of linearlayout after the adview has occupied its place. Hope this helps.

Categories

Resources