1

I'm having trouble placing an AdMob banner in settings. I have managed to place it in the first screen by using the following layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLinearLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:baselineAligned="false">

   <fragment
       android:id="@+id/admob"
       android:name="ro.infiniteloop.phonehancer.AdmobFragment"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/settings"
        android:name="ro.infiniteloop.phonehancer.SettingsFragment"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />

</LinearLayout>

The AdmobFragment layout being:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/AdmobLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ro.infiniteloop.phonehancer.AdmobFragment" >

    <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="ca-app-pub-5599997006266549/8381977716" />

</LinearLayout>

And the settings fragment layout:

<PreferenceCategory
    android:key="pref_key_storage_settings"
    android:title="@string/pref_sms_storage_title" >

    <CheckBoxPreference
        android:defaultValue="false"
        android:key="pref_key_auto_delete"
        android:summary="@string/pref_summary_auto_delete"
        android:title="@string/pref_title_auto_delete" />

.....

An image of the output

All good so far. But when I go inside a child preference screen the original layout is reset and the ad is no longer displayed

An image of the child preference screen

Is there a way to lay these elements inside a layout or to write some custom code that keeps the ad on all preference child screens?

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40

1 Answers1

0

Create a layout containing only admob

admob.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/AdmobLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<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="ca-app-pub-5599997006266549/8381977716" />

now add this view in settings fragment.

  getActivity().setcontentView(R.layout.admob);

Your ad will be visible

hitesh
  • 378
  • 4
  • 12
  • 1
    I coded the admob fragment in a separate layout from the beginning. My question is more how to keep the ad displayed on secondary (child) preference screens – user2260455 Oct 14 '14 at 12:48
  • Somebody already found a solution? Really need an answer. Thanks – Thomas Vos Jul 23 '15 at 11:38