Powered By Blogger

Monday 27 February 2012

Tabel layout through programming


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TableRow.LayoutParams;

import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class DynamicTL extends Activity {
    /** Called when the activity is first created. */
   
String s[]={"dheeraj","shivam","Ajeet","varun","arvind","indrabhan"};
TableLayout tl;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tl=(TableLayout)findViewById(R.id.tableLayout1);
        for(int i=0;i<s.length;i++)
        {
        TableRow tr=new TableRow(this);
        TextView tx1=new TextView(this);
        TextView tx2=new TextView(this);
        createView(tr,tx1,Integer.toString(i+1));
        createView(tr,tx2,s[i]);
        tl.addView(tr);
        }
    }
    public void createView(TableRow tr1,TextView t,String viewdata)
    {
    t.setText(viewdata);
    t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    t.setTextColor(Color.DKGRAY);
    t.setBackgroundColor(Color.CYAN);
    t.setPadding(20,0,0,0);
    tr1.setPadding(0,1,0,1);
    tr1.setBackgroundColor(Color.BLACK);
    tr1.addView(t);
    }
}

No comments:

Post a Comment