Last active
May 7, 2018 02:53
-
-
Save PattyAppier/0d31c921c9f0b38ad688267f73ea7fd4 to your computer and use it in GitHub Desktop.
PattysCoolAdApp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.pattyshomemac.pattycooladapp; | |
import com.google.android.gms.ads.AdListener; | |
import com.google.android.gms.ads.AdRequest; | |
import com.google.android.gms.ads.InterstitialAd; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class PattysCoolAdApp extends AppCompatActivity { | |
private static final int START_LEVEL = 1; | |
private int mLevel; | |
private Button mNextLevelButton; | |
private InterstitialAd mInterstitialAd; | |
private TextView mLevelTextView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_patty_cool_ad_app); | |
// Create the next level button, which tries to show an interstitial when clicked. | |
mNextLevelButton = ((Button) findViewById(R.id.next_level_button)); | |
mNextLevelButton.setEnabled(false); | |
mNextLevelButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
showInterstitial(); | |
} | |
}); | |
// Create the text view to show the cool.counts number. | |
mLevelTextView = (TextView) findViewById(R.id.level); | |
mLevel = START_LEVEL; | |
// Create the InterstitialAd and set the adUnitId (defined in values/strings.xml). | |
mInterstitialAd = newInterstitialAd(); | |
loadInterstitial(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_patty_cool_ad_app, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
private InterstitialAd newInterstitialAd() { | |
InterstitialAd interstitialAd = new InterstitialAd(this); | |
interstitialAd.setAdUnitId("PattyID"); | |
interstitialAd.setAdListener(new AdListener() { | |
@Override | |
public void onAdLoaded() { | |
mNextLevelButton.setEnabled(true); | |
} | |
@Override | |
public void onAdFailedToLoad(int errorCode) { | |
mNextLevelButton.setEnabled(true); | |
} | |
@Override | |
public void onAdClosed() { | |
// Proceed to the next level. | |
goToNextLevel(); | |
} | |
}); | |
return interstitialAd; | |
} | |
private void showInterstitial() { //allert | |
// Show the ad if it's ready. Otherwise toast and reload the ad. | |
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) { | |
mInterstitialAd.show(); | |
} else { | |
Toast.makeText(this, "Patty think Google Android is cool ~", Toast.LENGTH_SHORT).show(); | |
goToNextLevel(); | |
} | |
} | |
private void loadInterstitial() { | |
// Disable the next cool button and load the ad. | |
mNextLevelButton.setEnabled(false); | |
AdRequest adRequest = new AdRequest.Builder() | |
.setRequestAgent("android_studio:ad_template").build(); | |
mInterstitialAd.loadAd(adRequest); | |
} | |
private void goToNextLevel() { | |
// Show the next cool and reload the ad to prepare for the cool+ after. | |
mLevelTextView.setText("Cool " + (++mLevel)); | |
mInterstitialAd = newInterstitialAd(); | |
loadInterstitial(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
tools:context=".PattysCoolAdApp"> //app title | |
<!-- view for AdMob Interstitial Ad --> | |
<TextView | |
android:id="@+id/app_title" //app theme Title | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_marginTop="50dp" | |
android:text="@string/interstitial_ad_sample" // ad sample | |
android:textAppearance="?android:attr/textAppearanceLarge" /> | |
<TextView | |
android:id="@+id/level" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/app_title" | |
android:layout_centerHorizontal="true" | |
android:text="@string/start_level" // cool plus+ text | |
android:textAppearance="?android:attr/textAppearanceLarge" /> | |
<Button | |
android:id="@+id/next_level_button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:text="@string/next_level" /> // cool plus+ button | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<string name="app_name">PattyCoolAdApp</string> // app title | |
<string name="action_settings">Settings</string> | |
<string name="interstitial_ad_sample">Pattys Ad App Company</string> // ad theme title | |
<string name="start_level">Cool</string> // cool plus+ text | |
<string name="next_level">Cool+Plus</string> // coolplus+ button | |
<string name="interstitial_ad_unit_id">PattyID</string> PattyId | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment