Admob code not working - android

The following code is what I implemented just to check admob ads retrieval but I couldn't get any ads although the code is even no crashing.
Please inform what is wrong in this code.
public class MainActivity extends Activity {
public AdView admobView;
public View mainView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
admobView = new AdView(this, AdSize.BANNER, "Top secret");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
admobView.setLayoutParams(lp);
//mainView.setLayoutParams(lp);
RelativeLayout layout = new RelativeLayout(this);
AdRequest adRequest = new AdRequest();
//tested this
adRequest.addTestDevice(AdRequest.TEST_EMULATOR); // Emulator
//tested this
//adRequest.addTestDevice("20cb18e8ffc74ad2");
//tested this
//adRequest.addMediationExtra("3d879f364d5747c5", null);
admobView.loadAd(adRequest);
layout.addView(admobView);
//layout.addView(mainView);
setContentView(layout);
}

Did you add permissions and congifchanges of admob in your menifest. Cause i have run your code and it displayed in my emulator. And also in order to display add your app's targetSdkVersion need to be greater then 14.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
</activity>
Activity is as like you
public AdView admobView;
public View mainView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
admobView = new AdView(this, AdSize.BANNER, "a14f39f0b52dce6");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
admobView.setLayoutParams(lp);
//mainView.setLayoutParams(lp);
RelativeLayout layout = new RelativeLayout(this);
AdRequest adRequest = new AdRequest();
//tested this
adRequest.addTestDevice(AdRequest.TEST_EMULATOR); // Emulator
//tested this
//adRequest.addTestDevice("20cb18e8ffc74ad2");
//tested this
//adRequest.addMediationExtra("3d879f364d5747c5", null);
admobView.loadAd(adRequest);
layout.addView(admobView);
//layout.addView(mainView);
setContentView(layout);
}
And Manifest is ==
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ee"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<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|screenLayout|uiMode|screenSize|smallestScreenSize" >
</activity>
</application>
</manifest>

Related

AdMob shows banner on emulator, not shows on device

Admob shows test banner on emulator, logcat form emulator:
05-03 08:07:44.032 798-798/ru.xxx.xxx I/Ads: Starting ad request.
05-03 08:07:45.572 798-798/ru.xxx.xxx W/Ads: Loading already in progress, saving this object for future refreshes.
05-03 08:07:58.812 798-798/ru.xxx.xxx I/Ads: Scheduling ad refresh 60000 milliseconds from now.
05-03 08:07:58.922 798-798/ru.xxx.xxx I/Ads: Ad finished loading.
but when I run app on device, admob not shows test and real banner. logcat from real device:
05-03 13:17:23.388 28142-28142/ru.xxx.xxx I/Ads: Starting ad request.
05-03 13:17:23.983 28142-28142/ru.xxx.xxx W/Ads: Loading already in progress, saving this object for future refreshes.
05-03 13:17:34.748 28142-28247/ru.xxx.xxx W/Ads: There was a problem getting an ad response. ErrorCode: 0
05-03 13:17:34.749 28142-28142/ru.xxx.xxx W/Ads: Failed to load ad: 0
main activity:
public class AndroidLauncher extends AndroidApplication implements AdsController {
private static final String BANNER_AD_UNIT_ID = "xxx";
AdView bannerAd;
InterstitialAd interstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// Create a gameView and a bannerAd AdView
View gameView = initializeForView(new RunawayCat(this), config);
setupAds();
// Define the layout
RelativeLayout layout = new RelativeLayout(this);
layout.addView(gameView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layout.addView(bannerAd, params);
setContentView(layout);
}
public void setupAds() {
bannerAd = new AdView(this);
bannerAd.setVisibility(View.INVISIBLE);
bannerAd.setAdUnitId(BANNER_AD_UNIT_ID);
bannerAd.setAdSize(AdSize.SMART_BANNER);
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest ad = builder.build();
builder.addTestDevice("xxxx");
bannerAd.loadAd(ad);
}
#Override
public void showBannerAd() {
runOnUiThread(new Runnable() {
#Override
public void run() {
bannerAd.setVisibility(View.VISIBLE);
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest ad = builder.build();
bannerAd.loadAd(ad);
}
});
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.romavaleev.runawaycat"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/GdxTheme" >
<activity
android:name=".AndroidLauncher"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version"/>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
admob account is not banned, i tested on 3 devices.permissions in manifest is right
Try removing the builder.addTestDevice("xxxx")
Somthing like this
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest ad = builder.build();
bannerAd.loadAd(ad);
I integrate Ads everyday and I have never needed to add the test device, it always shows test ads if you use a test id for Ads
You can use this test banner id
ca-app-pub-3940256099942544/6300978111
and replace this id when you will generate your final APK
Just a guess. It could be you're assigning the testDevice after building the ad?
AdRequest ad = builder.build();
builder.addTestDevice("xxxx");
Swtich the two lines:
builder.addTestDevice("xxxx");
AdRequest ad = builder.build();

ADMOB - The following classes could not be instantiated: - com.google.android.gms.ads.AdView

I updated my old admob to the new admob. I downloaded SDK, imported google play service library and done everything in google documantation. But when I add the xml code:
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub-XXXXXXXXXXXXXXX/YYYYYYYY"
ads:adSize="BANNER"/>
I get the following message in error log:
The following classes could not be instantiated: - com.google.android.gms.ads.AdView
There is Manifest XML code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<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=".MainActivity"
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.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Coul you help me please?
You can create AdView in java code, but not in xml.
it works for me:
private AdView adView;
private static final String AD_UNIT_ID = "XXXXXXX";
#Override
public void onCreate(){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
#Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
#Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
/** Called before the activity is destroyed. */
#Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
Have you edited the proguard-project.txt? If not then open the proguard-project.txt file and put these lines there...
-keep public class com.google.android.gms.ads.** {
public *;
}
-keep public class com.google.ads.** {
public *;
}
Set target=android-13 or higher in your project.properties.

Android addmob app force closed

I am working with android app, and I am trying to add the google addmob to my app. And there are no errors in my code but the app seems to be force closed while running. I cant find the error. Please help me,
I used the code -
adView = new AdView(this, AdSize.BANNER, "my add id");
RelativeLayout layout = (RelativeLayout)findViewById(R.id.adView);
layout.addView(adView);
AdRequest request = new AdRequest();
request.setTesting(false);
adView.loadAd(request);
please help me since i am new to android development
Logcat:
11-09 17:26:43.508: E/AndroidRuntime(8982): at android.app.Activity.setContentView(Activity.java:1862)
11-09 17:26:43.508: E/AndroidRuntime(8982): Caused by: java.lang.ClassNotFoundException: com.google.ads.AdView
// try this
1. download latest google-play-services,jar
2. add jar to project build path
**main.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/lnrMain">
</LinearLayout>
public class MyActivity extends FragmentActivity {
LinearLayout lnrMain;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lnrMain = (LinearLayout) findViewById(R.id.lnrMain);
runOnUiThread(new Runnable() {
#Override
public void run() {
AdView adView = new AdView(MyActivity.this);
adView.setAdUnitId("0445b7141d9d4e1b");
adView.setAdSize(AdSize.BANNER);
AdRequest.Builder builder = new AdRequest.Builder();
builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
adView.loadAd(builder.build());
lnrMain.addView(adView);
}
});
}
}
**AndroidManifest.xml**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Demo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
<activity
android:name="MyActivity"
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.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
<meta-data
android:name="ADMOB_ALLOW_LOCATION_FOR_ADS"
android:value="true" />
<meta-data
android:name="com.google.android.gms.version"
android:value="4030500" />
</application>
</manifest>
Yesterday I had my first app integrated with admob and what I did is just I added a .jar Library to Java Build Path,. When in Manifest file I added these lines above closing application tag:
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
and these permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
This is how my Manifest.xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="PACKAGENAME"
android:versionCode="1"
android:versionName="1.1" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<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|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>
When in my main_acitivty.xml I added these lines of code where I actually want to see my Ad Banner:
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="YOU-ID"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
I actually did not write any code in my .java file. Because you can choose whetever to implement AdMob via XML or programatically.
All these steps are provided in here -> AdMob Banners
here is my code am used
****MainActivity.java`
package com.example.smacon;
import com.google.ads.*;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Surface;
import android.webkit.WebView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class MainActivity extends Activity {
private AdView adView;
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdRequest adRequest = new AdRequest();
//test mode on EMULATOR
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
//test mode on DEVICE (this example code must be replaced with your device uniquq ID)
adRequest.addTestDevice("4G74FC73D62D42B62A7F7DA61EF5F776");
adView = (AdView)findViewById(R.id.adView);
// Initiate a request to load an ad in test mode.
// You can keep this even when you release your app on the market, because
// only emulators and your test device will get test ads. The user will receive real ads.
adView.loadAd(adRequest);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("my url");
webView.setWebViewClient(new MyWebViewClient());
}
#Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
***** MyWebViewClient.java
package com.example.smacon;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebViewClient extends WebViewClient {
// show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// webProg.setVisibility(View.GONE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
**** manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smacon"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.smacon.MainActivity"
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|screenLayout|uiMode
|screenSize|smallestScreenSize" >
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
****activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="7565756565"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
</LinearLayout>
no errors in this code. But it is forced closed . :(

My UI code doesn't show anything in emulator

I'm starting out in android development and I grabbed a sample code snippet from a book for a few text fields on an interface. I checked over and over that my code is identical to that in the book, but every time I compile and run, I get nothing but a black screen in the emulator.
Here's my code:
package me.kevinossia.mystuff;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity
{
private LinearLayout nameContainer;
private LinearLayout addressContainer;
private LinearLayout parentContainer;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
createNameContainer();
createAddressContainer();
createParentContainer();
setContentView(parentContainer);
}
private void createNameContainer()
{
nameContainer = new LinearLayout(this);
nameContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
nameContainer.setOrientation(LinearLayout.HORIZONTAL);
TextView nameLbl = new TextView(this);
nameLbl.setText("Name: ");
nameContainer.addView(nameLbl);
TextView nameValueLbl = new TextView(this);
nameValueLbl.setText("Kevin");
nameContainer.addView(nameValueLbl);
}
private void createAddressContainer()
{
addressContainer = new LinearLayout(this);
addressContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
addressContainer.setOrientation(LinearLayout.VERTICAL);
TextView nameLbl = new TextView(this);
nameLbl.setText("Address: ");
addressContainer.addView(nameLbl);
TextView nameValueLbl = new TextView(this);
nameValueLbl.setText("26662");
addressContainer.addView(nameValueLbl);
}
private void createParentContainer()
{
parentContainer = new LinearLayout(this);
parentContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
parentContainer.setOrientation(LinearLayout.VERTICAL);
parentContainer.addView(nameContainer);
parentContainer.addView(addressContainer);
}
}
What am I missing here?
Here's my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.kevinossia.mystuff.tutorial"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" />
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme">
</application>
</manifest>
Here's the new manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.kevinossia.mystuff.tutorial"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" />
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme">
<activity
android:name="me.kevinossia.mystuff.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" android:theme="#android:style/Theme.Holo" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
You don't have anything as your launcher in your manifest. You should have something like
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme">
<activity
android:name="me.kevinossia.mystuff.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" android:theme="#android:style/Theme.Holo" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
inside the <application> tag. These are the important lines for that.
<intent-filter>
<action android:name="android.intent.action.MAIN" >
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
depending on your app
manifest.xml
intent-filters

Admob Banner Ad not displaying in Android

Hi I have a Android game app developed using android cocos2d i need to integrate admob i have included the following code but banner ad is not displaying please help regarding this your answers will be appreciated and it would be a great help. thank you.
public class MainActivity extends Activity {
private CCGLSurfaceView mGLSurfaceView;
private boolean isCreated = false;
public static FrameLayout m_rootLayout;
AdView adView;
// This is used to display Toast messages and is not necessary for your app
#Override
protected void onCreate(Bundle savedInstanceState) {
if (!isCreated) {
isCreated = true;
} else {
return;
}
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
try{
LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
getWindowManager().getDefaultDisplay().getWidth(),
getWindowManager().getDefaultDisplay().getHeight()+getWindowManager().getDefaultDisplay().getHeight()-50);
adView = new AdView(this, AdSize.BANNER, "admob id");
AdRequest request = new AdRequest();
adView.loadAd(request);
// Adding full screen container
addContentView(adView, adParams);
}catch (Exception e) {
FlurryAgent.logEvent("ADMOB ERROR: "+e);
}
mGLSurfaceView = new CCGLSurfaceView(this);
setContentView(mGLSurfaceView);
CCDirector.sharedDirector().attachInView(mGLSurfaceView);
getScaledCoordinate();
Global.assetManager = getAssets();
Global.context = this;
Global.loadUserInfo();
CCScene scene = CCScene.node();
scene.addChild(new SplashScene(), -1);
CCDirector.sharedDirector().runWithScene(scene);
//-------------IAP-----------------------
Log.d(TAG1, "Creating IAB helper.");
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.enableDebugLogging(true);
Log.d(TAG1, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d(TAG, "Setup finished.");
if (!result.isSuccess()) {
// Oh noes, there was a problem.
complain("Problem setting up in-app billing: " + result);
return;
}
// Hooray, IAB is fully set up. Now, let's get an inventory of stuff we own.
Log.d(TAG, "Setup successful. Querying inventory.");
mHelper.queryInventoryAsync(mGotInventoryListener);
}
});
Global.myActivity=this;
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.game.puzzlegame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.game.puzzlegame.MainActivity"
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.revmob.ads.fullscreen.FullscreenActivity"
android:configChanges="keyboardHidden|orientation" >
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"></activity>
</application>
Please review all these additions in your project.
In Java class:
AdView layout = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest();
//adRequest.setTesting(true);
layout.loadAd(adRequest);
In Layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="your key" />
</LinearLayout>
In Manifest:
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation" />
I have worked with AdMob Lib. Ver. 4.0.4 which worked fine in my case.
Try this code
LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
getWindowManager().getDefaultDisplay().getWidth(),
getWindowManager().getDefaultDisplay().getHeight()+getWindowManager().getDefaultDisplay().getHeight()-50);
AdView ad = new AdView(this, AdSize.BANNER, "admob_id");
adParams.addView(ad);
AdRequest r = new AdRequest();
//r.setTesting(true);
ad.loadAd(r);
Make sure that you included correct ID and have correspond permissions for your app.
In addition it would be good to implement ADListener that really displays what how its going with ads rendering.
Also don't forget to set test mode to query ads, AdMob guys can just ban you for irresponsible clicks.
for latest Ads in Android
use below code
AdView layout = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
//adRequest.setTesting(true);
layout.loadAd(adRequest);
beacuse AdRequest class final class so we can not create object.

Categories

Resources