Learn
& Implement

Get Started




Happy New Year

Implement Admob Reward Ads

In this tutorial, I will tell you how to implement admob reward ads.

build.gradle(module:app)


                        implementation 'com.google.android.gms:play-services-ads:19.6.0'
                        

Now add Internet permission in file
AndroidManifest.XML

                            <uses-permission android:name="android.permission.INTERNET"/>

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.TestApp">
                <activity android:name=".MainActivity">
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />

                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>

                <meta-data
                    android:name="com.google.android.gms.ads.APPLICATION_ID"
                    android:value="ca-app-pub-3940256099942544~3347511713"/>

        </application>

                        

MainActivity.java


                        import android.os.Bundle;
                        import android.view.View;
                        import android.widget.Button;
                        import android.widget.TextView;
                        import android.widget.Toast;
                        
                        import androidx.annotation.NonNull;
                        import androidx.appcompat.app.AppCompatActivity;
                        
                        import com.google.android.gms.ads.AdError;
                        import com.google.android.gms.ads.AdRequest;
                        import com.google.android.gms.ads.LoadAdError;
                        import com.google.android.gms.ads.MobileAds;
                        import com.google.android.gms.ads.rewarded.RewardItem;
                        import com.google.android.gms.ads.rewarded.RewardedAd;
                        import com.google.android.gms.ads.rewarded.RewardedAdCallback;
                        import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;
                        
                        public class MainActivity extends AppCompatActivity {
                        
                            Button button;
                        
                            RewardedAd rewardedAd;
                        
                            @Override
                            protected void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_main);
                        
                                MobileAds.initialize(this);
                        
                                button = findViewById(R.id.button);
                        
                                rewardedAd = new RewardedAd(this, "ca-app-pub-3940256099942544/5224354917");
                        
                                rewardedAd.loadAd(new AdRequest.Builder().build(), new RewardedAdLoadCallback(){
                        
                                    @Override
                                    public void onRewardedAdLoaded() {
                                        super.onRewardedAdLoaded();
                                        button.setVisibility(View.VISIBLE);
                                    }
                        
                                    @Override
                                    public void onRewardedAdFailedToLoad(LoadAdError loadAdError) {
                                        super.onRewardedAdFailedToLoad(loadAdError);
                                        Toast.makeText(MainActivity.this, "Error: "+loadAdError.getMessage(),
                                                Toast.LENGTH_SHORT).show();
                                    }
                                });
                        
                                button.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                        
                                        if (rewardedAd.isLoaded()){
                        
                                            RewardedAdCallback callback = new RewardedAdCallback() {
                                                @Override
                                                public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                                                    ((TextView) findViewById(R.id.TV))
                                                            .setText(String.valueOf(Integer
                                                                    .parseInt(((TextView) findViewById(R.id.TV)).getText().toString()) + 5));
                                                }
                        
                                                @Override
                                                public void onRewardedAdClosed() {
                                                    super.onRewardedAdClosed();
                                                    Toast.makeText(MainActivity.this, "Ad Closed", Toast.LENGTH_SHORT).show();
                                                }
                        
                                                @Override
                                                public void onRewardedAdFailedToShow(AdError adError) {
                                                    super.onRewardedAdFailedToShow(adError);
                                                    Toast.makeText(MainActivity.this, "Error: "+adError.getMessage(), Toast.LENGTH_SHORT).show();
                                                }
                        
                                                @Override
                                                public void onRewardedAdOpened() {
                                                    super.onRewardedAdOpened();
                                                    Toast.makeText(MainActivity.this, "Ad Opened", Toast.LENGTH_SHORT).show();
                                                }
                        
                                            };
                        
                                            rewardedAd.show(MainActivity.this, callback);
                        
                                        }
                        
                                    }
                                });
                        
                        
                            }
                        
                        }

                            

activity_main.xml


                        <xml version="1.0" encoding="utf-8"?>
                        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                            xmlns:tools="http://schemas.android.com/tools"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@android:color/white"
                            tools:context=".MainActivity">
                        
                            <TextView
                                android:id="@+id/TV"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentTop="true"
                                android:layout_alignParentEnd="true"
                                android:layout_margin="22dp"
                                android:background="#CEECFA"
                                android:minWidth="60dp"
                                android:text="0"
                                android:textAlignment="center"
                                android:textColor="#10194C"
                                android:textSize="25sp"
                                android:textStyle="bold" />
                        
                            <TextView
                                android:id="@+id/TV1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerInParent="true"
                                android:text="Watch ad to collect 5 points"
                                android:textAlignment="center"
                                android:textColor="#10194C"
                                android:textSize="25sp"
                                android:textStyle="bold" />
                        
                        
                            <Button
                                android:id="@+id/button"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@id/TV1"
                                android:layout_centerHorizontal="true"
                                android:layout_margin="32dp"
                                android:text="Show Ad"
                                android:textColor="@android:color/white"
                                android:textSize="18sp"
                                android:textStyle="bold"
                                android:visibility="invisible" />
                        
                        </RelativeLayout>

Hi there!

Would you like to support me.