1

i wanted to add textview and edittext params by code. here is my code

private void draw_table() {
    // TODO Auto-generated method stub
    TableLayout ll = (TableLayout) findViewById(R.id.input_table_2);

    for (int i = 0; i < 2; i++) {

        TableRow row = new TableRow(this);
        TableRow.LayoutParams lp = new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT);
        row.setLayoutParams(lp);
        // setting textVIEW
        textview = new TextView(this);
        textview.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        //textview.setPadding(0, 0, 5, 10);
        //textview.setTextAppearance(this,
        //      android.R.style.TextAppearance_Medium);
        textview.setText("Hello"); 
        // setting editText
        edittext = new EditText(this);
        edittext.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        //edittext.setEms(10);
        //edittext.setHint("gpa");
        //edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
        row.addView(textview);
        row.addView(edittext);
        ll.addView(row, i);
    }
}

but nothing appear on the screen. whats wrong in this code ?thanks in advance :)

1 Answers1

2

Use TableRow.LayoutParams to set your widgets LayoutParams. Like this

textview.setLayoutParams(new TableRow.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

also see how add and delete rows to table layout in java programically

Community
  • 1
  • 1
freddieptf
  • 2,134
  • 2
  • 15
  • 16