Reputation: 31
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?
Upvotes: 3
Views: 8797
Reputation: 190
When you programmatically create a ScrollView
inside of it you need to create a View
then add the ScrollView
inside this View
.
LinearLayout maincontainer = (LinearLayout) findViewById(R.id.weatherInfo);
maincontainer.setOrientation(LinearLayout.HORIZONTAL);
final HorizontalScrollView scrollView = new HorizontalScrollView(getApplicationContext());
maincontainer.addView(scrollView);
final LinearLayout linearLayout = new LinearLayout(getApplicationContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
scrollView.addView(linearLayout);
Upvotes: 3
Reputation: 950
I'm not clear what's wrong with your ViewGroup, but that seems to be where the problem is. If I take your code, debug it (the code posted above has several errors), and put it into the start code for a simple activity, it will then work as expected. It creates a scrolling text area with your test text.
Here's that code. Note that it expects the layout file to contain a simple linear layout with id linearLayout1
:
public class ListTestActivity extends Activity {
LinearLayout layout;
ScrollView myScrollView;
TextView textview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (LinearLayout) this.findViewById(R.id.linearLayout1);
myScrollView = new ScrollView(this);
myScrollView.setBackgroundColor(0xfff00fff);
myScrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
textview = new TextView(this);
textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
textview.setText("sadfasdfasdfasdfasdfasd fasdfsadfsadf");
myScrollView.addView(textview);
layout.addView(myScrollView);
}}
Upvotes: 1