Generating Edit Text Programmatically in Android

You can create it like so:

EditText myEditText = new EditText(context); // Pass it an Activity or Context
myEditText.setLayoutParams(new LayoutParams(..., ...)); // Pass two args; must be LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, or an integer pixel value.
myLayout.addView(myEditText);

This can be implemented anywhere on the UI thread; a click listener, an onCreate method, and everything in between.

There is a more generic example in this question, and a good rundown of these processes in this blog.

Leave a Comment