0

I'm adding table in linearlayout programmatically but it is not displaying on screen. Below is code -

public void displayTable(){
        TableLayout.LayoutParams lp2 = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
        TableLayout mTableLayout=new TableLayout(getActivity());

        TableRow mTableRow;
        LinearLayout mTableLinearLayout;
        TextView mSrNotxt,mDatetxt,mTimetxt;
        Date mDate=new Date();
        String mCDate=mDate.getDate()+"-"+(mDate.getMonth()+1)+"-"+(mDate.getYear()+1900)+"";

        mTableLayout.setLayoutParams(lp2);
        TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
        TableRow.LayoutParams tlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);

        for(int count=0;count<mTimeList.size();count++)
        {
            mTableRow=new TableRow(getActivity());
            mTableRow.setLayoutParams(lp);

            mSrNotxt=new TextView(getActivity());
            mDatetxt=new TextView(getActivity());
            mTimetxt=new TextView(getActivity());

            mSrNotxt.setTextColor(getResources().getColor(R.color.black));
            mDatetxt.setTextColor(getResources().getColor(R.color.black));
            mTimetxt.setTextColor(getResources().getColor(R.color.black));  

            mSrNotxt.setLayoutParams(tlp);
            mDatetxt.setLayoutParams(tlp);
            mTimetxt.setLayoutParams(tlp);

            mSrNotxt.setTextSize(12);
            mDatetxt.setTextSize(12);
            mTimetxt.setTextSize(12);

            if(count==0){
                mSrNotxt.setText("No.");
                mDatetxt.setText("Date");
                mTimetxt.setText("Time in Min.");
            }
            else{
                mSrNotxt.setText((count+1+""));
                mDatetxt.setText(mCDate);
                mTimetxt.setText(mTimeList.get(count));
            }

            mTableLinearLayout=new LinearLayout(getActivity());
            mTableLinearLayout.setLayoutParams(lp);
            mTableLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            mTableRow.addView(mTableLinearLayout);
            mTableLayout.addView(mTableRow);
        }
        System.out.println("table created");
        mChartLayout.addView(mTableLayout);
    }

xml view for chartLayout -

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >


            <LinearLayout
                android:id="@+id/chartView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            </LinearLayout>

    </ScrollView>

How can I display table?Is there any way?

Answer -

Need to add textview in view -

mTableLinearLayout.addView(mSrNotxt);
mTableLinearLayout.addView(mDatetxt);
mTableLinearLayout.addView(mTimetxt);

enter image description here

6
  • mChartLayout.requestLayout(); Commented Dec 10, 2014 at 11:09
  • @Suvitruf Not working. Commented Dec 10, 2014 at 11:12
  • show layout xml, and what mChartLayout is? Commented Dec 10, 2014 at 11:15
  • @Suvitruf I have added xml layout Commented Dec 10, 2014 at 11:19
  • @MikeM. Not getting what you want to say exactly Commented Dec 10, 2014 at 11:48

1 Answer 1

1

I am able to add Table using this code :

    TableLayout mTableLayout = new TableLayout(this);
    mTableLayout.setLayoutParams(new TableLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    for (int count = 0; count < 3; count++) {
        TableRow row = new TableRow(this);
        row.setLayoutParams((new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT)));

        TextView valueTV = new TextView(this);
        valueTV.setText("text : "+count);
        // valueTV.setId(5);
        valueTV.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        row.addView(valueTV);
        mTableLayout.addView(row);
    }
    mChartLayout.addView(mTableLayout);

Add other required properties

Hope it helps ツ

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

2 Comments

your code is working but i want 3 textview horizontally placed in table row
Show the desired output

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.