Android Scrollview Can't Be Created Programmatically.
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
Solution 1:
When you programmatically create a ScrollView
inside of it you need to create a View
then add the ScrollView
inside this View
.
LinearLayoutmaincontainer= (LinearLayout) findViewById(R.id.weatherInfo);
maincontainer.setOrientation(LinearLayout.HORIZONTAL);
finalHorizontalScrollViewscrollView=newHorizontalScrollView(getApplicationContext());
maincontainer.addView(scrollView);
finalLinearLayoutlinearLayout=newLinearLayout(getApplicationContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
scrollView.addView(linearLayout);
Solution 2:
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
:
publicclassListTestActivityextendsActivity {
LinearLayout layout;
ScrollView myScrollView;
TextView textview;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (LinearLayout) this.findViewById(R.id.linearLayout1);
myScrollView = newScrollView(this);
myScrollView.setBackgroundColor(0xfff00fff);
myScrollView.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
textview = newTextView(this);
textview.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
textview.setText("sadfasdfasdfasdfasdfasd fasdfsadfsadf");
myScrollView.addView(textview);
layout.addView(myScrollView);
}}
Post a Comment for "Android Scrollview Can't Be Created Programmatically."