Hi guys welcome to the Mr.Ethical Yt. Today we are going to learn how to implement the admob ads in the android application.
Implementation
Go to admob website and create an account . after creating the account add the app in which you wanted to show the redward ads then go to ad units and create ad unit.After creating the ad unit you will see screen like this
Now open your android project. Go to manifest.xml file the code given below under the application tag
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-2694150318750969~0000000023" />
Replace the value of line android:value with your your app id.Copy the below code and paste it in your activity_main.xml file
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF1D192A"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="120dp"
android:src="@drawable/watchad"
android:layout_margin="5dp"
android:layout_height="120dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textStyle="bold"
android:text="WATCH VIDEO"
android:textColor="#ffffff"
android:layout_margin="5dp"
android:textSize="25sp"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#ffffff"
android:gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Watch video to ge reward you will get 50 coins per each video and redeem them as money or use them to join match "
android:layout_margin="5dp"
android:textSize="15sp"
/>
<Button
android:layout_height="60dp"
android:textStyle="bold"
android:id="@+id/watchAd"
android:text="WATCH VIDEO"
android:textColor="#ffffff"
android:layout_marginTop="100dp"
android:background="@drawable/button_design"
android:layout_width="250dp"/>
</LinearLayout>
Then add this below code to you MainActivity.java file
Main activity.java
package com.grinding.telugu;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
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.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
RewardedVideoAd rewardedAd;
Button button;
boolean isLoading;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.watchandearn);
button=findViewById(R.id.watchAd);
ColorDrawable drawable=new ColorDrawable(Color.parseColor("#FF1D192A"));
getSupportActionBar().setBackgroundDrawable(drawable);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setElevation(0);
getSupportActionBar().setTitle("Watch Ads");
// MobileAds.in
//RequestConfiguration.Builder.setTestDeviceIds(Arrays.asList("33BE2250B43518CCDA7DE426D04EE231"));
MobileAds.initialize(getApplicationContext(),"ca-app-pub-2694150318750969/5429801369");
rewardedAd=MobileAds.getRewardedVideoAdInstance(this);
rewardedAd.setRewardedVideoAdListener(this);
rewardedAd.setRewardedVideoAdListener(this);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
if(rewardedAd.isLoaded()){
rewardedAd.show();
}
else{
Toast.makeText(getApplicationContext(),"Video not loaded",Toast.LENGTH_SHORT).show();
}
}
});
loadRewardedAd();
//MobileAds.initialize(this, new OnInitializationCompleteListener() {
// @Override
// public void onInitializationComplete(InitializationStatus initializationStatus) {
// }
// });
}
private void loadRewardedAd() {
if (rewardedAd == null || !rewardedAd.isLoaded()) {
isLoading = true;
rewardedAd.loadAd("ca-app-pub-2694150318750969/5429801369",
new AdRequest.Builder().build());
}
}
//implement RewardedVideoAdListners' callback methods like this
@Override
public void onRewarded(RewardItem reward) {
Toast.makeText(this, "onRewarded! currency: " + reward.getType() + " amount: " +
reward.getAmount(), Toast.LENGTH_SHORT).show();
// Reward the user.
}
@Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(this, "onRewardedVideoAdLeftApplication",
Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdClosed() {
Watchandearn.this.loadRewardedAd();
Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
Toast.makeText(this, "onRewardedVideoAdFailedToLoad"+errorCode, Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdLoaded() {
Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdOpened() {
Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoStarted() {
Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoCompleted() {
Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onOptionsItemSelected(MenuItem arg0) {
switch(arg0.getItemId()){
case android.R.id.home:
onBackPressed();
}
return super.onOptionsItemSelected(arg0);
}
}
That's it run the android app by building it .We have implemented reward ads in android app