views:

43

answers:

2

I've seen the Mario live wallpaper uses admob ads in the settings screen, but I haven't been able to do it myself. If I put the adview into the settings.xml like you would with a normal layout I get a class cast exception.

Here's a screenshot of the mario live wallpaper to illustrate what I'm trying to do.

example screenshot

A: 

You can implement a settings screen in two ways:

  1. Use the preferences framework so that the XML preferences file auto-creates the preferences layout and all its functionality
  2. Write the activity yourself, i.e. on loading show the current preferences, as they are shown, save them, using a normal layout XML file that you create

I suspect you are adding the AdMob object to an XML file that cannot handle it, and can onlky handle preference items. Please post your XML file, and specify what casting error you are seeing.

If you want ultimate control over the contents of the preferences screen, then implement it yourself as a normal activity, and then you can do anything you want.

Ollie C
A: 

I've managed to answer this for myself so I'll post the solution in here in case someone else has the same question.

I added a TabActivity as well as the standard Preferences activity, then nested the Preferences inside a Tab of the TabActivity. This means I've got a normal layout xml for the TabActivity that I can put an adview (or any other type of view) inside and I've still got the generated preferences screen working within that.

Code for the TabActivity

public class SettingsTabActivity extends TabActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tablayout);

        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent for the regular live wallpaper preferences activity
        intent = new Intent().setClass(this, Preferences.class);

        // Initialize a TabSpec and set the intent
        spec = tabHost.newTabSpec("TabTitle").setContent(intent);
        spec.setIndicator("TabTitle");

        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);      
    }
}

Code for the tablayout.xml

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/*your package name goes here for admob*"
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:orientation="vertical"
            android:layout_width="fill_parent" android:layout_height="fill_parent">

            <com.admob.android.ads.AdView android:id="@+id/ad"
                    android:layout_width="fill_parent" android:layout_height="wrap_content"
                    myapp:backgroundColor="#000000" myapp:primaryTextColor="#FFFFFF"
                    myapp:secondaryTextColor="#CCCCCC" />

            <TabWidget android:id="@android:id/tabs"
                    android:layout_width="fill_parent" android:layout_height="1dp"
                    android:tabStripEnabled="false" android:visibility="invisible" />

            <FrameLayout android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent" android:layout_height="fill_parent"
                    android:padding="1dp" />
    </LinearLayout>
</TabHost>

Setting android:visibility="invisible" and android:layout_height="1dp" on the TabWidget tag means the user can't tell it's actually a Tab

James