javaandroidgoogle-advertising-id

AdView Not Showing on first load


Ive been working on learning how to make games and Id like to understand how to add Adverts. The advert shows? But only after I close and reopen, "SMART_BANNER" doesnt work either. What am I doing wrong?

public class MainActivity extends FragmentActivity {

    public GoogleApiClient apiClient;
    private MainActivity main = this;
    public GameSurface gameSurface;
    RelativeLayout layout;
    RelativeLayout adlayout;
    private Saver saver;

    private static final String HIGHSCORE = "highscore";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        apiClient = new GoogleApiClient.Builder(this)
                .addApi(Games.API)
                .addScope(Games.SCOPE_GAMES)
                .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Toast.makeText(getBaseContext(), "Connection To Google Games Failed, No App Found Or No Internet", Toast.LENGTH_SHORT).show();
                    }
                }).build();

        MobileAds.initialize(this, getString(R.string.adappid));

        apiClient.connect();
        saver = Saver.getInstance(this);

        playerscores();

        // fullscreen
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        layout = new RelativeLayout(this);
        layout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

        AdView adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
        adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
        adView.loadAd(adRequestBuilder.build());

        //ads
        gameSurface = new GameSurface(this, main);
        layout.addView(gameSurface);

        layout.addView(adView);

        setContentView(layout);

    }
            // Set No Title
            //this.setContentView(new GameSurface(this));

    public void playerscores() {
        if (apiClient != null && apiClient.isConnected()) {
            Games.Leaderboards.loadCurrentPlayerLeaderboardScore(apiClient, "CgkI08DA0-sZEAIQAQ", LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(
                    new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {

                        @Override
                        public void onResult(Leaderboards.LoadPlayerScoreResult arg0) {
                            LeaderboardScore c = arg0.getScore();
                            String score = c.getDisplayScore();
                            saver.saveString(HIGHSCORE, score);
                        }
                    });
        }
    }

    public void gameover() {
        if (apiClient != null && apiClient.isConnected()) {
            Games.Leaderboards.submitScore(apiClient, getString(R.string.leaderboard_highscores), GameSurface.HighScore);
        }
    }

    public void showLeaderboard() {
        if (apiClient != null && apiClient.isConnected()) {
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(apiClient, "CgkI08DA0-sZEAIQAQ"), 1);
        } else {
            apiClient.connect();
        }
    }
}

Basically I want the advert to show as soon as the app opens. Id Also like it to be fixed to the bottom, I cant find a way to do this, I've tried adding gravity but adview doesn't have this attribute.

Any advice as to what Im doing wrong will be greatly appreciated.


Solution

  • Part Answer I worked out how to get the advert align to the bottom and Center it.

    RelativeLayout.LayoutParams viewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);viewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);viewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    
            adView.setLayoutParams(viewParams);
    

    EDIT [SOLVED]

    added an adview listener, reading through admob docs and seeing various listeners gave me an idea! then just made it redraw its self.

     adView.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    adView.setVisibility(View.GONE);
                    adView.setVisibility(View.VISIBLE);
                }
            });