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 5983089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:10:49+00:00 2026-05-22T22:10:49+00:00

I have a TableLayout with dynamically added TableRows depending on the size of a

  • 0

I have a TableLayout with dynamically added TableRows depending on the size of a cursor.
To each row I add a Delete-button, which is set to Invisible. Each row and button gets an id on creation that is the same if then belong to the same row.
WHen the user LongClicks on the TableRow the visibility is set to Visible instead and the Delete-button appears.

In the OnLongClick listener I check if the button Id is the same as the view (Here TableRow) that was clicked on.

        rel.addView(btnDelete);
            tr.addView(rel);


        tr.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int tableRowId = view.getId();
                 if(tableRowId == btnDelete.getId()){
                    btnDelete.setVisibility(View.INVISIBLE);
                    }
                    return true;
                }
            });

But it only seems to be true for the button on the bottom lowest button. So then it seems like the btnId is stored as the last btnId added, since the only TableRow it is true for is the last one. But when i use .setText(btnId + tableRowId) I can see they have the same id all the way down.

I tried to use Tag as well, but I stumble on the same problem.

Thanks alot!

Edit:

btnDelete = new Button(this);
btnDelete.setId(revolutionCounter);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
                        RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_VERTICAL);

btnDelete.setLayoutParams(params);
rel.addView(btnDelete);//a relativelayout inside the tablerow
tr.addView(rel);

Then I add the tr (tablerow) to the tablelayout. So I guess I am reusing the buttons? How should I go about it creating new ones? I thought that was what I was doing with the new Button(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-05-22T22:10:50+00:00Added an answer on May 22, 2026 at 10:10 pm

    Several solutions

    • Instead of capturing the longclick on the table row, capture it directly on the delete button by doing

    btndelete.setDuplicateParentStateEnabled(true)

    Button.setDuplicateParentStateEnabled(boolean)

    Add a LongClickListener on btnDelete and set the visibility to Visible/invisible, toggle it or whatever you want to do. If you need to access the table row inside the longclickListener, call

    TableRow tableRow = (TableRow) view.getParent();

    • If you want to keep the longclicklistener on the TableRow, you can access its btnDelete child this way:

      Button btnDelete = view.findViewById(view.getId());

    PS. Not sure if this is a problem, but you are still destroying previous delete buttons. Instead of

    btnDelete = new Button(this);
    

    you need to redeclare it

    Button btnDelete = new Button(this);
    

    Maybe this isn’t important and probably each row stores the value of the button object (otherwise it wouldn’t even display), but in other situations this has given me some headaches. In this case maybe it’s okay to reuse the variable declaration.

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

Sidebar

Related Questions

I have a layout that contains a TableLayout with three rows. Each row has
I have a TableLayout with 3 TableRows, each TableRow has 3 ImageViews. Each ImageView
I have the following problem spanning dynamically added rows to a TableLayout inside a
I have a tablelayout panel with multiple columns. I have added another 2 tablelayout
I have a working dynamic TableLayout that holds user data. Clicking an Add Event
I have list of products which have EditText in TableLayout . I want Keyboard
I have a List of TableLayout's that are each generated from data in my
I've got a problem. I wish to add rows dynamically to a TableLayout .
I have a TableLayout with just a single row (i.e. the Header Row) in
I have a TableLayout in the xml that has a row. I am adding

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.