I tried the following code for table layout in android without using xml file. But i didn’t get my screen on android emulator but getting the errror as “the application has stopped upexpectedly.Please try again.”
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Tablelayout extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LayoutParams params=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(params);
tv.setTextColor(Color.RED);
TableRow tr=new TableRow(this);
tr.addView(tv);
TableLayout tl=new TableLayout(this);
TableLayout.LayoutParams layoutparams=new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tl.addView(tr);
this.addContentView(tl, layoutparams);
}
}
I’m not getting a force close when I run this code, but you are missing something.
I tried adding the following, but did not see any text:
The problem is that when you call to tr.addView, you aren’t setting any layout parameters on the new row. If you change the line to the following, the text appears:
Edit: Here’s a thought on the force close, is Tablelayout correctly defined in your AndroidManifest.xml? You should have something like this.