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

  • SEARCH
  • Home
  • 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 7928931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:53:54+00:00 2026-06-03T19:53:54+00:00

I used the code below to create a TableRow with content dynamically. It works

  • 0

I used the code below to create a TableRow with content dynamically. It works good but I wish to get the values in the TableRow. Here is the sample code (I got it from Google), it has two text values in the TableRow. When I click the TableRow at any position it gives the corresponding value in the TableRow (I wish something similar to a ListView).

All_CustomsActivity.java

public class All_CustomsActivity extends Activity {

    String companies[] = { "Google", "Windows", "iPhone", "Nokia", "Samsung",
            "Google", "Windows", "iPhone", "Nokia", "Samsung", "Google",
            "Windows", "iPhone", "Nokia", "Samsung" };
    String os[] = { "Android", "Mango", "iOS", "Symbian", "Bada", "Android",
            "Mango", "iOS", "Symbian", "Bada", "Android", "Mango", "iOS",
            "Symbian", "Bada" };

    TableLayout tl;
    TableRow tr;
    TableRow mTable = null;
    TextView companyTV, valueTV;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tl = (TableLayout) findViewById(R.id.maintable);
        // addHeaders();
        addData();

    }

    /** This function add the headers to the table **/
    public void addHeaders() {

        /** Create a TableRow dynamically **/
        tr = new TableRow(this);
        tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        /** Creating a TextView to add to the row **/
        TextView companyTV = new TextView(this);
        companyTV.setText("Companies");
        companyTV.setTextColor(Color.GRAY);
        companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        companyTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        companyTV.setPadding(5, 5, 5, 0);
        tr.addView(companyTV); // Adding textView to tablerow.

        /** Creating another textview **/
        TextView valueTV = new TextView(this);
        valueTV.setText("Operating Systems");
        valueTV.setTextColor(Color.GRAY);
        valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        valueTV.setPadding(5, 5, 5, 0);
        valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tr.addView(valueTV); // Adding textView to tablerow.

        // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        // we are adding two textviews for the divider because we have two
        // columns
        tr = new TableRow(this);
        tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        /** Creating another textview **/
        TextView divider = new TextView(this);
        divider.setText("-----------------");
        divider.setTextColor(Color.GREEN);
        divider.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        divider.setPadding(5, 0, 0, 0);
        divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tr.addView(divider); // Adding textView to tablerow.

        TextView divider2 = new TextView(this);
        divider2.setText("-------------------------");
        divider2.setTextColor(Color.GREEN);
        divider2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        divider2.setPadding(5, 0, 0, 0);
        divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tr.addView(divider2); // Adding textView to tablerow.

        // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }

    /** This function add the data to the table **/
    public void addData() {

        for (int i = 0; i < companies.length; i++) {
            /** Create a TableRow dynamically **/
            tr = new TableRow(this);
            tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            // ImageView im = new ImageView(this);
            // im.setBackgroundResource(R.drawable.sample_image);
            // im.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            // LayoutParams.WRAP_CONTENT));
            // tr.addView(im);
            /** Creating a TextView to add to the row **/
            companyTV = new TextView(this);
            companyTV.setText(companies[i]);
            companyTV.setTextColor(Color.RED);
            companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            companyTV.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            companyTV.setPadding(5, 5, 5, 5);
            tr.addView(companyTV); // Adding textView to tablerow.

            /** Creating another textview **/
            valueTV = new TextView(this);
            valueTV.setText(os[i]);
            valueTV.setTextColor(Color.GREEN);
            valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            valueTV.setPadding(5, 5, 5, 5);
            valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            tr.addView(valueTV); // Adding textView to tablerow.

            // Add the TableRow to the TableLayout
            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            // tr.setOnClickListener(new View.OnClickListener() {
            // public void onClick(View view) {
            // view.setBackgroundColor(Color.DKGRAY);
            // }
            // });
            //
            // tr.setOnLongClickListener(new View.OnLongClickListener() {
            // public boolean onLongClick(View v) {
            // mTable = (TableRow) v; // assign selected TableRow gobally
            // openContextMenu(v);
            // return true;
            // }
            // });

        }
    }

    // @Override
    // public void onCreateContextMenu(ContextMenu menu, View v,
    // ContextMenu.ContextMenuInfo menuInfo) {
    // super.onCreateContextMenu(menu, v, menuInfo);
    // menu.add(0, v.getId(), 0, "Do YourStuff");
    //
    // }
    //
    // @Override
    // public boolean onContextItemSelected(MenuItem item) {
    // int ccount = (mTable).getChildCount();
    // String[] str = new String[ccount];
    // for (int i = 0; i < ccount; i++) {
    // TextView tv = (TextView) (((TableRow) mTable)).getChildAt(i);
    // str[i] = tv.getText().toString(); // set selected text data into the
    // // String array
    // }
    // Toast.makeText(All_CustomsActivity.this, Arrays.toString(str), 2)
    // .show();
    // return true;
    // }

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <TableLayout
            android:id="@+id/maintable"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="0,1" >
        </TableLayout>
    </ScrollView>

</LinearLayout>

In the code above some lines are commented, these lines are what I already tried to get the row values but it failed. Can anyone help me with this?

  • 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-06-03T19:53:55+00:00Added an answer on June 3, 2026 at 7:53 pm

    I guess you’re talking about getting those values on a TableRow click. If this is the case you could add a listener to your TableRow and use getChildAt to get a hold of the two TextViews and get the data:

    //...
    tr.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
          TableRow t = (TableRow) view;
          TextView firstTextView = (TextView) t.getChildAt(0);
          TextView secondTextView = (TextView) t.getChildAt(1);
          String firstText = firstTextView.getText().toString();
          String secondText = secondTextView.getText().toString();
       }
    });
    //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used the code below to dynamically create a group of radio buttons: self.wPaymantType.qgbSomeSelectionGroup
I have used the code below to read rfid tag values. try { if
I had used the below code to get the row index of the UIPickerView
My below code works fine and is used to populate a <select> item with
I have used the code below to create a button on the nav bar
I am using below code to create three tab in activity but it gives
I used the code below to create a graphic using dot (graphviz). digraph {
i used the code below to show the game center but if I rotate
Code below is used to create post data from jqGrid colmondel and post it.
I want to load wordpress in a smarty template. I used the code below

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.