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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:25:17+00:00 2026-05-24T17:25:17+00:00

TableLayout relativeLayout = (TableLayout)findViewById(R.id.ll1); TableRow row =new TableRow(this); Button button ; int counter =

  • 0
TableLayout relativeLayout = (TableLayout)findViewById(R.id.ll1);
        TableRow row =new TableRow(this); 
        Button button ;
        int counter = 0;

        for(int i = 0;i<=13;i++){
                counter++;
                button = new Button(this);
                button.setText(counter);


                row.addView(button);

            }

        relativeLayout.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        relativeLayout.computeScroll();             

XML File

<TableLayout android:id="@+id/ll1"  android:shrinkColumns="true"
        android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button1"
        xmlns:android="http://schemas.android.com/apk/res/android"></TableLayout>

But whenever i execute my application it just move my buttons in a single row and after 5 buttons all the buttons move out from the screen. i dont’ want to add more rows as i don’t want to hard-code anything in my code like placing some code if (counter%5==0) then do this and that.

Is there any other way to calculate the width that if the buttons are equivalent to the width of screen then do this or that or any other way like any property in table Layout which simply wrap content of a row?

  • 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-24T17:25:17+00:00Added an answer on May 24, 2026 at 5:25 pm

    Yes there is a way to get the exact width of any component in your app. Look at my answer to my question asked a while ago here. As you can see, I instantiate a Paint object, but that’s not necessary unless you wish to get the width of the text itself. However you must use the density multiplier:

    float scale = getContext().getResources().getDisplayMetrics().density;
    

    That will convert any function that gets the width of a component (in this case, your button) to the actual pixel width on any given devices. So you would get the button width (via invoking the getWidth() function on your Button object) and then multiply by that scaling factor.

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

Sidebar

Related Questions

I want to add rows to a tablelayout dynamically. This is my code. TableRow
I have a tablelayout in Android like this: 1) Row == IMAGEVIEW | TEXTVIEW
Before I added my ListView, along with changing my TableLayout height to wrap_content as
I have a TableLayout with a tableRow and a horizontal scroll view in the
Okay, so I've got this TableLayout, and its full of data - with all
How to set tablelayout row background color programatically.
I have a TableLayout with 3 TableRows, each TableRow has 3 ImageViews. Each ImageView
I have a Layout (using TableLayout, etc...) which works well. It seems like this:
I'm displaying a TableLayout with rows as follows: <?xml version=1.0 encoding=utf-8?> <TableRow xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_parent
How can I change the padding of a TableLayout programmatic. I try this code,

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.