4
\$\begingroup\$

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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Sep 25, 2012 at 15:36
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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){
 }
}
answered Oct 3, 2012 at 15:49
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.