I would like to know if its possible to add a view say textview directly from the activity (Dynamically) in android without actually having it in the layout ?
Asked
Active
Viewed 388 times
1
-
View my earlier [answer](http://stackoverflow.com/questions/6930604/android-add-textview-to-layout-when-button-is-pressed/6932540#6932540),I hope it help. – kameny Nov 05 '11 at 15:54
1 Answers
4
Inside your Activity:
TextView tv = new TextView(this);
tv.setId(42);
LayoutParams params = new LayoutParams(width, height);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
tv.setLayoutParams(params);
setContentView(tv);
Where myView is the View you want to add a View to. It can be of any type you want.

Eric Nordvik
- 14,656
- 8
- 42
- 50
-
What if its a relative layout that i'm using ? Can i explicitly give the position where i want to add it using the id's like its done in the xml file ? – Saiesh Nov 05 '11 at 16:00
-
And in this case myview is a view that should already be given in the xml because unless and until u do that you wouldnt get an id for it right ? But i want to add a view without defining it in the xml . – Saiesh Nov 05 '11 at 16:14
-
1Try my latest edit to add a view programmatically without having it in xml. – Eric Nordvik Nov 05 '11 at 16:23