16

I'm developing Contact Application, which adds Email address, phone number. I have to create edit text dynamically in code itself. I don't know how and where to implement this logic, suggest any help would be grateful.

Cœur
  • 37,241
  • 25
  • 195
  • 267
RajeshVijayakumar
  • 10,281
  • 11
  • 57
  • 84

5 Answers5

28

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.

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
10

Use below code for Add Edittext Programatically, it will solve your problem.

RelativeLayout mRlayout = (RelativeLayout) findViewById(R.id.mRlayout);
RelativeLayout.LayoutParams mRparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
EditText myEditText = new EditText(context);
myEditText.setLayoutParams(mRparams);
mRlayout.addView(myEditText);
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
2

Layout

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:gravity="center_horizontal" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Contact Application"
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:gravity="center_horizontal"/>

code

//container Layout
    TableLayout tbl=(TableLayout)findViewById(R.id.TableLayout1);
    //table row
    TableRow tr = new TableRow(this);
    TableLayout.LayoutParams tableRowParams=
            new TableLayout.LayoutParams
            (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
    //for set margin
    tableRowParams.setMargins(0, 10, 0, 0);
    tr.setLayoutParams(tableRowParams);
    //text view
    TextView tv=new TextView(this);
    tv.setText("Email");
    tv.setGravity(Gravity.CENTER);
    tv.setTextColor(Color.parseColor("#0070C0"));
    tv.setTextSize(26);
    tv.setLayoutParams(new TableRow.LayoutParams(100, TableRow.LayoutParams.WRAP_CONTENT));
    //add textview
    tr.addView(tv);
    //set layout params of edittext
    TableRow.LayoutParams etParams=
            new TableRow.LayoutParams
            (120,30);
    etParams.setMargins(10, 0, 0, 0);

    EditText et=new EditText(this);
    et.setLayoutParams(etParams);
    //set background
    et.setBackgroundResource(R.drawable.bg_grey);
    et.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
    tr.addView(et);

    tbl.addView(tr, tableRowParams);
Sonia John Kavery
  • 2,099
  • 2
  • 20
  • 36
0

EditText within the LinearLayout.

Try this.

LinearLayout linearLayout = new LinearLayout(getContext());
                                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                linearLayout.setOrientation(LinearLayout.VERTICAL);
                                layoutParams.setMargins(2, 1, 2, 1);
                                linearLayout.setLayoutParams(layoutParams);

EditText editText = new EditText (getContext());
                                    editText .setTextSize(15);
                                    editText .setLayoutParams(layoutParams);
                                    linearLayout.setGravity(Gravity.CENTER);
                                    editText .setText(Html.fromHtml(directiveMessage));
                                    linearLayout.addView(editText );
Gayathri
  • 249
  • 3
  • 13
0
EditText emailEditText = new EditText(this); //you can pass context based on your function.
emailEditText.setHint("Enter Email address:"); //works same as in XML file hint
emailEditText.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);  
yourLayout.addView(emailEditText);

set your Layout & LayoutParams based on your requirements.

setContentView(your layout, your layout params)

Same for the numberEditText except change input type to TYPE_CLASS_NUMBER.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Jithendra
  • 11
  • 1