Admob in Android emulator problem

If admob doesn’t show in application tested on emulator try this solution:

Check AndroidManifest.xml
1.) user-permission

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

2.) activity

<activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation"/>

3.) metadata

      <meta-data 
        android:value="my_ad_id_unit" 
        android:name="ADMOB_PUBLISHER_ID" />

This is AndroidManifest.xml example.
You have to type your ID_UNIT in meta-data android:value !!!!!

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cz.okhelp.admob"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 
 
 
    <application android:icon="@drawable/icon" android:label="@string/app_name">
       <meta-data 
        android:value="a14c13151a52fc4" 
        android:name="ADMOB_PUBLISHER_ID" />
        <activity android:name=".AdmobActivity"
                  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>
</manifest>

Activity.java example

 
package cz.okhelp.admob;
 
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 AdmobActivity extends Activity {
   LinearLayout linLay;
private AdView adView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        linLay = (LinearLayout)findViewById(R.id.idReklamaLayout);
 
		try {
			// Create the adView
			 adView = new AdView(this, AdSize.BANNER, "a14c13151a52fc4");//MY_AD_UNIT_ID
			// Lookup your LinearLayout assuming it’s been given
			// the attribute android:id="@+id/mainLayout"
			LinearLayout layout = (LinearLayout)findViewById(R.id.idReklamaLayout);
			// Add the adView to it
			layout.addView(adView);
			// Initiate a generic request to load it with an ad
			adView.loadAd(new AdRequest());
 
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
                        adView = null;
		}
    }
  @Override
  public void onDestroy() {
    if(adView != null)
      adView.destroy();
    super.onDestroy();
  }
}

main.xml example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/idReklamaLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 
 
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>
This entry was posted in Android Examples Code, Bugs, warnings, errors and tagged , , . Bookmark the permalink.


Comments are closed.



4,291 views