Debian Codegraph @

templates/activities/GoogleAdMobAdsActivity/root/src/app_package/SimpleActivity.java.ftl

raw
1 package ${packageName};
2
3 <#if adFormat == "banner">
4 import com.google.android.gms.ads.AdRequest;
5 import com.google.android.gms.ads.AdView;
6 <#elseif adFormat == "interstitial">
7 import com.google.android.gms.ads.AdListener;
8 import com.google.android.gms.ads.AdRequest;
9 import com.google.android.gms.ads.InterstitialAd;
10 </#if>
11
12 import android.os.Bundle;
13 import ${superClassFqcn};
14 import android.view.Menu;
15 import android.view.MenuItem;
16 <#if adFormat == "interstitial">
17 import android.view.View;
18 import android.widget.Button;
19 import android.widget.TextView;
20 </#if>
21 import android.widget.Toast;
22 <#if applicationPackage??>
23 import ${applicationPackage}.R;
24 </#if>
25
26 public class ${activityClass} extends ${superClass} {
27 // Remove the below line after defining your own ad unit ID.
28 private static final String TOAST_TEXT = "Test ads are being shown. "
29 + "To show live ads, replace the ad unit ID in res/values/strings.xml with your own ad unit ID.";
30
31 <#if adFormat == "interstitial">
32 private static final int START_LEVEL = 1;
33 private int mLevel;
34 private Button mNextLevelButton;
35 private InterstitialAd mInterstitialAd;
36 private TextView mLevelTextView;
37 </#if>
38
39 @Override
40 protected void onCreate(Bundle savedInstanceState) {
41 super.onCreate(savedInstanceState);
42 setContentView(R.layout.${layoutName});
43
44 <#if adFormat == "banner">
45 // Load an ad into the AdMob banner view.
46 AdView adView = (AdView) findViewById(R.id.adView);
47 AdRequest adRequest = new AdRequest.Builder()
48 .setRequestAgent("android_studio:ad_template").build();
49 adView.loadAd(adRequest);
50 <#elseif adFormat == "interstitial">
51 // Create the next level button, which tries to show an interstitial when clicked.
52 mNextLevelButton = ((Button) findViewById(R.id.next_level_button));
53 mNextLevelButton.setEnabled(false);
54 mNextLevelButton.setOnClickListener(new View.OnClickListener() {
55 @Override
56 public void onClick(View view) {
57 showInterstitial();
58 }
59 });
60
61 // Create the text view to show the level number.
62 mLevelTextView = (TextView) findViewById(R.id.level);
63 mLevel = START_LEVEL;
64
65 // Create the InterstitialAd and set the adUnitId (defined in values/strings.xml).
66 mInterstitialAd = newInterstitialAd();
67 loadInterstitial();
68 </#if>
69
70 // Toasts the test ad message on the screen. Remove this after defining your own ad unit ID.
71 Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show();
72 }
73
74
75 @Override
76 public boolean onCreateOptionsMenu(Menu menu) {
77 // Inflate the menu; this adds items to the action bar if it is present.
78 getMenuInflater().inflate(R.menu.${menuName}, menu);
79 return true;
80 }
81
82 @Override
83 public boolean onOptionsItemSelected(MenuItem item) {
84 // Handle action bar item clicks here. The action bar will
85 // automatically handle clicks on the Home/Up button, so long
86 // as you specify a parent activity in AndroidManifest.xml.
87 int id = item.getItemId();
88
89 if (id == R.id.action_settings) {
90 return true;
91 }
92
93 return super.onOptionsItemSelected(item);
94 }
95
96 <#if adFormat == "interstitial">
97 private InterstitialAd newInterstitialAd() {
98 InterstitialAd interstitialAd = new InterstitialAd(this);
99 interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
100 interstitialAd.setAdListener(new AdListener() {
101 @Override
102 public void onAdLoaded() {
103 mNextLevelButton.setEnabled(true);
104 }
105
106 @Override
107 public void onAdFailedToLoad(int errorCode) {
108 mNextLevelButton.setEnabled(true);
109 }
110
111 @Override
112 public void onAdClosed() {
113 // Proceed to the next level.
114 goToNextLevel();
115 }
116 });
117 return interstitialAd;
118 }
119
120 private void showInterstitial() {
121 // Show the ad if it's ready. Otherwise toast and reload the ad.
122 if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
123 mInterstitialAd.show();
124 } else {
125 Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
126 goToNextLevel();
127 }
128 }
129
130 private void loadInterstitial() {
131 // Disable the next level button and load the ad.
132 mNextLevelButton.setEnabled(false);
133 AdRequest adRequest = new AdRequest.Builder()
134 .setRequestAgent("android_studio:ad_template").build();
135 mInterstitialAd.loadAd(adRequest);
136 }
137
138 private void goToNextLevel() {
139 // Show the next level and reload the ad to prepare for the level after.
140 mLevelTextView.setText("Level " + (++mLevel));
141 mInterstitialAd = newInterstitialAd();
142 loadInterstitial();
143 }
144 </#if>
145 }