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>
Related
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
I am integrating AdMob Banner ad in my Activity. The layout consist of a GridView and AdView.
What I Want:-
When ad is not shown, i.e. at start or when ad is getting downloaded or anything, I want the GridView to have all the screen. But when ad is to be displayed, it should cover required space below but do not overlap anything.
My Layout:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:weightSum="10"
android:id="#+id/ll_gridmain"
tools:context="mohits.app.picreddit.itemgrid">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:divider="#null"
android:numColumns="2"
android:background="#454545" >
</GridView>
<LinearLayout android:id="#+id/banner_view"
android:orientation="horizontal"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:visibility="gone">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
ads:adSize="BANNER"
ads:adUnitId="#string/ad_banner"
android:visibility="gone">
</com.google.android.gms.ads.AdView>
</LinearLayout>
</LinearLayout>
My Activity:-
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("xxx")
.build();
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
mAdView.setVisibility(View.VISIBLE);
bannerView.setVisibility(View.VISIBLE);
}
});
What I am Getting:-
The banner_view layout is always showing even if I am setting it to GONE. This should only become visible when the ad is to be shown.
Thank you.
The problem is even if you remove your banner, the weight of the GridView will still be 9 and the weightSum of the layout is 10, so the GridView will not fill the whole LinearLayout.
Removing the following line from your layout should work:
android:weightSum="10"
Also, setting the visibility of the AdView is redundant.
Setting the visibility of a ViewGroup will also affect their children.
You could remove the following attribute from your AdView:
android:visibility="gone"
After that, setting the visibility of banner_view should be enough:
#Override
public void onAdLoaded() {
super.onAdLoaded();
bannerView.setVisibility(View.VISIBLE);
}
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.
I'm trying to setup an AdMob banner on the bottom of the page, but the Ad does not show.
Space appears the bottom of the page there, but the AdMob does not show there.
Add and reference the Google Play services library. - YES
Add a meta-data tag in AndroidManifest.xml. - YES
Declare com.google.android.gms.ads.AdActivity in the manifest. - YES
Set up network permissions in the manifest. - YES
Somebody tell me why.
Thank you.
My XML
<?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" >
<LinearLayout
android:id="#+id/flContainer" <!-- DATA Layout -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/ad_layout"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/ad_layout" <!-- AdMob Layout -->
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="vertical" />
</RelativeLayout>
MainActivity.java
// Create an ad.
adView = new AdView(context);
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.ad_layout);
//layout.getBottom();
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);
You told fldContainer to matchParent. THis means it will consume all the space of the parent, leaving none for your ad.
Instead try:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/flContainer" <!-- DATA Layout -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/ad_layout" <!-- AdMob Layout -->
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical" />
</LinearLayout>
I have followed this tutorial: AdMob Code
But I cannot get the banner to display (not even the common XML adSize error that I have seen a lot of online:
This is the code in my main_activity:
private AdView adView;
private static final String AD_UNIT_ID = "ca-app-pub-XXXXXXXXXXXXXXXXXX";
GoogleMap mMap;
LatLng myposition;
Marker marker;
#Override
protected 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.lyout);
layout.addView(adView);
//Create an ad request. Check kogcat 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("MY_DEVICE_ID")
.build();
//Start loading the ad in the background.
adView.loadAd(adRequest);
}
And this is my layout file:
<?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="#+id/lyout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/the_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTilt="45"
map:cameraZoom="15"
/>
</LinearLayout>
At first my layout file was only:
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/the_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTilt="45"
map:cameraZoom="15"
/>
EDIT: LogCat removed as working
But that wasn't working either. How do I get these ads to display?
Thanks
You can load the banner ad like below from the xml layout file:
<?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"
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"
ads:adUnitId="MY_AD_UNIT_ID"
ads:testDevices="TEST_EMULATOR"
ads:loadAdOnCreate="true"
ads:adSize="BANNER"/>
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/the_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTilt="45"
map:cameraZoom="15" />
</LinearLayout>
Note: if you have generated your appId just recently in admob account then may be there will be no advertise to display for your id to Admob engine so wait for a while and then try again. May be that will also work.
Hope it will help you..