I am trying to show an interstitial ad after a certain number of clicks but I am not able to do so, I know there are some answers already there but they don't much help me.
Here is a list of onClick methods I want to use to implement an interstitial ad after certain clicks
@SuppressLint("SetTextI18n")
private void back() {
if (position > 0) {
position = (position - 1) % quotes_list.size();
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
Log.d(TAG, quotes_list.toString());
}
}
@SuppressLint("SetTextI18n")
private void next() {
position = (position + 1) % quotes_list.size();
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
Log.d(TAG, quotes_list.toString());
}
@SuppressLint("SetTextI18n")
private void random() {
position = randomQ.nextInt(quotes_list.size());
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
Log.d(TAG, quotes_list.toString());
}
private void copy() {
if (mInterstitialAd != null) {
mInterstitialAd.show(HomeActivity.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
startActivity(new Intent(HomeActivity.this, HomeActivity.class));
mInterstitialAd = null;
intertitalAd();
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("text", quotesTxt.getText());
if (clipboardManager != null) {
clipboardManager.setPrimaryClip(clipData);
}
Toast.makeText(getApplicationContext(), "Copied", Toast.LENGTH_SHORT).show();
Log.d(TAG, quotes_list.toString());
}
});
} else {
startActivity(new Intent(HomeActivity.this, HomeActivity.class));
}
}
private void share() {
clickCount += 1;
if (clickCount < 4) {
startActivity(new Intent(HomeActivity.this, HomeActivity.class));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, quotesTxt.getText());
startActivity(Intent.createChooser(intent, "Share to"));
} else {
if (mInterstitialAd != null) {
mInterstitialAd.show(HomeActivity.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
startActivity(new Intent(HomeActivity.this, HomeActivity.class));
mInterstitialAd = null;
intertitalAd();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, quotesTxt.getText());
startActivity(Intent.createChooser(intent, "Share to"));
clickCount = 0;
}
});
} else {
startActivity(new Intent(HomeActivity.this, HomeActivity.class));
}
}
}
@SuppressLint("SetTextI18n")
private void latestQuote() {
position = quotes_list.size() - 1;
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
Log.d(TAG, quotes_list.toString());
}
@SuppressLint("SetTextI18n")
private void firstQuote() {
position = 0;
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
Log.d(TAG, quotes_list.toString());
}
There are five methods: latestQuote, firstQuote, back, next, and random. The confusion I have is with the onClick methods, since I'm getting a text, rather than getting a new activity/fragment whenever a button is clicked, so basically what I want is to show a text after the interstitial ad, but within the same activity., but, as I understand it, you have to get a new intent after an interstitial ad.
However, I also want to show interstitial ads on copy() and share() too. I tried to add a counter to the share() first, but that did not work for me.
Update :- as requested in the comment here is the full code
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
TextView countTxt, quotesTxt, noAds;
ImageView previousBtn, randomBtn, shareBtn, copyBtn, nextBtn;
List<String> quotes_list;
DatabaseReference databaseReference;
TextView noInternet;
RelativeLayout mainRelativeLayout;
Model model;
int position = 0;
Random randomQ = new Random();
int clickCount = 0;
private InterstitialAd mInterstitialAd;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
Window window = this.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(this, R.color.woodActionBar));
countTxt = findViewById(R.id.countText);
quotesTxt = findViewById(R.id.quotesTextView);
previousBtn = findViewById(R.id.backBtn);
randomBtn = findViewById(R.id.randomBtn);
shareBtn = findViewById(R.id.shareBtn);
copyBtn = findViewById(R.id.copyBtn);
nextBtn = findViewById(R.id.nextBtn);
noInternet = findViewById(R.id.noInternet);
mainRelativeLayout = findViewById(R.id.relativelayoutMain);
noAds = findViewById(R.id.noAds);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Objects.requireNonNull(this.getSupportActionBar()).setDisplayShowTitleEnabled(false);
previousBtn.setOnClickListener(this);
randomBtn.setOnClickListener(this);
shareBtn.setOnClickListener(this);
copyBtn.setOnClickListener(this);
nextBtn.setOnClickListener(this);
databaseReference = FirebaseDatabase.getInstance().getReference("Quotes");
model = new Model();
quotes_list = new ArrayList<>();
databaseReference.addValueEventListener(new ValueEventListener() {
@SuppressLint("SetTextI18n")
@Override
public void onDataChange(@NonNull @org.jetbrains.annotations.NotNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot1 : snapshot.getChildren()) {
model = dataSnapshot1.getValue(Model.class);
if (model != null) {
quotes_list.add(model.getTitle());
position = randomQ.nextInt(quotes_list.size());
Log.d(TAG, quotes_list.toString());
}
}
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
Log.d(TAG, quotes_list.toString());
}
@Override
public void onCancelled(@NonNull @org.jetbrains.annotations.NotNull DatabaseError error) {
Toast.makeText(HomeActivity.this, "Error", Toast.LENGTH_SHORT).show();
Log.d(TAG, error.getMessage());
}
});
// Ads
MobileAds.initialize(this, initializationStatus -> {
});
AdView mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
}
@Override
public void onAdFailedToLoad(@NotNull LoadAdError adError) {
super.onAdFailedToLoad(adError);
mAdView.loadAd(adRequest);
}
@Override
public void onAdOpened() {
super.onAdOpened();
}
@Override
public void onAdClicked() {
super.onAdClicked();
}
@Override
public void onAdClosed() {
super.onAdClosed();
}
});
intertitalAd();
// Internet on/off
if (isOnline()) {
noInternet.setVisibility(View.INVISIBLE); // Online
mainRelativeLayout.setVisibility(View.VISIBLE);
} else {
noInternet.setVisibility(View.VISIBLE); // Disconnected
mainRelativeLayout.setVisibility(View.INVISIBLE);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menue, menu);
return super.onCreateOptionsMenu(menu);
}
@SuppressLint("NonConstantResourceId")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.latestQuote:
latestQuote();
return true;
case R.id.firstQuote:
firstQuote();
return true;
case R.id.guide:
guide();
default:
return super.onOptionsItemSelected(item);
}
}
@SuppressLint("SetTextI18n")
private void latestQuote() {
position = quotes_list.size() - 1;
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
Log.d(TAG, quotes_list.toString());
}
@SuppressLint("SetTextI18n")
private void firstQuote() {
position = 0;
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
Log.d(TAG, quotes_list.toString());
}
private void guide() {
Intent intent = new Intent(HomeActivity.this, Guide.class);
startActivity(intent);
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.backBtn:
back();
break;
case R.id.randomBtn:
random();
break;
case R.id.copyBtn:
copy();
break;
case R.id.shareBtn:
share();
break;
case R.id.nextBtn:
next();
break;
}
}
@SuppressLint("SetTextI18n")
private void back() {
if (position > 0) {
position = (position - 1) % quotes_list.size();
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
Log.d(TAG, quotes_list.toString());
}
}
@SuppressLint("SetTextI18n")
private void next() {
position = (position + 1) % quotes_list.size();
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
Log.d(TAG, quotes_list.toString());
}
@SuppressLint("SetTextI18n")
private void random() {
position = randomQ.nextInt(quotes_list.size());
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
Log.d(TAG, quotes_list.toString());
}
private void copy() {
if (mInterstitialAd != null) {
mInterstitialAd.show(HomeActivity.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
startActivity(new Intent(HomeActivity.this, HomeActivity.class));
mInterstitialAd = null;
intertitalAd();
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("text", quotesTxt.getText());
if (clipboardManager != null) {
clipboardManager.setPrimaryClip(clipData);
}
Toast.makeText(getApplicationContext(), "Copied", Toast.LENGTH_SHORT).show();
Log.d(TAG, quotes_list.toString());
}
});
} else {
startActivity(new Intent(HomeActivity.this, HomeActivity.class));
}
}
private void share() {
clickCount += 1;
if (clickCount < 4) {
startActivity(new Intent(HomeActivity.this, HomeActivity.class));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, quotesTxt.getText());
startActivity(Intent.createChooser(intent, "Share to"));
} else {
if (mInterstitialAd != null) {
mInterstitialAd.show(HomeActivity.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
startActivity(new Intent(HomeActivity.this, HomeActivity.class));
mInterstitialAd = null;
intertitalAd();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, quotesTxt.getText());
startActivity(Intent.createChooser(intent, "Share to"));
clickCount = 0;
}
});
} else {
startActivity(new Intent(HomeActivity.this, HomeActivity.class));
}
}
}
// Internet on/off
public boolean isOnline() {
boolean connected = false;
try {
ConnectivityManager connectivityManager = (ConnectivityManager) this.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
connected = networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
return connected;
} catch (Exception e) {
e.printStackTrace();
}
return connected;
}
public void intertitalAd() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this, "ca-app-pub-3940256099942544/8691691433", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
mInterstitialAd = null;
}
});
}
}