1
votes

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?

3
Add several Buttons as u need and make it Visible only when u need at runtime, - Viswanath Lekshmanan
Thank you @Ariu, but I don't think this is a elegant solution - GVillani82

3 Answers

1
votes

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);
0
votes

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

-1
votes

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);