user8304564
user8304564

Reputation:

Button is not getting enabled?

When the rewarded ad has done loading, mButton is supposed to get enabled. But, the button gets enabled only when the current activity is MainActivity.

For example, if the video has loaded when the current activity is AnotherActivity, the button does not get enabled!

Code in MainActivity java class:

@Override
public void onRewardedVideoAdLoaded() {
    mButton.setEnabled(true); //works only in the MainActivity activity
    Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.me.myapplication.MainActivity">

    <Button
        android:id="@+id/mButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</android.support.constraint.ConstraintLayout>

How can I enable mButton when the video has done loading while I'm in AnotherActivity?

Upvotes: 1

Views: 69

Answers (3)

Pdroid
Pdroid

Reputation: 76

That's because you can not update UI when the respective Activity is not visible. You can maintain a flag when onRewardedVideoAdLoaded() is getting executed, and then check the flag to enable/disable the button when MainActivity is brought foreground.

Upvotes: 1

Pdroid
Pdroid

Reputation: 76

You will need to add the button enable code in both the places - in onResume() and in onRewardedVideoAdLoaded().

private long mTimeRemaining;
private Button mTestButton;
private static boolean sEnable;

@Override
public void onResume() {
    super.onResume();

    mTestButton.setEnabled(sEnable);
    ....
}

 ...

@Override
public void onRewardedVideoAdLoaded() {
    Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
    sEnable = true;
    mTestButton.setEnabled(sEnable);

}

Upvotes: 0

Yupi
Yupi

Reputation: 4470

If your ad is loaded in some another Activity rather then in MainActivity then save your state for example use sharedpreferences. You could use values like: 1 if ad is loaded and 0 if not and do something like this:

If video is loaded:

 PreferenceManager.getDefaultSharedPreferences(this).edit().
                                putInt("value", 1).apply(); // word value is key

Then override onResume and onStart in your MainActivity and inside check is video loaded by getting the value from SharedPreferences like this:

 int i = PreferenceManager.getDefaultSharedPreferences(this).
                            getInt("value", 0).apply(); //0 is default value

 if(i == 1){
   mButton.setEnabled(true); 
 }
 else
  mButton.setEnabled(false); 

I hope this will help. But note that sharedpreferences will always remember the state and button will be enabled next time app started until user delete data or reinstall app. I don't know is that what you want but if course it's possible to delete from sharedpreferences every time user exits app.

Upvotes: 0

Related Questions