Sunday, October 9, 2011

[android-developers] Re: scrollview can't work programmatically

I think it has to do with you are extending ViewGroup. Try this redone code where it is extending LinearLayout instead and see if it does what you need it to.

    public class MyView extends LinearLayout
    {
        ScrollView myScrollView;
        TextView tv;

        public MyView( Context context )
        {
            super(context);
            myScrollView = new ScrollView(context);
            myScrollView.setBackgroundColor(0xfff00fff);
            tv = new TextView(context);
            tv.setTextColor( Color.BLACK );

            tv.setText("sadfasdfasdfasdfasdfasdfasdfsadfsadf");
            tv.setHeight(5000);
            tv.setWidth(3200);

            myScrollView.addView( tv, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT ) );
            addView( myScrollView, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT ) );
        }

        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b)
        {
            int width = r-l;
            int height =b-t;
            myScrollView .layout(0, 0, width, height-100);
        }
    }

It works in a test situation, but not sure it will do what you want in your application, so give it a shot and let us know.

Steven
Studio LFP
http://www.studio-lfp.com


On Sunday, October 9, 2011 1:08:22 PM UTC-5, billconan wrote:
Hello guys,

I want to use scrollview in my app. I tried to add a text view into
the scrollview, but I can't see anything rendered, except the
background color of the scroll view.

here is how I did it:
public class MyView extends ViewGroup
{
ScrollView myScrollview;
        Textview tv;

        public MyView(Context context) {
myScrollView = new ScrollView(context);
myScrollView.setBackgroundColor(0xfff00fff);

textview=new TextView(context);

textview.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                textview.setLayoutParams(params)
textview.setText("sadfasdfasdfasdfasdfasdfasdfsadfsadf");

textview.layout(0, 0, 1000, 2000);
textview.setHeight(5000);
textview.setWidth(3200);
                myScrollView .addView(tv);
                addView(myScrollview);
        }

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
// TODO Auto-generated method stub

int width = r-l;
int height =b-t;

myScrollView .layout(0, 0, width, height-100);
}
}


I found almost all the scrollview tutorial are using xml to define the
view. But I want to do it in a programmatic way. But anyway, I also
tried xml.

I copied Romain Guy's xml from here for testing :http://www.curious-
creature.org/2010/08/15/scrollviews-handy-trick/

The xml scrollview by itself is correct, if I create this scrollview
and add it to the activity, using

scrollview= (ScrollView)
getLayoutInflater().inflate(R.layout.scrollviewid,null);
setContentView(scrollview);

or setContentView(R.layout.scrollviewid);

it worked. However, if I want to make the scrollview a child view of
some other view, again I could only see the background of the
scrollview. nothing inside is rendered:

public class MyView extends ViewGroup
{
       ScrollView myScrollview;

        public MyView(Activity activity,Context context)
        {
                super(context);

myScrollview= (ScrollView)
activity.getLayoutInflater().inflate(R.layout.restaurantcategoryselectorviewlayout,null);
myScrollview.setBackgroundColor(0xfff00fff);

addView(myScrollview);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
// TODO Auto-generated method stub

int width = r-l;
int height =b-t;

myScrollview.layout(0, 0, width, height-100);
}
}



What's wrong with my code? Is there any example of creating scrollview
with program not xml?

Also, are those java source code of android also at kernel.org? since
the git service is down, where can I download android source code?

Thanks you!

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

No comments:

Post a Comment