user3040968
user3040968

Reputation:

ScrollView Programmatically

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.

Upvotes: 1

Views: 419

Answers (2)

nKn
nKn

Reputation: 13761

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);

Upvotes: 2

FD_
FD_

Reputation: 12919

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

Upvotes: 0

Related Questions