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?

\$\endgroup\$
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){

    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.