<!-- build.gradle -->
apply plugin: 'com.android.application'
android {
compileSdkVersion 28 defaultConfig {
applicationId "com.example.interstitialads" minSdkVersion 16 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }
buildTypes {
release {
minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' }
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.google.android.gms:play-services-ads:17.2.0'}
<!-- ads activity -->
<?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=".Ads">
<!-- view for AdMob Interstitial Ad --> <TextView android:id="@+id/app_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="@string/homePage" 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:textColor="@color/colorPrimary"
android:text="Go to Website" /></RelativeLayout>
<!-- ads.java -->
package com.example.interstitialads;
import com.google.android.gms.ads.AdListener;import com.google.android.gms.ads.AdRequest;import com.google.android.gms.ads.InterstitialAd;
import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.WindowManager;import android.widget.Button;import android.widget.Toast;
public class Ads extends AppCompatActivity {
private Button mNextLevelButton; private InterstitialAd mInterstitialAd;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_ads);
//hiding title bar getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//hiding action bar getSupportActionBar().hide();
// 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 InterstitialAd and set the adUnitId (defined in values/strings.xml). mInterstitialAd = newInterstitialAd(); loadInterstitial();
}
private InterstitialAd newInterstitialAd() {
InterstitialAd interstitialAd = new InterstitialAd(this); interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id)); 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() {
// Show the ad if it's ready. Otherwise toast and reload the ad. if (mInterstitialAd != null && mInterstitialAd.isLoaded())
{
mInterstitialAd.show(); }
else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show(); goToNextLevel();
}
}
private void loadInterstitial()
{
// Disable the next level 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()
{
Intent intent = new Intent(getApplicationContext(),web.class); startActivity(intent); }
}
<!--web.xml -->
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".web">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"
>
<WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_marginBottom="0dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" tools:layout_editor_absoluteY="21dp"/> </LinearLayout>
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"> </com.google.android.gms.ads.AdView>
</RelativeLayout>
<!--ads.java -->
package com.example.interstitialads;
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.WindowManager;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;
import com.google.android.gms.ads.AdRequest;import com.google.android.gms.ads.AdView;import com.google.android.gms.ads.MobileAds;
public class web extends AppCompatActivity {
public WebView a; private AdView mAdView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_web);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//hiding title bar
getSupportActionBar().hide();//hiding action bar
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest);
a =(WebView)findViewById(R.id.webView); WebSettings webSettings = a.getSettings(); webSettings.setJavaScriptEnabled(true); a.loadUrl("https://www.w3schools.com");
//http://arfan.epizy.com //https://nahin221.blogspot.com //https://nahindcoder.blogspot.com/ a.setWebViewClient(new WebViewClient());
}
@Override public void onBackPressed() {
if (a.canGoBack())
{
a.goBack(); }
else {
super.onBackPressed(); }
}
}
<!--menifest -->
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.interstitialads"> <!-- Include required permissions for Google Mobile Ads to run. --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:allowBackup="true" android:icon="@drawable/iconny" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".web"></activity> <activity android:name=".Ads" android:label="@string/title_activity_ads"> <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Include the AdActivity configChanges and theme. --> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" />
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713" /> </application>
</manifest>
<resources> <string name="app_name">Interstitial Ads</string> <string name="title_activity_ads">Ads</string>
<string name="action_settings">Settings</string>
<string name="homePage">Welcome Page</string>
<!-- - This is an ad unit ID for an interstitial test ad. Replace with your own interstitial ad unit id. For more information, see https://support.google.com/admob/answer/3052638 <!- --> <string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/8691691433</string></resources>
<--value-->
<resources> <string name="app_name">Interstitial Ads</string> <string name="title_activity_ads">Ads</string>
<string name="action_settings">Settings</string>
<string name="homePage">Welcome Page</string>
<!-- - This is an ad unit ID for an interstitial test ad. Replace with your own interstitial ad unit id. For more information, see https://support.google.com/admob/answer/3052638 <!- --> <string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/8691691433</string></resources>