Study for the day when you don't have to worry about price tags.
--Your friends at LectureNotes

Lab Manuals of Android admob integration lab manual Mca 1st sem by subham Kumar sah

  • Other
  • Uploaded 8 months ago
Subham Kumar Sah
Subham Kumar Sah
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

Admob Tutorial Integrating the Google Mobile Ads SDK into an app is the first step toward displaying ads and earning revenue. Once you've integrated the SDK, you can choose an ad format and follow the steps to implement it. This guide is for publishers who want to monetize an Android app with AdMob and aren't using Firebase. For simplicity, I am not using Firebase. Prerequisites • • • Use Android Studio 1.0 or higher Target Android API level 14 or higher Recommended: Create a Google AdMob account and register an app. Import the Mobile Ads SDK Apps can import the Google Mobile Ads SDK with a Gradle dependency that points to Google's Maven repository. In order to use that repository, you need to reference it in the app's projectlevel build.gradle file. Open yours and look for an allprojects section: Example project-level build.gradle (excerpt) allprojects { repositories { google() jcenter() } } Add the google() directive above if it's not already present. Next, open the app-level build.gradle file for your app, and look for a "dependencies" section. Example app-level build.gradle (excerpt) dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.google.android.gms:play-services-ads:17.2.1' } Add the line in bold above, which instructs Gradle to pull in the latest version of the Mobile Ads SDK. Once that's done, save the file and perform a Gradle sync. Update your AndroidManifest.xml Add your AdMob App ID to your app's AndroidManifest.xml file by adding the <meta-data> tag shown below. You can find your App ID in the AdMob UI. For android:value insert your own AdMob App ID in quotes, as shown below.

Text from page-2

<manifest> <application> <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="[ADMOB_APP_ID]"/> </application> </manifest> Initialize MobileAds Before loading ads, have your app initialize the Mobile Ads SDK by calling MobileAds.initialize() with your AdMob App ID. This needs to be done only once, ideally at app launch. Here's an example of how to call the initialize() method in an Activity: Example MainActivity (excerpt) package ... import ... import com.google.android.gms.ads.MobileAds; public class MainActivity extends AppCompatActivity { ... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 MobileAds.initialize(this, "YOUR_ADMOB_APP_ID"); } ... } Select an ad format The Mobile Ads SDK is now imported and you're ready to implement an ad. AdMob offers a number of different ad formats, so you can choose the one that best fits your app's user experience. Banner: Banner ads are rectangular image or text ads that occupy a spot within an app's layout. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. IMPLEMENT A BANNER

Text from page-3

Add AdView to the layout The first step toward displaying a banner is to place AdView in the layout for the Activity in which you'd like to display it. The easiest way to do this is to add one to the corresponding XML layout file. Here's an example that shows AdView at the bottom of an Activity: main_activity.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:layout_width="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <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> Always test with test ads When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account. The easiest way to load test ads is to use our dedicated test ad unit ID for Android banners: ca-app-pub-3940256099942544/6300978111

Text from page-4

It's been specially configured to return test ads for every request, and you're free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app. Load an ad Once the AdView is in place, the next step is to load an ad. That's done with the loadAd() method in the AdView class. It takes an AdRequest parameter, which holds runtime information (such as targeting info) about a single ad request. Here's an example that shows how to load an ad in the onCreate() method of an Activity: MainActivity (excerpt) package ... import ... import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class MainActivity extends AppCompatActivity { private AdView mAdView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); } } That's it! Your app is now ready to display banner ads. Interstitial: Interstitials are full-screen ads that cover the interface of an app until closed by the user. They're best used at natural pauses in the flow of an app's execution, such as between levels of a game or just after a task is completed. IMPLEMENT AN INTERSTITIAL Create an interstitial ad object Interstitial ads are requested and shown by InterstitialAd objects. The first step is instantiating InterstitialAd and setting its ad unit ID. This is done in the onCreate() method of an Activity:

Lecture Notes