0

I have add the Banners ads in my Android app. Ads are being showed in the design of layout file but it is not showing in the app

Here is the main class where i am adding the AdView

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_main_list);
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    mAdView.loadAd(adRequest);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.maintitlebar);
    loadPage();

    /*AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);*/
}

Here is the layout file of the above java file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context=".MainListActivity$PlaceholderFragment"
android:padding="0dp"
>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
    android:layout_alignParentTop="true">

    </ListView>
<TextView
    android:id="@android:id/empty"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:text="@string/no_text"
    />
<com.google.android.gms.ads.AdView
    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>

So how to show ads in android app

Log Cat error is following

11-08 03:42:39.923    2519-2613/com.example.talha.appforblog W/Ads﹕ There was a problem getting an ad response. ErrorCode: 0
11-08 03:42:39.931    2519-2519/com.example.talha.appforblog W/Ads﹕ Failed to load ad: 0
user3585510
  • 131
  • 2
  • 10
  • Did you see what's the response from Admob in your logcat? Also - does the ListView cover the entire screen? – SuperFrog Nov 08 '15 at 00:08
  • Yes , Listview cover the entire screen along with title bar only. I have edited the Log Cat response in the above code. PLease look at it – user3585510 Nov 08 '15 at 05:40

2 Answers2

0

Try to give the Banners ads some space something like this:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/child_r1">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="400dp"
    android:id="@+id/listView_"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:transitionGroup="false" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/child_r2"
    android:gravity="center_vertical|center|center_horizontal"
    android:layout_below="@+id/child_r1">


    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

First import these two in your main:

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

Second Put only this code for adview:

 AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

Third be sure that you have this:

    compile 'com.google.android.gms:play-services-ads:8.1.0'

In your "buld.gradle" under "dependencies" Something like this:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.google.android.gms:play-services-ads:8.1.0'

}
dragon3002
  • 131
  • 2
  • 4
  • 14
0

Try to set ListView above AdView

Set android:layout_above attribute to ListView.

Something like this:

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
    android:layout_alignParentTop="true"
    ***android:layout_above="@+id/adView"***>
   </ListView>

<TextView
    android:id="@android:id/empty"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:text="@string/no_text"
    />
<com.google.android.gms.ads.AdView
    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>
Darshan
  • 515
  • 1
  • 3
  • 16