Fixed ad at the bottom of PrefereceActivity - android

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());

Related

Admob banner visible in preview mode, but not when running

Im trying to add a banner to my Android application, but can't get it to work. The banner is visible in the preview but not when running on my Genymotion device. This is my layout-file:
<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"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.my.package.MapsActivity"
tools:layout="#android:layout/list_content"
android:orientation="vertical">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="450dip"
tools:context="com.example.my.package.MapsActivity"/>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"/>
</LinearLayout>
And in the Activity-file I have this code:
MobileAds.initialize(mContext, "ca-app-pub-xxxxxxxxxxxxxxxxx~xxxxxxxxxx");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
It does not work with test ads either. What can be wrong?
Hank
One thing I would like to say that whenever you create a banner or interstitial than you have to wait for few hours for ads to display on your mobile screen.
Maybe your gmail account used in the Genymotion is the same with admob account. Try change to another gmail in your Genymotion.

Adjust the Layout when AdMob Banner get Displayed

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);
}

Add admob in app error

I try add admob Ads in my app. Initial I add code :
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("abc")
.build();
adView.loadAd(adRequest);
but it not run, it show error
11-23 11:16:20.285: E/AndroidRuntime(24361): FATAL EXCEPTION: main
11-23 11:16:20.285: E/AndroidRuntime(24361): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.phohaycom/com.example.phohay.activity.ReadingActivity2}: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
So I try add code :
AdView mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId("ca-app-pub-1845170371498416/12345678");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("D5AF9D9AFD269EF3B817CD9804BF2CE9")
.build();
mAdView.loadAd(adRequest);
It run but when I run it, it not show Ads which only displays text "Either XML attribute "ad Size" or XML attribute "supported Ad Sizes" should be specified, but not both.
My xml file :
<RelativeLayout 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:background="#ffffff" >
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</RelativeLayout>
it fails anywhere? Please help me
I think remove either of ads:adSize="BANNER" or mAdView.setAdSize(AdSize.BANNER); will help.
I have done it.
I don't add code in xml. Instead I create Linear Layout and add Ads in code
AdView adView = new AdView(getApplication());
adView.setAdUnitId("ca-app-pub-1845170371498416/123456789");
adView.setAdSize(AdSize.BANNER);
LinearLayout adLinLay = (LinearLayout) findViewById(R.id.Llayout);
adLinLay.addView(adView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(
"D5AF9D9AFD269EF3B817CD9804BF2CE9").build();
adView.loadAd(adRequest);
Thank everyone! good luck
Can you please try this one ? :
Remove this one:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
Delete the xmlns:ads*** tag
Add this one:
xmlns:ads="http://schemas.android.com/apk/res-auto"
xml file should looks like below:
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"/>
</LinearLayout>
Hope this will help you.

Instead of AdView an empty box appear( Android Element Layout Issue)

I'm trying to place AdView to layout:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/home_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"/>
</LinearLayout>
It is showing correct my content and the boxholder in the bottom of view, there is supposed to be an adView, but i see empty box and ads doesnt appear.
In my logs, i see:
Ad finished loading.
W/cr.BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 30285
Scheduling ad refresh 30000 milliseconds from now.
Ad finished loading.
Ad is not visible. Not refreshing ad.
What I'm doing wrong? Thank you in advance.
Following are possible errors
Your Banner ad unit id is not correct
You have not set ad properly on Google ad mob follow below link
follow this link
If my answer resolve your issue, please vote up
You have set the height of the inner LinearLayout to match_parent. THat will consume all of the available space leaving none for your AdView, so it will think it is not visible. CHange the LinearLayout to:
<LinearLayout
android:id="#+id/home_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
Got answer for my question. XML was correct, just Adblock havent initialized in workflow. To do that, I needed just initialize in activity adMob block in onCreate Action:
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Also import depreacted libraries:
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
Thank others for help and suggestions.

android - adview is empty in the bottom of the listView

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.

Categories

Resources