1

I picked up Java earlier in the week, and I am currently at the last step of creating an app. I am trying to print data in a table created programmatically. The issue is, I can't seem to figure out how to add scrollview to this table. The table goes on for hungreds of rows, so I need scrollview in order to view it correctly. Everything I have tried crashes my app. The following is my create table function.

private void createTable ()
 { 
   DecimalFormat format = new DecimalFormat ("##.00");

   LinearLayout.LayoutParams linearContainerParams = 
    new LinearLayout.LayoutParams ( 
    ViewGroup.LayoutParams.MATCH_PARENT, 
    ViewGroup.LayoutParams.WRAP_CONTENT, 
    0.0f); 

    LinearLayout.LayoutParams linearWidgetParams = new LinearLayout.LayoutParams (
        ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.MATCH_PARENT, 
        1.0f); 

        TableLayout.LayoutParams tableContainerParams = 
        new TableLayout.LayoutParams ( 
        ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        0.0f); 

        TableLayout.LayoutParams tableWidgetParams = 
        new TableLayout.LayoutParams ( 
        ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.MATCH_PARENT, 
        1.0f); 

        TableRow.LayoutParams rowContainerParams = 
        new TableRow.LayoutParams ( 
        ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        0.0f); 

        TableRow.LayoutParams rowWidgetParams = 
        new TableRow.LayoutParams ( 
        ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.MATCH_PARENT, 
        1.0f); 

        mRoot = new LinearLayout (this); 
        mRoot.setOrientation (LinearLayout.VERTICAL); 
        mRoot.setBackgroundColor (Color.LTGRAY); 
        mRoot.setLayoutParams (linearContainerParams); 

        mTableLayout = new TableLayout (this); 
        mTableLayout.setOrientation (TableLayout.VERTICAL); 
        mTableLayout.setBackgroundColor (Color.BLUE); 
        mTableLayout.setLayoutParams (tableContainerParams); 
        mRoot.addView (mTableLayout); 

        mTableRow = new TableRow (this); 
        mTableRow.setOrientation (TableLayout.VERTICAL); 
        mTableRow.setBackgroundColor (Color.CYAN); 
        mTableRow.setLayoutParams (rowContainerParams); 
        mTableLayout.addView (mTableRow); 

        mTextView = new TextView (this); 
        mTextView.setText ("Total"); 
        mTextView.setTextColor (Color.RED); 
        mTextView.setGravity (Gravity.RIGHT); 
        mTextView.setLayoutParams (rowWidgetParams); 
        mTableRow.addView (mTextView); 

        mTextView = new TextView (this); 
        mTextView.setText ("Month");

        mTextView.setTextColor (Color.RED); 
        mTextView.setGravity (Gravity.RIGHT); 
        mTextView.setLayoutParams (rowWidgetParams); 
        mTableRow.addView (mTextView); 

        int i = 0;

        for (i = 0; i < mTotalPaymentCount; ++i)
        {
          TextView text = new TextView (this);
          text.setText ("" + (i + 1));
          row.addView (text);

          text.setTextColor (Color.RED); 
          text.setGravity (Gravity.RIGHT); 
          text.setLayoutParams (rowWidgetParams);

          mTableLayout.addView (row);
        }


        setContentView (mRoot);
}

Is there any possible way for me to get scrollview working at all? Thank you to any help.

2 Answers2

2

I think @FD_'s answer is right, however, if you still want to do it the way you proposed, the right way to do it is creating a ScrollView object and then adding the TableLayout via its addView() method.

ScrollView sv = new ScrollView(this);
sv.addView(yourTableLayout);
nKn
  • 13,691
  • 9
  • 45
  • 62
  • Because I have done so much with this app, I think I will stick to finishing it this way. Though, I will look into those tutorials. Is there a certain place I have to place sv.addView(tableLayout)? I'm getting a lot of crashes when I add it after I initialize my layout. –  Jan 12 '14 at 12:59
  • Sorry, I forgot to add the context to the constructor (although you probably seen that). What's the message of the crashes you're having? I don't know how are you doing it but when I was messing with dynamic scrollviews I followed this little tutorial and it worked like a charm: http://www.dreamincode.net/forums/topic/130521-android-part-iii-dynamic-layouts/ – nKn Jan 12 '14 at 13:05
  • What's the crash message? – nKn Jan 12 '14 at 13:17
  • Updated with log cat info. –  Jan 12 '14 at 13:24
  • I seem to have found the issue. I have two layouts here. A Linear layout and a Table layout. Is there a way to only apply scrollview to one of them? –  Jan 12 '14 at 13:29
  • Ok, one thing you should know: Each `ScrollView` must have at maximum one and just one direct child view. If you're adding that view and you're getting that exception, it means that somewhere you're adding more than one child to the `ScrollView`. Usually the best way to get rid ot this kind of errors is adding a `LinearLayout` as a child of the `ScrollView` and adding as much elements as you want to the `LinearLayout`, as it may have more than one view to host directly. See an example here: http://stackoverflow.com/questions/10316235/scrollview-can-host-only-one-direct-child-exception – nKn Jan 12 '14 at 13:30
  • Now I see that that makes a lot of sense...but after making a new ScrollView sv and then doing sv.addView (mRoot) it still crashes. I'm starting to think this is far from saving. –  Jan 12 '14 at 13:36
  • This approach is a bit more complicated than using the solution that @FD_ proposed, as it is more delicate, but I think these are the two options you can handle. I suggest reading the link I added before, and if you're getting more exceptions, try googleing them before posting, probably hundreds of users had the same issues you're experiencing and they're already solved in many questions of StackOverflow. – nKn Jan 12 '14 at 13:39
  • Will do. Thank you so much for the help and the links of reference. It really helps a lot. –  Jan 12 '14 at 13:43
  • Hope it helps! Patience and perseverance :-) – nKn Jan 12 '14 at 13:44
0

You should have a look at the ListView widget. ListViews were designed to be much more efficient with storage and work fluently even with hundreds of rows. ListViews instantiate their cells only when they are needed, while you are instantiating hundreds of views at once. Moreover, ListViews already have the whole scrolling functionality built in.

Here is a tutorial to get you started: http://www.vogella.com/tutorials/AndroidListView/article.html

FD_
  • 12,947
  • 4
  • 35
  • 62