4

I have about three Activities, and all these three activities have Banner ads at the bottom which are set by code in the OnCreate() method of the Three Activities.

And due to some reason I need to Finish each Activity while moving from one activity to the other, and startActivity() for coming back to the first Activity.

I wanted to know, how do i make just one Banner Ad for all these three Activity instead of calling them individually from different onCreate, because my doubt is that on transition of Activities I am refreshing Ads(quiet ofently) which isn't a good practice for your clicks.

Should I declare it in a static Class so that it can be called from any activity and just one instance would be there(so no refreshing due to activity creation)

Suggestions are welcome.

Some one Some where
  • 777
  • 1
  • 8
  • 26
  • 1
    Without knowing what each activity does or why you have to finish them as you move from one to the other, this may not be relevant....Have you thought about simply having a single activity with FrameLayout and (below that) the banner ad view? Instead of moving from one activity to the next, simply build the view of each activity and place it in the FrameLayout. The banner ad view would remain consistent throughout. Alternatively, depending on Android version, use Fragments instead of activities. Just an idea. – Squonk Mar 09 '12 at 05:56
  • Though you can set your ad banner common for all activities, a new ad request is send out to admob when each activity is show up. if user switch the activity very frequently, you might get an very low fill rate, Use one Activity + multiple Fragments instead, see [here](http://stackoverflow.com/a/30341571/1487475) – zdd May 20 '15 at 07:12

4 Answers4

2

Do you know about implementing ViewStub?

For your problem, ViewStub is used to place AdMob ads at Footer, you just have to create layout for this Footer and then include this layout in your XML layouts (activity layouts) by using ViewStub example.

Here is an example for implementing ViewStub, yes its for Title bar but you can take concept from it.

Now, to optimize solution (code), you can create an Abstract class and extends Activity class and include your AdMob ads code inside this class.

For example:

public abstract class BaseActivity extends Activity
{
   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

        public void setFooterAds()
        {
              // Make ViewStub visible
              // include your Ads code
        }
}

Now, you just have to extend this BaseActivity class in your Activity classes, and call setFooterAds() method to display AdMob ads.

CreativeMind
  • 897
  • 6
  • 19
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • I would be implementing it asap, to check how it works, I hope the layout doesn't get created again as I dont wish my Ads to get Refreshed, will get back after trying once. thanks – Some one Some where Mar 13 '12 at 11:11
  • Hi Paresh, Pls have a look at my problem i also have a problem in adding ads to my app.....http://pastebin.com/LpnLuRDT – Robi Kumar Tomar Feb 27 '14 at 13:20
1

I think the only way out here is to use single activity and multiple fragments..the activity will have a frame layout and a fragment containing Ad..While different screens (fragments) will be replaced depending on the UX, the Ad-containing-fragment will stay as it is, common to all screens!

Uncaught Exception
  • 2,149
  • 18
  • 25
1

There is a simple way to do it!

Smoked a "lumpia" to know that something like "ViewStub" existed, it is interesting and thanks for the tip @Paresh data, but too complicated for now for this purpose. There is a simpler way to use a banner for all your activities and it is using a SINGLETON that stores the information of "AdView" and "AdRequest" for all your application. Once AdMob and the Banners module has been initialized with your respective ID, you can use it anywhere in your program, just adding the AdView to the layout of each activity, without forgetting to remove it from the layout after finishing each activity.

  1. Add in your project a singleton class for your specific module (in this case Banners):

public class SingletonAdMobBanner

private static SingletonAdMobBanner instance;
private AdView adView;
private AdRequest adRequest;

public synchronized static SingletonAdMobBanner getInstance() {
    if (instance == null) instance = new SingletonAdMobBanner();
    return instance;
}

AdView getAdView() {return adView;}
AdRequest getAdRequest() {return adRequest;}
void setAdView(AdView adView) {this.adView = adView;}
void setAdRequest(AdRequest adRequest) {this.adRequest = adRequest;}
  1. Add the following to MainActivity:

    // Initialize AdMob
    try {
        MobileAds.getVersionString();
    } catch (Exception ignored) {
        MobileAds.initialize(getApplicationContext(), new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
    }
    
    // Init Banner module
    SingletonAdMobBanner sam = SingletonAdMobBanner.getInstance();
    sam.setAdView(new AdView(getApplicationContext()));
    AdView adView = sam.getAdView();
    adView.setAdUnitId("your module id");
    adView.setAdSize(AdSize.SMART_BANNER);
    sam.setAdRequest(new AdRequest.Builder().build());
    
  2. Create a public class or a method to load the banner within the specific layout that you designed to display the banner in each of the activities. This is an example:

    public static void adLoadBanner (Activity activity) {
        SingletonAdMobBanner sam = SingletonAdMobBanner.getInstance ();
        if (sam.getAdView ()! = null && sam.getAdRequest ()! = null) {
            LinearLayout container = activity.findViewById (R.id.ad_view_container);
            container.addView (sam.getAdView ());
            sam.getAdView (). loadAd (sam.getAdRequest ());
        }
    }
    

DONT FORGET TO REMOVE THE BANNER FROM LAYOUT, BEFORE LIVING YOUR ACTIVITY in onDestroy:

    // Remove Ad from this activity
    LinearLayout container = findViewById(R.id.ad_view_container);
    container.removeAllViews();
nnyerges
  • 563
  • 4
  • 15
0

You can put the code like this in your main activity, so that the ads banner will be displayed in all the three activities.

import com.google.ads.*; 

public class testActivity extends Activity { 
private static final String MY_AD_UNIT_ID = "yourId"; 
private AdView adView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    super.loadUrl("file:///android_asset/www/index.html");          
    // Create the adView 
    adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID); 
    LinearLayout layout = super.root; // this is the only change 
    layout.addView(adView); 
    adView.loadAd(new AdRequest());

Xml File:

<com.admob.android.ads.AdView
      android:id="@+id/ad"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      xmlns:backgroundColor="#000000"
      xmlns:primaryTextColor="#ffffff"
      xmlns:secondaryTextColor="#cccccc"
rgamber
  • 5,749
  • 10
  • 55
  • 99