📜 ⬆️ ⬇️

We give the user of the application game currency using AdMob Rewarded Video

Hello! In this article I would like to share my experience with you, namely, to show how the process of presenting game currency (for example, coins) to a user with the help of AdMob Rewarded Video works.

image

Foreword


About 3 months ago I began developing my own project. I plan to release Android applications on the topic of quizzes. Most recently (about 2 weeks ago) I published my quiz application with a geographical bias on Google Play. Yes, I understand perfectly well that there are hundreds of such quizzes, and even thousands, and they all mostly repeat the functionality of each other.

Many such projects, if they are from an indi-developer (as it seemed to me) have not properly configured monetization stages. Let's be frank if you are indie, then your main goal is to earn money on advertising shows, to sell additional content and other things. With the right approach to content and advertising in most cases, the application will find its audience. It can be said that monetization is the key to the success of any project / application.
')
For myself, I identified several types of monetization: advertisements (banner, interstitial, video) and sharing in social networks (Facebook, Twitter, VK).
I believe that the user of your application should receive a reward for all these points in order to feel some satisfaction.

How to connect a banner or interstitial advertising, most of you know and read various tutorials, but most likely have not yet encountered the connection of video advertising, and there is not so much information on the subject in the network.
That made me write this post.

Beginning of work


So, I will show the implementation of AdMob Rewarded Video Add on a test project. From it it will be completely clear how it can be added to your existing project or just to work with it in the future. Also, we will indirectly touch on the topic SharedPreferences for storing coins.

Step 1:
We need to make a layout in which we will have a TextView (showing the number of coins), the Video button (clicking on it starts viewing the advertisement) and the Game button (when clicked, we will add one coin to the user):

image

Step 2:
Let's start implementation.
In this step, we need to: add dependencies in the gradle, initialize AdMob ads and create several constants.

Add dependencies:

compile 'com.google.firebase:firebase-core:10.2.0' compile 'com.google.firebase:firebase-ads:10.2.0' 

Initialize Admob:

 public class App extends Application { @Override public void onCreate() { super.onCreate(); MobileAds.initialize(this, Constants.ADMOB_ID); } } 

Create a class Constants.java:

 class Constants { private Constants() { throw new AssertionError(); } static final String PREF_COINS = "pref_coins"; static final String ADMOB_ID = "YOUR_ADMOB_ACCOUNT_ID_HERE"; static final String AD_MOB_REWARDED_VIDEO_ID = "YOUR_ADMOB_ADD_ID"; static final int REWARD_FOR_VIDEO = 20; } 

In the 'static final int REWARD_FOR_VIDEO' constant, we indicate the number of coins we give the user for viewing video ads.

Step 3:
In this step, we will describe the actions that should occur, namely: create the necessary fields and deactivate the 'Video' button in the onCreate () method, write down the values ​​of our coins in the SharedPreference, write the loadRewardVideo () method, which will load the video advertisement.

 public class MainActivity extends AppCompatActivity { private AdRequest mAdRequest; private RewardedVideoAd mRewardedVideoAd; private SharedPreferences mSharedPreferences; private TextView mTextCoins; private Button mButtonVideo; private int coins; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); coins = mSharedPreferences.getInt(PREF_COINS, 0); setContentView(R.layout.activity_main); mButtonVideo = (Button) findViewById(R.id.btn_video); mButtonVideo.setOnClickListener(clickListener); mButtonVideo.setEnabled(false); findViewById(R.id.btn_game).setOnClickListener(clickListener); mTextCoins = (TextView) findViewById(R.id.tv_coins); mTextCoins.setText(getResources().getQuantityString(R.plurals.coins, coins, coins)); //AdMob Rewarded Video mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this); mRewardedVideoAd.setRewardedVideoAdListener(rewardedVideoAdListener); mAdRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build(); loadRewardVideo(); View.OnClickListener clickListener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_video: if (mRewardedVideoAd.isLoaded()) { mRewardedVideoAd.show(); } break; case R.id.btn_game: coins++; mTextCoins.setText(getResources().getQuantityString(R.plurals.coins, coins, coins)); break; } } }; private void loadRewardVideo() { mRewardedVideoAd.loadAd(AD_MOB_REWARDED_VIDEO_ID, mAdRequest); } } 

Step 4:
Create a RewardedVideoAdListener listener. In the onRewardedVideoAdLoaded () method, we activate our button, which we previously turned off. Also, we conduct all our main actions in the onRewarded method, in which we add our coins for viewing the video to the existing ones and upon completion of the advertisement we show the user toast with the text that he received an award of 20 coins.

  private RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() { @Override public void onRewardedVideoAdLoaded() { mButtonVideo.setEnabled(true); } @Override public void onRewardedVideoAdOpened() { } @Override public void onRewardedVideoStarted() { } @Override public void onRewardedVideoAdClosed() { mButtonVideo.setEnabled(false); loadRewardVideo(); } @Override public void onRewarded(RewardItem rewardItem) { coins += REWARD_FOR_VIDEO; mTextCoins.setText(getResources().getQuantityString(R.plurals.coins, coins, coins)); String msg = getResources().getQuantityString(R.plurals.congrats, REWARD_FOR_VIDEO, REWARD_FOR_VIDEO); Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); mSharedPreferences.edit().putInt(PREF_COINS, coins).apply(); } @Override public void onRewardedVideoAdLeftApplication() { } @Override public void onRewardedVideoAdFailedToLoad(int i) { } }; 

In this step, we did the basic actions that add logic by clicking on the button, and also described the behavior of video advertising in the RewardedVideoAdListener.

And finally, let's add life cycles to our video ads:

 @Override protected void onPause() { super.onPause(); mSharedPreferences.edit().putInt(PREF_COINS, coins).apply(); if (mRewardedVideoAd != null) { mRewardedVideoAd.pause(this); } } @Override protected void onResume() { super.onResume(); if (mRewardedVideoAd !=null) { mRewardedVideoAd.resume(this); } } @Override protected void onDestroy() { super.onDestroy(); if (mRewardedVideoAd != null) { mRewardedVideoAd.destroy(this); } } 

And in general, that's all. The result can be seen in this video:



PS: Let me remind you once again that in this example I showed how to do this in a test project. In its application it is implemented more brightly and colorfully using custom buttons. I will leave a link to my application in the comment to the post, so as not to be blocked for advertising.

PPS: Also, it seems to me that in indie development of Android applications in the field of quizzes, you need to have an idea about working with custom buttons.

The right monetization system and the ability to create a button of any shape and type is our everything. I would like to highlight my next article on this particular topic and show how to work correctly with buttons on devices with pre lollipop and higher.

Source: https://habr.com/ru/post/335008/


All Articles