Hello,

I'm trying to show a ScrollView with a button anchored to bottom of
screen. This is coded programmatically (without using XML layout for
now).

The trouble is when the ScrollView has many items (requiring
scrolling), it prevents the button at the bottom from showing. The
bottom button is sort of pushed off the screen.

Look at the code below. I have a TODO which mentions a workaround to
specify the exact pixel height of the ScrollView, but this is not a
good solution. If you change the for loop to add only 2 buttons, then
the bottom button will show.

So how do I make the ScrollView show a scrolling list, with the bottom
button in view, and without specifying an exact size of the
ScrollView? I've seen some discussions here:

http://groups.google.com/group/android-developers/browse_thread/thread/fe2862a135b46f7f/bae38e8cf012e06a?lnk=gst&q=add+button+below+scrollview#bae38e8cf012e06a

http://groups.google.com/group/android-developers/browse_thread/thread/7d45c5ba63c9f8e9/ec675a5ce8835d1d?lnk=gst&q=set+layout_weight+in+code#ec675a5ce8835d1d

But I don't know how to apply these to my situation. Please help.

package com.test.ScrollViewTest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MyScrollViewTest extends Activity
{
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);

                LinearLayout ll = new LinearLayout(this); // the main layout of 
the
screen
                ll.setOrientation(LinearLayout.VERTICAL);

                TextView tv1 = new TextView(this);
                tv1.setText("First line of text");

                TextView tv2 = new TextView(this);
                tv2.setText("Second line of text");

                ll.addView(tv1);
                ll.addView(tv2);

                ScrollView sv = new ScrollView(this);
                // You can only add one child (usually a layout) to a 
scrollview,
                // otherwise you get exception: "ScrollView can host only one 
direct
child"
                LinearLayout ll2 = new LinearLayout(this);

                ll2.setOrientation(LinearLayout.VERTICAL);

                /*
                 * TODO: PROBLEM HERE: You cannot use WRAP_CONTENT for height,
because
                 * the layout below the scrollView will not show (it's sort of
pushed
                 * off the screen - even if a margin is specified).
                 *
                 * An alternate value of 300 works OK (remove the bottom margin
too).
                 * But this is only suitable for screen size 320x480 in portrait
mode.
                 */
                LinearLayout.LayoutParams paramsLinear = new
LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.FILL_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT /*300*/);

                // Add 50 pixel margin to bottom of scrollview (this margin 
doesn't
scroll)
                paramsLinear.bottomMargin = 50;

                sv.addView(ll2);

                // Add some stuff to scrollView to test its depth
                // (it should leave a margin at bottom of screen)
                for (int i = 1; i < 10; i++)
                {
                        TextView textView = new TextView(this);
                        textView.setText("Text View " + i);
                        LinearLayout.LayoutParams p = new 
LinearLayout.LayoutParams(
                                        LinearLayout.LayoutParams.FILL_PARENT,
                                        LinearLayout.LayoutParams.WRAP_CONTENT);
                        ll2.addView(textView, p);

                        Button buttonView = new Button(this);
                        buttonView.setText("Button " + i);
                        ll2.addView(buttonView, p);
                }

                ll.addView(sv, paramsLinear);

                RelativeLayout rl = new RelativeLayout(this);

                RelativeLayout.LayoutParams paramsRelative = new
RelativeLayout.LayoutParams(
                                RelativeLayout.LayoutParams.FILL_PARENT,
                                RelativeLayout.LayoutParams.WRAP_CONTENT);

                // Make the button anchored to the bottom of the screen...
                // ...But this doesn't work when the ScrollView has a scrolling 
list
of items. Why??
                paramsRelative.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                rl.setLayoutParams(paramsRelative);

                Button b = new Button(this);
                b.setText("Button anchored to bottom");
                rl.addView(b, paramsRelative);
                ll.addView(rl);

                setContentView(ll);
        }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to