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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:01:18+00:00 2026-05-25T13:01:18+00:00

I’m trying to correlate how the Android XML looks versus creating it programmatically and

  • 0

I’m trying to correlate how the Android XML looks versus creating it programmatically and I’m failing miserably. I’ve read several references stating that the type for the LayoutParams for the View must be equal to those of the parent, but I’m just not grokking it.

Given the following XML, how would I re-create this programmatically?

<TableRow
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_gravity="center_vertical"
>
    <LinearLayout  
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_gravity="center_vertical"
        android:paddingTop="3dp"
        android:orientation="vertical"
    >
        <EditText 
            android:padding="2dp"
            android:layout_width="100dp"
            android:layout_height="35dp"
            android:layout_column="0"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:textSize="15dp"
            android:numeric="integer"
        />
    </LinearLayout>

    <LinearLayout 
        android:layout_height="35dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:padding="2dp"
        android:orientation="vertical"
        >

        <CheckBox android:text="Check1" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_weight="1"
            android:textSize="10sp" 
            android:background="@drawable/checkbox_background" 
            android:button="@drawable/checkbox" 
            android:textColor="@color/Gray"
        />
        <CheckBox android:text="Check2" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="10sp" 
            android:background="@drawable/checkbox_background" 
            android:button="@drawable/checkbox" 
            android:textColor="@color/Gray"
        />
    </LinearLayout>

Here’s what I’ve tried:

// Get the TableLayout
TableLayout tl = (TableLayout)findViewById(R.id.score_table);

LayoutParams lp;
TableRow.LayoutParams tp;

// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100);
tr.setLayoutParams(new TableRow.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

LinearLayout ll0_1 = new LinearLayout(this);
// If the layout params of the child should be of
// the type of the parent this should be 
// TableRow.LayoutParams?
tp = (TableRow.LayoutParams)tr.getLayoutParams();
tp.height = TableRow.LayoutParams.WRAP_CONTENT;
tp.width = TableRow.LayoutParams.FILL_PARENT;
tp.gravity = Gravity.CENTER_VERTICAL;
// TableRow.LayoutParams has no padding so this
// gets set on the LinearLayout?
ll0_1.setPadding(0,3,0,0);
ll0_1.setLayoutParams(tp);
tr.addView(ll0_1);

EditText et0 = new EditText(this);
et0.setId(100);
et0.setInputType(InputType.TYPE_CLASS_NUMBER);
// Same as above; LP set to type of parent
lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT, 1f);
lp.height = 35;
lp.width = 100;
// Same as above; the parent's type doesn't have 
// these attributes
et0.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
et0.setPadding(0, 3, 0, 0);
et0.setLayoutParams(lp);
ll0_1.addView(et0);

LinearLayout ll0_2 = new LinearLayout(this);
ll0_2.setOrientation(LinearLayout.VERTICAL);
tr.addView(ll0_2);

CheckBox cb0_1 = new CheckBox(this);
cb0_1.setTextSize(10f);
cb0_1.setTextColor(Color.GRAY);
cb0_1.setBackgroundResource(R.drawable.checkbox_background);
cb0_1.setButtonDrawable(R.drawable.checkbox);
cb0_1.setText("Nil");
ll0_2.addView(cb0_1);

CheckBox cb0_2 = new CheckBox(this);
cb0_2.setTextSize(10f);
cb0_2.setTextColor(Color.GRAY);
cb0_2.setBackgroundResource(R.drawable.checkbox_background);
cb0_2.setButtonDrawable(R.drawable.checkbox);
cb0_2.setText("10fer");
ll0_2.addView(cb0_2);

// Duplicate code removed for brevity
//...

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

There is no error, but it does not match the XML. Instead, it looks like this:
enter image description here

The first one is from the XML, the second one is my attempt at reproducing it programmatically.

  • 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-25T13:01:18+00:00Added an answer on May 25, 2026 at 1:01 pm

    LayoutInflater is the easiest way :

    public View myView(){
           View v;
           LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           v = inflater.inflate(R.layout.myview, null);
           TextView text1 = (TextView) v.findViewById(R.id.dolphinTitle);
           Button btn1 = (Button) v.findViewById(R.id.dolphinMinusButton);
           TextView text2 = (TextView) v.findViewById(R.id. dolphinValue);
           Button btn2 = (Button) v.findViewById(R.id. dolphinPlusButton);
    return v;
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I'm parsing an XML file, the creators of it stuck in a bunch social
I am trying to loop through a bunch of documents I have to put

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.