Click here to Skip to main content
15,886,590 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have an app that shows an ad with a button click.
This works well, but now I want to show this ad on app launch.
I am using appodeal, and I am using some code, to check if the ad has finished loading and then show, but it is not working, it only works with button listener.
Can someone please help me fix this issue?
I also want the activity that shows the ad to finish after showing the ad.
Thanks.

What I have tried:

<pre>
@Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_rewarded_interstitial);
        progress = findViewById(R.id.progress);
...
//on oncreate
//This is what I have to check if the ad has finished loading, but this code 
//doesn't work and ad is only shown after button click, with a button listener.
if(progress.getProgress()==100){
            try {
                ad.show();
                Thread.sleep(3000);
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
                finish();
            }
        }
}
Posted
Updated 2-Aug-18 4:19am
Comments
Arthur V. Ratz 2-Aug-18 9:12am    
Does it actually mean that you want to render ad on start-up before the main app's activity layout is rendered ?? If so, here's my solution below.
Mike V Baker 2-Aug-18 9:45am    
Have you tried putting it in OnResume instead of OnCreate? Two reasons. OnCreate might be too early in the life cycle for what you're trying to do, and OnResume will also run it when they switch from one app to another and return. If you don't want it to pull up a new add in OnResume then perhaps OnCreateView would work.

1 solution

XML
<-- Advertisement dialog layout -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/adImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/ad1" />
</LinearLayout>



The code that you must implement in your main app's activity's OnCreate method prior to setContentView method invocation:


Java
package com.epsilon.adapp;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final AlertDialog.Builder m_myAdDlgBuilder =
                new AlertDialog.Builder(this);

        final View myAdView = this.getLayoutInflater().
                inflate(R.layout.dialog_ad, null);

        ImageView adImageView = myAdView.findViewById(R.id.adImageView);

        adImageView.setImageDrawable(getDrawable(this.getResources().getIdentifier("ad" +
                (new Random().nextInt(3) + 1), "drawable", getPackageName())));

        m_myAdDlgBuilder.setView(myAdView);

        m_myAdDlgBuilder.setPositiveButton("ok", null);

        AlertDialog myAdDialog = m_myAdDlgBuilder.create();

        myAdDialog.setCanceledOnTouchOutside(true);

        m_myAdDlgBuilder.show();

        setContentView(R.layout.activity_main);
    }
}


Here's the link to the ready-to-use Android project:
https://nofile.io/f/F0j207xZbn7/AdApp.zip
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900