0
      scrollview = (ScrollView)findViewById(R.id.detailedScrollView);


  for (Quotation quotation : object.quotes){

          TextView quote = new TextView(this);
          quote.setText(quotation.getQuote());
          quote.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
          scrollview.addView(quote);


      }

Let's say there are three quotes, then I want to have three textViews. However, the code above crashes my app. Any obvious mistakes? Here's the error I'm getting:

11-06 17:35:53.214: E/AndroidRuntime(1430): java.lang.IllegalStateException: ScrollView can host only one direct child
2
  • What's the logcat say? What's the error you're getting? Commented Nov 6, 2011 at 22:41
  • 11-06 17:35:53.214: E/AndroidRuntime(1430): java.lang.IllegalStateException: ScrollView can host only one direct child Ah scrollview can only have one child? I guess I need to put everything in a linearLayout then into the scrollview? Commented Nov 6, 2011 at 22:44

3 Answers 3

6

You can't add views directly inside a scrollview. A scrollview can only contain a single layout object. What you have to do is to add a linearlayout in your scrollview, then add the textview to the linearlayout

Sign up to request clarification or add additional context in comments.

Comments

3

Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container. Please more detail

With best regards, Psycho

Comments

0

You need to add a "LinearLayout" (or "RelativeLayout") inside ScrollView. Say you have the layout xml as follows:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <LinearLayout
    android:id="@+id/linearlayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"        
    >       
  </LinearLayout>
</ScrollView>

And now you want to add the 'TextView' programmatically, which is as follows:

LinearLayout linearLayout =(LinearLayout) this.findViewById(R.id.linearlayout1);
for (Quotation quotation : object.quotes){
     TextView quote = new TextView(this);
     quote.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     quote.setPadding(4, 0, 4, 0);    //left,top,right,bottom
     quote.setText(quotation.getQuote());          
     linearLayout.addView(quote);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.