Showing posts with label application. Show all posts
Showing posts with label application. Show all posts

Tuesday, April 1, 2014

How to add Google AdSense in Android Application

,
In this android tutorial, we will learn how to show Google Ads on Android Application. We will need the following two things: 
1. Ad Unit Id from Admob.com
2. Google Play Services installed in Android SDK.

 

How to get Ad Unit Id from admob.com:

1. Go to admob.com
2. Create New Account (if you are already an admob user, then log in).
3. Select Sites & Apps.
4. Select the Device like Android/iOS/Windows.
5. Fill The Description of Your Application -> OK.
6. Go to Sites & Apps.

How to add Google AdSense in Android Application
admob.com


7. Select Manage Application.

How to add Google AdSense in Android Application
Sites & Apps -> Manage Settings

8. Now you can see Ad Unit Id.

How to add Google AdSense in Android Application
Get Ad Unit Id(Publisher ID)

How to get Google Play Services Folder: 

 

1. Open android SDK -> Click on Windows -> android SDK manager -> extras -> download Google play services.
How to add Google AdSense in Android Application
Install Google Play Service Packages


Now we have Admob Ad Unit Id and Google play services folder. Create a new project to test Ads on android Application and use Google play services as a library in your android project.

 

How to Add Google Adsense code in Android Application:


1. Now select project -> right click on project -> properties -> android -> add library -> select Google play services folder -> Apply.

How to add Google AdSense in Android Application
Add Google Play Services Package As a Library

2. Open your android XML file and add the following line in layout:   
           xmlns:ads=http://schemas.android.com/apk/res-auto
3. Add AdView in Layout:
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="YOUR_AD_UNIT_ID"/> 
4. The Android XML file will look like this:


<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="Your_AD_UNIT_ID"/>
</RelativeLayout>
5. Now open your Java file and initialize AdView Object in your Activity.

                 AdView ad=(AdView)findViewById(R.id.adView);
6. Load the ad from Google:

                 ad.loadAd(new AdRequest.Builder().build());

Now change AndroidManifest.xml file according to the code given in the end of the tutorial & run your project. Now install the application on your phone & check ads!

For Interstitial Ads: This Ad will be displayed on your phone screen.

 

1. Use the following code in your activity: 

    InterstitialAd end_ad=newInterstitialAd(this);
    end_ad.setAdUnitId(getResources().getString(“Your_AD_UNIT_ID”));
    end_ad.loadAd(newAdRequest.Builder().build());

2. Show Ad if loaded:

if(end_ad.isLoaded())
          {
             end_ad.show();
          }

Now change AndroidManifest.xml file according to the code given in the end of the tutorial & run your project. Now install the application on your phone & check ads!

Now change AndroidManifest.xml file:

<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Main_Activity"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18"/>

<!-- Used to request banner and interstitial ads. -->
<uses-permissionandroid:name="android.permission.INTERNET"/>

<!-- Used to avoid sending an ad request if there is no connectivity. -->
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>

<activity
android:name="com.example.Main_Activity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<!-- Activity required to show ad overlays. -->
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>

Both the above examples are used in the following Android applications:

JMI SPI Converter

Namaz Guide in English

Download and test the APK File.
Please share and comment to improve this blog.
For more Android tutorials see List of Android Tutorials. Thanks to all.. :)
Read more

Monday, March 31, 2014

AdMob Manager In Android Application

,


This is a sample activity which shows how to use AdMob Manager in your application.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Register or login at admob.com, and download GoogleAdMobAdsSdkAndroid-4.0.4, which contains a GoogleAdMobAdsSdk.jar file.
2.) Create a project MyAdMob.
3.) In your project’s root directory create a subdirectory lib. Copy the AdMob JAR (GoogleAdMobAdsSdk.jar) file into that lib directory.

4.) Right-click on your project from the Package Explorer tab and select Properties.
5.) Select Java Build Path from left panel and Select Libraries tab from the main window.
6.) Click on Add JARs.
7.) Select the JAR copied to the libs directory, Click OK to add the SDK to your Android project.
8.) Add some relevant permissions in AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.sample.MyAdMob" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
   <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyAdMob" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.google.ads.AdActivity"android:configChanges="keyboard|keyboardHidden|orientation" />
   </application>
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
9.) Run the Application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. MyAdMob. Enter following information:
Project name: MyAdMob
Build Target: Android 2.1
Application name: MyAdMob
Package name: com.sample.MyAdMob
Create Activity: MyAdMob
On Clicking Finish MyAdMob code structure is generated with the necessary Android Packages being imported along with MyAdMob.java. MyAdMob class will look like following:
package com.sample.MyAdMob;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MyAdMob extends Activity
{
    private static final String MY_BANNER_UNIT_ID = "a14db01ee16f245";
 
   @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Lookup R.layout.main
        LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
        // Create the adView
        // Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
        AdView adView = new AdView(this, AdSize.BANNER, MY_BANNER_UNIT_ID);
        // Add the adView to it
        layout.addView(adView);
        // Initiate a generic request to load it with an ad
        AdRequest request = new AdRequest();
        request.setTesting(true);
        adView.loadAd(request);          
   }
}
Output –The final output:
Read more
 

Download Android Game Copyright © 2016 -- Powered by Blogger