1

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 ?

1
  • View my earlier answer,I hope it help. Commented Nov 5, 2011 at 15:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 ?
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 .
Try my latest edit to add a view programmatically without having it in xml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.