2

I'm using a custom layout to display preferences so that I can insert an ad banner at the bottom of the screen. The idea is to have the banner stick to the bottom of the page, and have the preferences displayed in the rest of the screen in a scrollview. I have a problem with the height taken by the preferences. I've currently hardcoded it to "2000dip" in the XML layout below.

It works fine, the banner stays at the bottom, preferences at the top in a scrollable view.

But having a hardcoded height value is bad and I'd like it to automatically being set to the exact height taken by the preferences because currently it is either too short or too long (with a blank area after the preferences). I've tried to replace the hardcoded value with wrap_content or fill_parent with no success. Any idea?

In my PreferenceActivity I've included the following two lines:

    setContentView(R.layout.preferences_scrollable);
    addPreferencesFromResource(R.layout.preferences);

And I've defined a layout preferences_scrollable in the xml file preferences_scrollable.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">

    <ScrollView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_weight="1"
        android:id="@+id/pref_scrollable_sv">

        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:orientation="vertical"
            android:id="@+id/pref_scrollable_ll">

            <ListView android:id="@android:id/list"
                android:layout_width="fill_parent" android:layout_height="2000dip">

            </ListView>
        </LinearLayout>
    </ScrollView>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="vertical">
        <com.google.ads.AdView android:id="@+id/ad"
            ads:adSize="BANNER" android:layout_width="fill_parent"
            ads:loadAdOnCreate="true" ads:adUnitId="xxx"
            android:layout_height="wrap_content" 
            android:layout_weight="0"/>
    </LinearLayout>
</LinearLayout>
gpo
  • 3,388
  • 3
  • 31
  • 53

3 Answers3

1

Using the attribute "android:fillViewport" in the scrollview makes it have a correct height. So that would be the solution except that with this attribute, the view doesn't scroll, or scrolls very badly. Looks like an Android bug to me.

Anyway, the solution that works is to drop the ScrollView and instead use a LinearLayout (that supports scrolling as well). The layout correctly working with a scrollable preference list (content filled from Java code) on top and an ad banner on the bottom is given below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <LinearLayout android:layout_width="fill_parent"
        android:id="@+id/ad_layout" android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:layout_alignParentBottom="true">

        <com.google.ads.AdView android:id="@+id/ad"
            ads:adSize="BANNER" android:layout_width="fill_parent"
            ads:loadAdOnCreate="true" ads:adUnitId="xxx"
            android:layout_height="wrap_content" primaryTextColor="#FFFFFF"
            secondaryTextColor="#CCCCCC" />
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:layout_above="@+id/ad_layout">

        <ListView android:id="@android:id/list" android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        </ListView>
</LinearLayout>     
</RelativeLayout>
gpo
  • 3,388
  • 3
  • 31
  • 53
0

Try to set <android:layout_marginRight="10dp" android:layout_marginBottom="10dp" android:layout_marginLeft="5dp>" in layout properties.

Yahor10
  • 2,123
  • 1
  • 13
  • 13
0

Use a RelativeLayout for your outer layout. Example:

   <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" .....>

       <LinearLayout android:layout_alignParentBottom="true" android:id="@+id/adsLayout" ...>
           <!-- This is the ads part of it, which will remain the same as you have above-->
       </LinearLayout>
            <!-- You have to have the ads part first, so you are able to reference it with the scrollview -->
       <ScrollView .... android:layout_above="@+id/adsLayout">
          <!-- This ScrollView contents will remain the same -->
       </ScrollView>
   </RelativeLayout>

So, in the ads portion of the layout, you use android:layout_alignParentBottom="true" to put it at the bottom of the screen. Then in the ScrollView, you put android:layout_above="@+id/adsLayout", so it goes above the ads view. This will format it as you need.

That should work. Just comment if you have issues.

Reed
  • 14,703
  • 8
  • 66
  • 110
  • I'm trying now with a RelativeLayout. However this doesn't solve the problem of the ListView height being incorrect: The Banner is correctly stuck at the bottom of the page. The scrollview takes the remaining screen height, ok. But the ListView contained in the ScroillView doesn't match the height of its content and only takes a little space on the top. Issue seems to be that the ListView only takes the size of its initial content, while in the Java code in my post I changed it to be filled with the Preferences. The ListView doesn't get resized to fit the entire content of the Preferences. – gpo Oct 06 '11 at 08:35
  • Try setting the height of the ListView to "fill_parent" – Reed Oct 06 '11 at 23:50
  • fill_parent didn't help either. But somehow I found a solution and will post it here. – gpo Oct 23 '11 at 13:10