Reputation: 777
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.
Upvotes: 4
Views: 8878
Reputation: 583
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.
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;}
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());
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();
Upvotes: 1
Reputation: 2179
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!
Upvotes: 1
Reputation: 128428
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.
Upvotes: 2
Reputation:
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"
Upvotes: 0