0% found this document useful (0 votes)
35 views

MD Lab 5

The document describes creating an Android application with buttons and interstitial ads. It includes the steps to set up the project, layout the user interface with text, buttons and images, and code the app functionality in Java. The app loads and displays interstitial ads between levels, tracking the current level number. The code handles loading, displaying, and error handling for the interstitial ads.

Uploaded by

DHU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

MD Lab 5

The document describes creating an Android application with buttons and interstitial ads. It includes the steps to set up the project, layout the user interface with text, buttons and images, and code the app functionality in Java. The app loads and displays interstitial ads between levels, tracking the current level number. The code handles loading, displaying, and error handling for the interstitial ads.

Uploaded by

DHU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment - 5

Problem Definition: Create an android application with button


Procedure:
Step 1: Create a New Android Project.
Step 2: Configure the Project Settings.
Step 3: Add an App Launcher Icon.
Step 4: Select the App Activity Type.
Step 5: Specify Activity Details.
Step 6: Review Your Simple App.

XML – Code and Design:


<?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"
android:background="#D3FAEC"
tools:context=".MainActivity">

<!-- view for AdMob Interstitial Ad -->

<TextView
android:id="@+id/app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="50dp"
android:layout_marginEnd="88dp"
android:text="@string/navrachana_university"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#F60C0C" />

<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/cse_20124017"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#D51717" />

<Button
android:id="@+id/hello_my_friend_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/hello_my_friend" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="109dp"
android:layout_height="136dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="151dp"
android:layout_marginEnd="151dp"
android:layout_marginBottom="442dp"
app:srcCompat="@drawable/download" />

</RelativeLayout>

Java Code:
package com.example.a20124017_002;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

import androidx.annotation.NonNull;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Locale;

import android.widget.Toast;

import com.example.a20124017_002.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {


// Remove the below line after defining your own ad unit ID.
private static final String TOAST_TEXT = "Test ads are being shown. "
+ "To show live ads, replace the ad unit ID in res/values/strings.xml with your own ad
unit ID.";
private static final String TAG = "MainActivity";

private static final int START_LEVEL = 1;


private int mLevel;
private Button mNextLevelButton;
private InterstitialAd mInterstitialAd;
private TextView mLevelTextView;
private ActivityMainBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

MobileAds.initialize(this, new OnInitializationCompleteListener() {


@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});

// Load the InterstitialAd and set the adUnitId (defined in values/strings.xml).


loadInterstitialAd();

// Create the next level button, which tries to show an interstitial when clicked.
mNextLevelButton = binding.helloMyFriendButton;
mNextLevelButton.setEnabled(false);
mNextLevelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showInterstitial();
}
});

// Create the text view to show the level number.


mLevelTextView = binding.level;
mLevel = START_LEVEL;

// Toasts the test ad message on the screen. Remove this after defining your own ad unit
ID.
Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show();
}

@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_main, 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);
}

public void loadInterstitialAd() {


AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this, getString(R.string.interstitial_ad_unit_id), adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
mNextLevelButton.setEnabled(true);

Toast.makeText(MainActivity.this, "onAdLoaded()",
Toast.LENGTH_SHORT).show();
interstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
// Make sure to set your reference to null so you don't
// show it a second time.
mInterstitialAd = null;
Log.d(TAG, "The ad was dismissed.");
}

@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
// Make sure to set your reference to null so you don't
// show it a second time.
mInterstitialAd = null;
Log.d(TAG, "The ad failed to show.");
}

@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d(TAG, "The ad was shown.");
}
});
}

@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.i(TAG, loadAdError.getMessage());
mInterstitialAd = null;
mNextLevelButton.setEnabled(true);

String error = String.format(


Locale.ENGLISH,
"domain: %s, code: %d, message: %s",
loadAdError.getDomain(),
loadAdError.getCode(),
loadAdError.getMessage());
Toast.makeText(
MainActivity.this,
"onAdFailedToLoad() with error: " + error, Toast.LENGTH_SHORT)
.show();
}
});
}

private void showInterstitial() {


// Show the ad if it"s ready. Otherwise toast and reload the ad.
if (mInterstitialAd != null) {
mInterstitialAd.show(this);
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
goToNextLevel();
}
}

private void goToNextLevel() {


// Show the next level and reload the ad to prepare for the level after.
mLevelTextView.setText("Level " + (++mLevel));
if (mInterstitialAd == null) {
loadInterstitialAd();
}
}
}

Manifests:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.a20124017_002">

<!-- 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:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.20124017_002"
tools:targetApi="31">

<!--
You can find your app ID in the AdMob UI. For android:value,
insert your own AdMob app ID in quotes, as shown below.
Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713
-->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />

<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name">
<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:exported="false"
android:theme="@android:style/Theme.Translucent" />
</application>

</manifest>
Output:
simple application interface with university name ,enrollment no and logo..

You might also like