I'm new to Android and I wanted to create a class that would load an ad inside the current layout. All the layouts have a RelativeLayout id=adLayout
From my main, I have:
AdLoader al = new AdLoader();
al.GrabAd(this,"123");
Then on my AdLoader.java class I have:
public class AdLoader {
private DfpAdView adView;
public void GrabAdFor(Activity act, String adId) {
adView = new DfpAdView(act, AdSize.BANNER, adId);
RelativeLayout rl = (RelativeLayout) act.findViewById(R.id.adLayout);
rl.addView(adView);
adView.loadAd(new AdRequest());
}
}
Is it incorrect to pass the Activity? Am I even passing the activity or the View/Context?
1 Answer 1
You could also make a baseactivity for all the other activity that are going to use the adView
public class BaseActivity extends Activity {
private DfpAdView adView;
RelativeLayout rl;
//some other stuff
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baseactivity_layout)
rl = (RelativeLayout) findViewById(R.id.adLayout);
adView = new DfpAdView(this, AdSize.BANNER, adId);
rl.addView(adView);
adView.loadAd(new AdRequest());
}
}
And and make your MainActivity (or all the other activities that are using the adview)
public class MainActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState){
}
}