admob rewarded video ads android example
XML (activity_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="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=".after32.Rewardvideoads">
<TextView
android:id="@+id/text"
android:gravity="center"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/watch"
android:text="show"
android:layout_marginTop="50dp"
android:layout_below="@+id/text"
/>
</RelativeLayout>
JAVA (MainActivity.java)
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;
import com.tutorial.personal.androidstudiopro.R;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Rewardvideoads extends AppCompatActivity implements RewardedVideoAdListener {
private Button mWatch;
private TextView mText;
private RewardedVideoAd mAd;
int coin=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rewardvideoads);
mWatch=(Button)findViewById(R.id.watch);
mText=(TextView) findViewById(R.id.text);
mWatch.setVisibility(View.INVISIBLE);
mAd= MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(this);
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if (!mAd.isLoaded()) {
loadAd();
} else {
}
}
});
}
}, 3, 5, TimeUnit.SECONDS);
mWatch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mAd.isLoaded()){
mAd.show();
}
}
});
}
public void loadAd(){
if(!mAd.isLoaded()){
mAd.loadAd("ca-app-pub-394454099942544/55445917",new AdRequest.Builder().build());
}
}
@Override
public void onRewardedVideoAdLoaded() {
mWatch.setVisibility(View.VISIBLE);
Toast.makeText(this, "Click this button for earn coin", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdOpened() {
Toast.makeText(this, "Rewarded Video Ad has opened", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoStarted() {
Toast.makeText(this, "Watch full video for earn Coin", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdClosed() {
Toast.makeText(this, "Rewarded Video Ad has closed", Toast.LENGTH_SHORT).show();
loadAd();
mWatch.setVisibility(View.INVISIBLE);
}
@Override
public void onRewarded(RewardItem rewardItem) {
coin=coin+rewardItem.getAmount();
mText.setText("Your coin : "+coin);
}
@Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(this, "Rewarded Video Ad has closed", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
@Override
public void onRewardedVideoCompleted() {
}
@Override
public void onPause() {
super.onPause();
mAd.pause(this);
}
@Override
public void onResume() {
super.onResume();
mAd.resume(this);
loadAd();
}
@Override
public void onDestroy() {
super.onDestroy();
mAd.destroy(this);
}
@Override
protected void onStart() {
loadAd();
super.onStart();
}
}