I have three activities in total and i am implementing admob for each activity, every activity has its own banner and when the activity is changes the other activity hangs a little because of the ad loading in the background, is there any way that one banner appears in all the activities when switched to avoid delays.
you can do it just load ad in application class and use it in any activity.
you can download demo
as I do it,
App class
import android.app.Application;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class App extends Application {
AdView adView;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ca-app-pub-1267746788642565/8418489933");
// Request for Ads
AdRequest adRequest = new AdRequest.Builder().build();
// Load ads into Banner Ads
adView.loadAd(adRequest);
}
public void loadAd(LinearLayout layAd) {
// Locate the Banner Ad in activity xml
if (adView.getParent() != null) {
ViewGroup tempVg = (ViewGroup) adView.getParent();
tempVg.removeView(adView);
}
layAd.addView(adView);
}
}
main Activity
public class MainActivity extends Activity {
App app;
LinearLayout layAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layAd = (LinearLayout) findViewById(R.id.layad);
app = (App) getApplication();
app.loadAd(layAd);
Button btnNext = (Button) findViewById(R.id.next);
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent iNext = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(iNext);
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
app.loadAd(layAd);
super.onResume();
}
}
Second Activity
public class SecondActivity extends Activity {
App app;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
LinearLayout layAd = (LinearLayout) findViewById(R.id.layad);
app = (App) getApplication();
app.loadAd(layAd);
}
}
Manifest xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admobdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:name="com.example.admobdemo.App"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.admobdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.admobdemo.SecondActivity"
android:label="@string/app_name" >
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>
main activity layout xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:id="@+id/layad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="@+id/next"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
and second activity layout xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:id="@+id/layad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
The ideal way is to use fragments for each of your screens
. This way, you'd use a single activity having a single adview.
If you want to use multiple activities instead, then the only workaround I know of is to use a static method to load the ads:
public class MyAdView {
public static void SetAD(AdView adView){
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);
}
}
Usage:
public class SomeActivity extends Activity {
private AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.caller_main);
MyAdView.SetAd((AdView)findViewById(R.id.adView));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With