I am making this app in which i used viewpager to make tabs I have a fragments whose Xml file for the fragments goes like this.
RelativeLayout 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"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="12dp"
android:paddingRight="12dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="80dp">
<lumenghz.com.pullrefresh.PullToRefreshView
android:id="#+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="30dp"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="20dp"
android:id="#+id/list_item"
android:dividerHeight="18dp"
android:divider="#color/background_material_dark_1"
android:layout_marginLeft="12dp"
android:paddingBottom="30dp"
/>
</lumenghz.com.pullrefresh.PullToRefreshView>
</LinearLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxxxxxxxxxxxxxxxxxxx"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
I have added loaded the ad mobs from the on Activity created of the fragment.
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AdView mview = (AdView) code_chef_view.findViewById(R.id.adView2);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("A987C6AB63C6D5978CFCD37A5E0A1BD2").build();
mview.loadAd(adRequest);
}
But i stil have no luck, the app simply just doesn't show any add
Edit
I have added the test device.
But still in the log cat i get the following output.
05-25 22:53:25.563 3952-3952/com.example.illumy.codecompete I/Ads: Ad finished loading.
05-25 22:53:26.363 3952-4372/com.example.illumy.codecompete W/Ads: Error while pinging URL: https://pagead2.googleadservices.com/pagead/adview?ai=C5-JY9xInWZP4DcjMogPRgLSABKPT_P5Gu_zj1YcE5Kvp5bgBEAEgwuzcIGDlqueDxA6IAQGgAfvU0tcDqQLRte3gEgypPqgDAaoEyAFP0IrqdztxRahmvz2hxOH7_XDxCHUIUHuDbX-YI0V9FbcO3HVg3oGkgtTi1dQ41iSgY0pU1vpj-cNwreGF48ojYZrnpKrieaXXIPa7zdRJKMkiOvwgLmGfEub4zwChIoo8Ugus7thhcn5P9S-dSJN9FfcjgVQTw71RI-vchY_2jkYjB1xQjC8wNvE3izf0kLo8lvCSmwapJ_cEju4WzuKsRlK8LeEi156CmaHnM_SkMf6kjMOAkXje5LPd7GHP2a8SinlW7DIdnsAEt5ucv3yIBfW37cMCkAYBoAY5wAYLgAftqq0omAcBqAemvhvYBwGgCLTBPbAIAtIIBQiAARABsQnj9m_HuA3s-oIULRorbW9iaWxlYXBwOjoyLWNvbS5leGFtcGxlLmlsbHVteS5jb2RlY29tcGV0ZQ&sigh=QUko0PHK8eU&cid=CAASUeRo2clccqH7vOhLHEjtGjORhRNT7ouUYpJdPL0JXhtOrYzWxSDbugc8j2f6tOKTWbh5i-glOzCQgN_5yOidRaOw-qwaRSOhjbr37tIOCTlLNw&gvr=1. Unable to resolve host "pagead2.googleadservices.com": No address associated with hostname
05-25 22:53:36.433 3952-4369/com.example.illumy.codecompete I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
Related
i've sucessfully integrate Admob in my app . i have my Admob key . but it isn't working . it is not showing ads in app.
This is my XML . this is what i've done so far
<?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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.radioaudio.motivationalaudios.MyYouTubePlayer">
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/videoTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Arise, Awake and stop not till the truth is known - Radio Story | IIT Kanpur Radio"
android:textColor="#000"
android:textSize="20dp" />
<com.google.android.gms.ads.NativeExpressAdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub-5059726881726792/7013814664"
ads:adSize="280x132">
</com.google.android.gms.ads.NativeExpressAdView>
</LinearLayout>
my java code
NativeExpressAdView adView = (NativeExpressAdView) findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder()
.addTestDevice("A5E3E2068BD88202CBC281AD76984BEE")
.build();
adView.loadAd(request);
Native ads takes time to show. So kindly wait for 2-3 hours. They will pop up automatically. you might created your Account just a minute ago ..
I'd like to place ads in my MainActivity below FrameLayout like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 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"
xmlns:ads="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:id="#+id/fragmentContainer"
android:layout_centerHorizontal="true">
</FrameLayout>
<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>
I have added this to my Manifest:
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
And my build.gradle also looks fine. Here is my java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(getApplicationContext(),"ca-app-pub-xxxxxxxx~xxx");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // An example device ID
.build();
mAdView.loadAd(request);
But when I run the app I can't see the ads and in the LogCat I got this errors:
W/Ads: Invalid unknown request error: [Cannot determine request type. Is your ad unit id correct?]
W/Ads: There was a problem getting an ad response. ErrorCode: 1
W/Ads: Failed to load ad: 1
I have tried almost everything I found on the web but nothing worked for me. I think maybe the issue is in my activity_main.xml because of the FrameLayout doesn't give enough place to the ads.
Your code seems fine (although you should call MobileAds.initialize() from the onCreate() method of your Application class, not from an Activity).
Looks like the only problem could be the banner_ad_unit_id resource being an invalid ad unit ID (as the error message also suggests).
I'm using fragments in an Android app, in the Fragment class I'm calling admob in onViewCreated, the Admob banner shows without any problem in Smartphone when calling only one fragment, but not showing at all in the tablets when calling 2 fragments.
EDIT:
Just found in the logcat :
04-07 15:35:37.373: W/Ads(1380): Not enough space to show ad. Needs 1024x50 dp, but only has 340x471 dp.
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// TODO: load ads
mAdView = (AdView) getView().findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
mAdView.loadAd(adRequest);
}
In the fragment XML layout file, I've added the admob AdView :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white" xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
app:adSize="SMART_BANNER"
app:adUnitId="#string/admob_banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</RelativeLayout>
This is the layout file for sw720p calling 2 fragments:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/darker_gray" xmlns:tools="http://schemas.android.com/tools">
<fragment android:name="com.recipes.app.RecipesListFragment"
android:id="#+id/list_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"/>
<fragment android:name="com.recipes.app.DetailFragment"
android:id="#+id/detail_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</LinearLayout>
I'm facing a strange problem by adding adMob banner to my app.
It's first time i do this with android studio, so i've followed tutorial (the only different thing is the edit of build.gradle file).
I've add banner to layout as always:
<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:background="#drawable/bg_cut"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/banner" />
<com.google.android.gms.ads.AdView
android:id="#+id/banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/admob_id" />
</RelativeLayout>
and initialized banner into my activity:
AdView mAdView = (AdView) findViewById(R.id.banner);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Now when i launch my app, banner space is empty, and i obtain some warning like
W/Ads﹕ Not enough space to show ad. Needs 360x50 dp, but only has 328x479 dp.
but log told me that banner is loaded correctly:
I/Ads﹕ Scheduling ad refresh 40000 milliseconds from now.
I/Ads﹕ Ad finished loading.
How can i solve this problem?
You will see, successfully:
I/Ads﹕ Scheduling ad refresh 40000 milliseconds from now.
I/Ads﹕ Ad finished loading.
AdMob may or may not complain about "having enough room" but it will not show up if there is padding in the parent view. You need to wrap the views you want to have padding in either another layout or add padding to that particular view directly.
Please remove the padding from the parent 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_cut"
tools:context=".MainActivity">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/banner" />
<com.google.android.gms.ads.AdView
android:id="#+id/banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/admob_id" />
</RelativeLayout>
In case you're still struggling I've found a working example on http://www.ahotbrew.com/android-studio-admob-banner-and-interstitial-tutorial/
Just click on download code.
today Google suspended my app saying that my ads violated Google TOS but I don't know what did it actually violate. I think there might be something wrong with my code structure i guess because I tried publishing it two times and in both it got suspended.
In my app I am using a View Pager and inside it I have 4 tabs. So the ads are placed in the View Pager as below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<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"
app:adSize="SMART_BANNER"
app:adUnitId="#string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
and the code in the View Pager activity
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
I would be very thankful if someone told me the reason behind the continuous suspension.