Pages

Saturday, September 8, 2012

Android Table Layout Example Code Without Xml


package example.withoutxml;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;

public class LayoutsExample extends Activity  {


Button b1,b2,b3,b4;
EditText et1,et2,et3;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);


TableLayout tl=new TableLayout(this);


//    tl.setShrinkAllColumns(true);

tl.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                                            TableRow.LayoutParams.WRAP_CONTENT));
tl.setStretchAllColumns(true);
tl.setBackgroundResource(R.drawable.day);

//TableRow tr=new TableRow(this);

// tr.addView(b1);
// tl.addView(tr);


TableRow tr1=new TableRow(this);
//tr1.addView(b1);
//tr1.setBackgroundResource(R.drawable.bunch);

// TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
//        TableRow.LayoutParams.WRAP_CONTENT);


et1=new EditText(this);
et1.setHeight(50);
et1.setWidth(50);
et1.setText("first row text box");

b1=new Button(this);
b1.setHeight(50);
b1.setWidth(50);
b1.setText("first row button");

//  b1.setLayoutParams(params);
tr1.addView(et1);  
tr1.addView(b1);
   tl.addView(tr1);
  // tr1.addView(et1,params);
//tr1.setGravity(Gravity.CENTER_HORIZONTAL);

TableRow tr2=new TableRow(this);
//tr2.setBackgroundResource(R.drawable.day);
// TableRow.LayoutParams params1 = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
//       TableRow.LayoutParams.WRAP_CONTENT);
// b2.setLayoutParams(params1);
et2=new EditText(this);
et2.setHeight(50);
et2.setWidth(50);
et2.setText("second row text box");

b2=new Button(this);
b2.setHeight(50);
b2.setWidth(50);
b2.setText("second row button");
b4=new Button(this);
b4.setHeight(50);
b4.setWidth(50);
b4.setText("second row second button");
tr2.addView(b4);
tr2.addView(et2);
tr2.addView(b2);

tl.addView(tr2);




TableRow tr3=new TableRow(this);
//tr3.setBackgroundResource(R.drawable.day);
// TableRow.LayoutParams params2 = new TableRow.LayoutParams
//                               (TableRow.LayoutParams.MATCH_PARENT,
//                              TableRow. LayoutParams.WRAP_CONTENT);
// b2.setLayoutParams(params2);

et3=new EditText(this);
et3.setHeight(50);
et3.setWidth(50);
et3.setText("third row text box");

b3=new Button(this);
b3.setHeight(50);
b3.setWidth(50);
b3.setText("third row button");
b3.setGravity(android.view.Gravity.RIGHT);
b3.setOnClickListener(this);
tr3.addView(et3);
tr3.addView(b3);

tl.addView(tr3);

setContentView(tl);
}


}

1 comment: