I'm trying to implement the award-winning AdMob video in my app that consists of a game. When the player loses, a Dialog Box that represents a Game Over screen is displayed for him, containing two buttons: one that restarts the game and another that gives him a chance to continue playing from where he left off when watching the winning video. To do this, I created the mRewardedVideo object, as well as its Listener, in the Game Activity and created the showLoseDialog function in a class called Game. When the player finished watching the video, I wanted to hide the button that restarts the game and change the text of the button indicating that it can already receive its reward, which in case it would continue to play from where it stopped. The problem is that the onRewardedVideoCompleted method of Listener, called when the user finishes watching the video, is in the Activity, while the buttons are in the function of the Game class, that is, I have no idea how to access the buttons from the Listener method. Could anyone tell me if this is possible?
MainActivity.java:
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mRewardedVideoAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
}
@Override
public void onResume() {
mRewardedVideoAd.resume(this);
super.onResume();
}
@Override
public void onPause() {
mRewardedVideoAd.pause(this);
super.onPause();
}
@Override
public void onDestroy() {
mRewardedVideoAd.destroy(this);
super.onDestroy();
}
@Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
@Override
public void onRewarded(RewardItem rewardItem) {
Toast.makeText(this, "onRewarded! currency: " + rewardItem.getType() + " amount: " +
rewardItem.getAmount(), Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
@Override
public void onRewardedVideoCompleted() {
//btnPlayAgain.setVisibility(View.GONE);
//btnRewardedVideo.setText(R.string.receiveReward);
}
public void lose(){
Game.showLoseDialog(MainActivity.this, getLayoutInflater().inflate(R.layout.dialog_lose, null), mRewardedVideoAd);
}
private void loadRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
new AdRequest.Builder().build());
}
Game.java:
public class Game {
public static void showLoseDialog(Context context, View dialogView, final RewardedVideoAd mRewardedVideoAd){
Button btnPlayAgain = dialogView.findViewById(R.id.btnPlayAgain);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
mBuilder.setView(dialogView);
final AlertDialog dialog = mBuilder.create();
final Button btnRewardedVideo = dialogView.findViewById(R.id.btnRewardedVideo);
btnRewardedVideo.setVisibility(View.VISIBLE);
if(mRewardedVideoAd.isLoaded()){
btnRewardedVideo.setVisibility(View.VISIBLE);
}else{
btnRewardedVideo.setVisibility(View.GONE);
}
btnPlayAgain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
btnRewardedVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}
});
dialog.show();
}