I want to create layout programmatically in different class which is not an activity so i will call one function from Activity and all code for creating layout is in function. so please give me some idea to write code.
this is my Activity:
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout tl = (TableLayout) findViewById(R.id.table1);
testing t1 = new testing();
Main m = new Main();
t1.makelayout(tl,m);
}
}
And this is my class which has function that is generating layout programmatically:
public class testing {
public void makelayout(TableLayout tl,Main m1) {
// TODO Auto-generated method stub
//Main m = new Main();
TextView tv= new TextView(m1);
tv.setText("hello1");
tl.addView(tv);
}
}
can i do this or not please help me.
If your
makelayout(TableLayout tl,Main m1)would bemakelayout(Context c, TableLayout tl)and you call it by giving the Activity’s context, then it would work.For such “external” things, you always need to give over the appropriate context to allow creating the Views in it.