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);
}
Related
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.
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 a PreferenceActivity for a LiveWallpaper and have used the solution here (LINK) to create an Admob ad. It works, but the ad disappers when the user scrolls up or down and loads again when it comes into view. Is there a way so that the ad always stays at the bottom (or top) of the activity (like in a regular activity)?
Before
((LinearLayout)view).addView(adView);
Try setting the layoutParam like
adView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT))
OR Change the layout like following:
<?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:id="#+id/mainLayout"
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"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="#string/ad_unit_id"/>
</RelativeLayout>
And code part would be like this:
mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdListener(new ToastAdListener(this));
mAdView.loadAd(new AdRequest.Builder().build());
I need to implement AdMob's AdView in the bottom of the Activity with the ListView. I added AbView to my layout sucessfully. My layout file looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/archiveLayout"
android:background="#FFFFFF"
>
<ListView
android:id="#android:id/list"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_above="#+id/adView"
android:background="#000000"
android:layout_height="wrap_content"
/>
<TextView android:id="#android:id/empty"
android:gravity="center|center"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="You havn't received any quote yet!"
/>
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="320dip"
android:layout_height="50dip"
ads:adUnitId="ID"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
android:layout_alignParentBottom="true"/>
and that is how I add it in my activity's onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.archive_list);
setListAdapter(new QuoteAdapter(this,
android.R.layout.simple_list_item_1, reverse(getContentDatabase(this))));
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.setEnabled(true);
adView = new AdView(this, AdSize.BANNER, "ID");
AdRequest re = new AdRequest();
re.addTestDevice(AdRequest.TEST_EMULATOR);
re.addTestDevice("E83D20734F72FB3108F104ABC0FFC738");
adView.loadAd(re);
}
it is shows properly with defined width and height in the bottom of the screen. But there's no content in it. Just the black empty screen. Tell me please what I can do with it.
I found it much easier to just add it solely in the xml file. It looks like you've blended the two ways together. You've got ads:loadOnCreate enabled, and you're also making a new one programmatically. Try doing it just the XML way, it loads up when the page is brought up, and I've had 100% fill rate using the xml only method. No need to complicate things if you're just trying to display it when the xml adview shows.
The problem was that I didn't receive ads:adUnitId. When I did it with the admob site everything works properly.