Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to add some buttons in my TableRow, at runtime. My TableRow has this structure:

    <TableRow 
            android:layout_marginTop="100px" 
            android:gravity="bottom" 
            android:paddingTop="50px" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/riga1">

            <Button 
                android:textSize="32px" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:id="@+id/buttonOK2" 
                android:text="YES"
                android:onClick="setResult"/>
        </TableRow>

And my java code is:

TableRow tr = (TableRow) findViewById(R.id.riga1);
        Button b = new Button(this);  
        b.setText("button 1");  
        b.setId(1);
        b.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));   
        tr.addView(b);

        Button b1 = new Button(this);  
        b1.setText("button 2");  
        b1.setId(2);
        b1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));   
        tr.addView(b1);

But this seems not to work. In my activity I can see only the button defined in the xml file.

Another problem is that if I set in the related TableLayout the parameter:

android:stretchColumns="*" 

I must insert (in xml file) at least one button in the TableRow of the aforementioned TableLayout, otherwise I obtain a "division by zero error" near streatchColumns What can I do, in order to let the tableRow empty?

share|improve this question
    
Add several Buttons as u need and make it Visible only when u need at runtime, –  Viswanath L Apr 4 '13 at 12:01
1  
Thank you @Ariu, but I don't think this is a elegant solution –  Joseph82 Apr 4 '13 at 12:03

3 Answers 3

up vote 1 down vote accepted

I think that your problem is that you are using the wrong LayoutParams object for your TextViews try this:

 TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT); 

and then set this layoutParams object:

 b.setLayoutParams(layoutParams);
share|improve this answer
    
You are right @Emil. Now it works! Thank you! :) –  Joseph82 Apr 4 '13 at 12:10
    
You welcome, Have a nice coding : ) –  Emil Adz Apr 4 '13 at 12:11

Instead of table layout you should use relative layout it's work for me try it

In Your XML File Like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myMainRelativeLayout"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/btnAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="47dp"
    android:text="Add Button" />

And your java code like bellow

public class MainActivity extends Activity implements
    android.view.View.OnClickListener {

Button btnAdd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.btnAdd) {
        RelativeLayout rl = (RelativeLayout)findViewById(R.id.myMainRelativeLayout);
        Button btn = new Button(this);
        btn.setId(1234);
        btn.setText("Add Button Runtime");
        btn.setOnClickListener(this);
        rl.addView(btn);
    }
    if(v.getId() == 1234)
    {
        Toast.makeText(getApplicationContext(), "This is Your New Button", Toast.LENGTH_LONG).show();

    }
}

}

try this one it's really work

share|improve this answer
    
if my answer help you please give me vote –  Bhadresh Devani May 11 at 17:37

first you need to find the button using this statement

btn=(Button)findViewById(R.id.Button1);

than you can set the visibility using this:

btn.setVisibility(View.INVISIBLE);

Then you can set the Visibility using the statement:

btn.setVisibility(View.VISIBLE);

share|improve this answer
    
he's adding dynamically then how he will get id? –  Viswanath L Apr 4 '13 at 12:03
    
Of course @Ravi, I can't use the id! –  Joseph82 Apr 4 '13 at 12:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.