Add EditText(s) dynamically and retrieve values – Android

Adding EditText to your Android application is no different from adding any other form elements except for one thing. Retrieving values from them is slightly different and of course nothing impossible. Just a little more bit of coding and thats it.

Following code snippet creates a series of EditTexts and also let you to access its values.
Output of the following code snippet

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;

import java.util.ArrayList;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.FILL_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static android.widget.LinearLayout.VERTICAL;

public class Sample extends Activity {
    private List<EditText> editTextList = new ArrayList<EditText>();

    public void onCreate(Bundle savedInstanceState) {

        LinearLayout linearLayout = new LinearLayout(this);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);

        int count = 10;

    private Button submitButton() {
        Button button = new Button(this);
        return button;

    // Access the value of the EditText

    private View.OnClickListener submitListener = new View.OnClickListener() {
        public void onClick(View view) {
            StringBuilder stringBuilder = new StringBuilder();
            for (EditText editText : editTextList) {

    // Using a TableLayout as it provides you with a neat ordering structure

    private TableLayout tableLayout(int count) {
        TableLayout tableLayout = new TableLayout(this);
        int noOfRows = count / 5;
        for (int i = 0; i < noOfRows; i++) {
            int rowId = 5 * i;
        int individualCells = count % 5;
        tableLayout.addView(createLeftOverCells(individualCells, count));
        return tableLayout;

    private TableRow createLeftOverCells(int individualCells, int count) {
        TableRow tableRow = new TableRow(this);
        tableRow.setPadding(0, 10, 0, 0);
        int rowId = count - individualCells;
        for (int i = 1; i <= individualCells; i++) {
            tableRow.addView(editText(String.valueOf(rowId + i)));
        return tableRow;

    private TableRow createOneFullRow(int rowId) {
        TableRow tableRow = new TableRow(this);
        tableRow.setPadding(0, 10, 0, 0);
        for (int i = 1; i <= 5; i++) {
            tableRow.addView(editText(String.valueOf(rowId + i)));
        return tableRow;

    private EditText editText(String hint) {
        EditText editText = new EditText(this);
        return editText;
Share this:
  • Print
  • Digg
  • StumbleUpon
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Orkut
Bookmark the permalink.
  • Koyie

    Now we know who the sensblie one is here. Great post!

  • Chirag

    thanks for this code,it work for me.

  • Sathishnayar

    Really great, This was very helpfull buddy,,,

  • Banureaj

    Thankyou very much for this code, it work for me. .

    but how to make the EditText view as shown below

    thankyou very much, i really need it . 

  • OjSlinky


    Will the values saved only be a string value? If the user input some numbers, could the above code interpret it as number and do some math with it? 

    I learned a whole lot from this example. Very neat and efficient!

  • Ashwini

    how to fetch the value of edit text which is different activity in android….???

  • Nilaxan

    Now i’m designing a game interface for crossword in Android, so that i need to create 8×8 edittext , how can i do this please let me know

    • Murali

      Hi Nilaxim
      Can you please help how to design a crossword in android. For crossword building did you used java canvas or with the xml. If u help me in this it’s great appreciated

  • Heidi

    Hello Prasnna

    I am an Android Beginner. Could you please put comments in this code, it would be easier for me to understand. I’d be grateful.


  • Hari Prasad

    how to get data from edittext which is in listview