Reputation: 38
I was trying to show the interstitial ads after 5 button clicks, below is my code which is not working, kindly someone show me how to solve this. I have tried 2 ways, both are not working.
PS: I'm new to android development.
First Code
int counter = 0;
...
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Button play_video = (Button) rootView.findViewById(R.id.play_video);
play_video.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter == 5) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
counter = 0;
} else {
counter++;
stream(vidData);
getDialog().dismiss();
}
if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
}
});
Second Code (Shared Preferance)
private int counter = 0;
.....
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Button play_video = (Button) rootView.findViewById(R.id.play_video);
play_video.setOnClickListener(new View.OnClickListener() {
Context context = getActivity();
SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
int counter = preferences.getInt("CounterR", 0);
@Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded() & counter >= 5) {
counter = 0;
mInterstitialAd.show();
} else {
counter++;
stream(vidData);
getDialog().dismiss();
}
editor.putInt("CounteR", counter);
editor.commit();
if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
});
Updated Full Code
private int counter = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.vid_alert, container, false);
close_dialog = (Button) findViewById(R.id.close_dialog);
mInterstitialAd = new InterstitialAd(getActivity());
mInterstitialAd.setAdUnitId("xxxxxxxxxxxxxxxxxx");
AdRequest adRequest = new AdRequest.Builder().build();
// Load ads into Interstitial Ads
mInterstitialAd.loadAd(adRequest);
// Create and set AdListener for interstitial
mInterstitialAd.setAdListener(new AdListener() {
public void onAdClosed() {
// When user closes ad end this activity (go back to first
// activity)
finish();
}
private void finish() {
// TODO Auto-generated method stub
}
});
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
Bundle mArgs = getArguments();
final String vidData = mArgs.getString("vid_data");
final String vidID = mArgs.getString("vid_id");
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Context context = getActivity();
SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
Button stream_video = (Button) rootView.findViewById(R.id.stream_video);
stream_video.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter == 5) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
counter = 0;
} else {
// means interstitial not yet loaded,so make a load
// request
AdRequest newadRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(newadRequest);
// as nothing will happen when we get inside this else
// statement, you should call the stream method from
// here too
streamFB(vidData);
getDialog().dismiss();
}
} else {
counter++;
streamFB(vidData);
getDialog().dismiss();
}
}
});
Button close_dialog = (Button) rootView.findViewById(R.id.close_dialog);
close_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter == 5) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
counter = 0;
} else {
// means interstitial not yet loaded,so make a load
// request
AdRequest newadRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(newadRequest);
// as nothing will happen when we get inside this else
// statement, you should call the stream method from
// here too
getDialog().dismiss();
}
} else {
counter++;
getDialog().dismiss();
}
}
});
// Do something else
return rootView;
Upvotes: 1
Views: 4288
Reputation: 11
Remove counter = 0 after mInterstitialAd.show(); from the if condition and place it in the onAdClosed() function see example below:
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
counter=0;
startNewGame();
}
});
Upvotes: 1
Reputation: 1725
Try this, if it is not working than most probably there is problem in your ads code.
int counter = 0;
...
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Button play_video = (Button) rootView.findViewById(R.id.play_video);
play_video.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter != 5) {
counter++;
stream(vidData);
getDialog().dismiss();
}
else if (counter ==5){
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
counter = 0;
}
}
if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
}
});
}
Upvotes: 0
Reputation: 2636
Problem is with your logic, the else part should come with the outer if, check my code below:
if (counter == 5) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
counter = 0;
}else{
// means interstitial not yet loaded,so make a load request
AdRequest newadRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(newadRequest);
// as nothing will happen when we get inside this else statement, you should call the stream method from here too
stream(vidData);
getDialog().dismiss();
}
}else {
counter++;
stream(vidData);
getDialog().dismiss();
}
move this code in your onclick method. let me know if it worked, edited directly here on stackoverflow, please check the syntax errors too, there might be one or two.
Upvotes: 3