Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7543747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:30:28+00:00 2026-05-30T08:30:28+00:00

I am trying to develop a custom table through coding in Android. I am

  • 0

I am trying to develop a custom table through coding in Android. I am trying to create following layout

<LinearLayout>
    <Table>
        <TableRow> Title </TableRow>
        <TableRow> Header </TableRow>
    </Table>
    <ScrollView>
        <Table>
            <TableRow> Row1 </TableRow>
            <TableRow> Row2 </TableRow>
        </Table>
    </ScrollView>
</LinearLayout>

I saw few post here and this is very near to what I want but not fully.

The class declaration is

public class TableView extends TableLayout implements OnClickListener

Constructor

    public TableView(Activity context, String title, String[] headers,
        List<String[]> data) {
    super(context);
    _data = data;
    _title = title;
    _context = context;
    _headers = headers;
}

init code

        LinearLayout linearLayout = new LinearLayout(_context);
    // Create a new Table Layout
    TableLayout table = new TableLayout(_context); 
    
    // Set stretch and shrink for all the columns in the table
    table.setStretchAllColumns(true);  
    table.setShrinkAllColumns(false); 
    
    // Add the title row to the table  
    TableRow rowTitle = new TableRow(_context);  
    rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);  
    
    TableRow rowHeader = new TableRow(_context);
    
    // title column/row  
    TextView title = new TextView(_context);  
    title.setText("User Details");  
    
    // Set properties of title.
    title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);  
    title.setGravity(Gravity.CENTER);  
    title.setTypeface(Typeface.SERIF, Typeface.BOLD);  
    
    // Create a table layout params and add the param to span 6 columns
    TableRow.LayoutParams params = new TableRow.LayoutParams();  
    params.span = 6;  
    
    rowTitle.addView(title, params);
      
    // labels column  
    TextView headerLabel1 = new TextView(_context);  
    headerLabel1.setText("Company Name");  
    headerLabel1.setTypeface(Typeface.DEFAULT_BOLD);  
    
    rowHeader.addView(headerLabel1);
    
    TextView headerLabel2 = new TextView(_context);  
    headerLabel2.setText("User Type");  
    headerLabel2.setTypeface(Typeface.DEFAULT_BOLD);  
    
    rowHeader.addView(headerLabel2);

    TextView headerLabel3 = new TextView(_context);  
    headerLabel3.setText("Active");  
    headerLabel3.setTypeface(Typeface.DEFAULT_BOLD);  
    
    rowHeader.addView(headerLabel3);

    TextView headerLabel4 = new TextView(_context);  
    headerLabel4.setText("Mobile #");  
    headerLabel4.setTypeface(Typeface.DEFAULT_BOLD);  
    
    rowHeader.addView(headerLabel4);

    TextView headerLabel5 = new TextView(_context);  
    headerLabel5.setText("Login Id");  
    headerLabel5.setTypeface(Typeface.DEFAULT_BOLD);  
    
    rowHeader.addView(headerLabel5);

    TextView headerLabel6 = new TextView(_context);  
    headerLabel6.setText("TimeZone");  
    headerLabel6.setTypeface(Typeface.DEFAULT_BOLD);  
    
    rowHeader.addView(headerLabel6);
    
    table.addView(rowTitle);  
    table.addView(rowHeader);
    
    linearLayout.addView(table);

    // Add Data to row
    ScrollView dataScroll = new ScrollView(_context);
    LayoutParams dataParams = LP_WC_WC;
    dataParams.height = LayoutParams.FILL_PARENT;
    dataParams.width = LayoutParams.FILL_PARENT;
    dataScroll.setLayoutParams(dataParams);
    TableLayout rowTable = new TableLayout(_context);
    rowTable.setLayoutParams(LP_FP_WC);
    
    TableRow row = new TableRow(_context);;
    
    TextView dataText1 = new TextView(_context);  
    dataText1.setText("IBM");  
    dataText1.setTypeface(Typeface.DEFAULT_BOLD);  
    
    row.addView(dataText1);
    
    TextView dataText2 = new TextView(_context);  
    dataText2.setText("Administrator");  
    dataText2.setTypeface(Typeface.DEFAULT_BOLD);  
    
    row.addView(dataText2);
    
    TextView dataText3 = new TextView(_context);  
    dataText3.setText("Yes");  
    dataText3.setTypeface(Typeface.DEFAULT_BOLD);  
    
    row.addView(dataText3);
    
    TextView dataText4 = new TextView(_context);  
    dataText4.setText("123-123-1234");  
    dataText4.setTypeface(Typeface.DEFAULT_BOLD);  
    
    row.addView(dataText4);
    
    TextView dataText5 = new TextView(_context);  
    dataText5.setText("testcompany");  
    dataText5.setTypeface(Typeface.DEFAULT_BOLD);  
    
    row.addView(dataText5);
    
    TextView dataText6 = new TextView(_context);  
    dataText6.setText("UTC+5:30");  
    dataText6.setTypeface(Typeface.DEFAULT_BOLD);  
    
    row.addView(dataText6);
    
    rowTable.addView(row);
    dataScroll.addView(rowTable);
    
    linearLayout.addView(dataScroll);
    
    _context.setContentView(linearLayout);  

On executing this code I am seeing only the title and header bars not the contents. What I am missing?

Any pointer or link would help me.

@Edit : For people struct with same problem here is the solution that worked for me.

XML file which I created:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="true"
    android:stretchColumns="true" >

    <TableRow
        android:id="@+id/headerRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/dataRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <HorizontalScrollView
                    android:id="@+id/horizontalScrollView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:id="@+id/linearLayout2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal" >

                        <TableLayout
                            android:id="@+id/dataTableLayout"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent" >

                            <TableRow
                                android:id="@+id/actualDataRow"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" >

                                <TextView
                                    android:id="@+id/textView1"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="Large Text"
                                    android:textAppearance="?android:attr/textAppearanceLarge" />
                            </TableRow>
                        </TableLayout>
                    </LinearLayout>
                </HorizontalScrollView>
            </LinearLayout>
        </ScrollView>
    </TableRow>

    <TableRow
        android:id="@+id/footerRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>
</TableLayout>

</LinearLayout>

The main activity which I had used to call the sub activity. In the main activity window I have added only a button which calls my custom component. The code of my main activity is

import in.ns.views.CustomView;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout.LayoutParams;

public class CustomComponentActivity extends Activity implements OnClickListener {
Button viewButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    viewButton = (Button) findViewById(R.id.button1);
    viewButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    CustomView cv = new CustomView(this);
    LayoutParams LP_FP_FP = new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    //this.addView(cv);
    //this.addContentView(cv.getView(), LP_FP_FP);
    setContentView(cv.getView());
    Log.d("custom", "added view");
}
}

The class which is creating my custom component.

import in.ns.custom.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class CustomView extends LinearLayout {

private Context _context;
private LayoutInflater _inflater;
private View _view;

public CustomView(Context context) {
    super(context);
    _context = context;
    _inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    _view = _inflater.inflate(R.layout.custom, this);
    init();
}

public View getView() {
    return _view;
}

private void init() {
    TableLayout tableLayout = (TableLayout) _view.findViewById(R.id.dataTableLayout);
    TableRow tableRow ; //(TableRow) tableLayout.getChildAt(0);
    
    tableLayout.removeAllViews();
    
    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;
    TextView tv6;
    for(int i = 0; i < 32; i++) {
        tableRow = new TableRow(_context);
        
        tv1 = new TextView(_context);
        tv1.setText("Success1 ");
        
        tableRow.addView(tv1);
        
        tv2 = new TextView(_context);
        tv2.setText("Success2 ");

        tableRow.addView(tv2);
        
        tv3 = new TextView(_context);
        tv3.setText("Success1 ");
        
        tableRow.addView(tv3);
        
        tv4 = new TextView(_context);
        tv4.setText("Success2 ");

        tableRow.addView(tv4);
        
        tv5 = new TextView(_context);
        tv5.setText("Success1 ");
        
        tableRow.addView(tv5);
        
        tv6 = new TextView(_context);
        tv6.setText("Success2 ");

        tableRow.addView(tv6);
        
        tableLayout.addView(tableRow);          
    }
    
    tableRow = new TableRow(_context);
    
    TextView tv11 = new TextView(_context);
    tv11.setText("Success11 ");
    
    tableRow.addView(tv11);
    
    TextView tv21 = new TextView(_context);
    tv21.setText("Success21 ");
    tableRow.addView(tv21);
    
    tableLayout.addView(tableRow);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:30:30+00:00Added an answer on May 30, 2026 at 8:30 am

    Instead of creating the views from the code, you could have used an XML and inflate it after calling the constructor.

    There are a few mistakes in your code:

    • _context.setContentView(linearLayout);

    That’s not the correct way of doing it. You shouldn’t pass an Activity around.
    Construct the view from the Activity and set the content view from it.

    • You are never adding a child to the instance.

    There is no this.addChild() anywhere. Everything is created is not added to TableLayout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom search result webpart, and I need to develop
I'm trying to develop a custom control for asp.net that will have the following
I'm trying to develop a firefox extension that inserts additional HTTP header fields into
I am trying to develop custom control in JSF 1.2 (using facelets). I followed
I am trying develop an Android app which uses Google maps. So for the
I'am trying to develop a Custom drupal module which will read arguments from the
I am trying to develop a custom component to act as a divider. <?xml
I am trying to develop a custom activity for my sharepoint workflow that would
I am trying to develop some general purpose custom ValidationAttributes. The fact that one
I am trying to develop a custom widget for camera preview. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.