Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create EditText on button click in Android

    • 0
    • 0
    • 0
    • 0
    • 3
    • 0
    • 0
    • 0
    • 8.46k
    Comment on it

    This blog will help to create a custom edit text on button click.This tutorial includes a xml file which hold a button after click on button create a layout parmas for edit text. Each edit text has different id and every times when you click, a button create custom edit text with different ids.
    See the code below step by step and follow that to create a custom edit text .

    Step-1 Create a xml file res/custom_edit_text.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/buttonView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Create Edit Text" >
        </Button>
    </LinearLayout
    

    Step-2- Create a Activity src/CreateEditText.

    import android.app.Activity;
    import android.os.Bundle;
    import android.text.InputFilter;
    import android.text.InputType;
    import android.util.TypedValue;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;
    public class CreateEditText extends Activity {
        private Button buttonView;
        private LinearLayout parentLayout;
        private int hint=0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.custom_edittext);
            buttonView=(Button)findViewById(R.id.buttonView);
            parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
            buttonView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    createEditTextView();
                }
            });
        }
        protected void createEditTextView() {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams (
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.setMargins(0,10,0,10);
                EditText edittTxt = new EditText(this);
                int maxLength = 5;
                hint++;
                edittTxt.setHint("editText"+hint);
                edittTxt.setLayoutParams(params);
               // edtTxt.setBackgroundColor(Color.WHITE);
                edittTxt.setInputType(InputType.TYPE_CLASS_TEXT);
                edittTxt.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
                edittTxt.setId(hint);
                InputFilter[] fArray = new InputFilter[1];
                fArray[0] = new InputFilter.LengthFilter(maxLength);
                edittTxt.setFilters(fArray);
                parentLayout.addView(edittTxt);
        }
    }
    

 3 Comment(s)

  • Thank you sir for this tutorial, it works perfectly fine
    But i'd like to ask, 
    Is there a way i can add all the children of LinearLayout below the existing contents in a scrollView and they will appear just exactly the way i defined them in my xml 

    please check out my xml code below
    Thank you very much sir
    Expecting your kind response

     
     <ScrollView
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="481dp"
            android:layout_marginTop="8dp"
            android:paddingBottom="50dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.481"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView5">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="120dp"
                android:orientation="vertical"
                android:paddingBottom="40dp">
    
                <EditText
                    android:id="@+id/Type"
                    android:layout_width="match_parent"
                    android:layout_height="46dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="52dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp"
                    android:background="@drawable/edittext_bg"
                    android:ems="10"
                    android:hint="Enter type of Item"
                    android:inputType="text"
                    android:paddingStart="20dp"
                    android:paddingLeft="20dp"
                    android:paddingTop="10dp"
                    android:paddingEnd="10dp"
                    android:paddingRight="10dp"
                    android:paddingBottom="10dp"
                    android:textSize="14sp" />
    
                <EditText
                    android:id="@+id/ItemSize"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="24dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp"
                    android:background="@drawable/edittext_bg"
                    android:ems="10"
                    android:hint="Enter size of Item"
                    android:inputType="numberDecimal"
                    android:paddingStart="20dp"
                    android:paddingLeft="20dp"
                    android:paddingTop="10dp"
                    android:paddingEnd="10dp"
                    android:paddingRight="10dp"
                    android:paddingBottom="10dp"
                    android:textSize="14sp"/>
    
                <EditText
                    android:id="@+id/quantity"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="24dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp"
                    android:background="@drawable/edittext_bg"
                    android:ems="10"
                    android:hint="Enter Quantity of Item"
                    android:inputType="number"
                    android:paddingStart="20dp"
                    android:paddingLeft="20dp"
                    android:paddingTop="10dp"
                    android:paddingEnd="10dp"
                    android:paddingRight="10dp"
                    android:paddingBottom="10dp"
                    android:textSize="14sp"/>
    
                <EditText
                    android:id="@+id/priceOfItem"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="24dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp"
                    android:background="@drawable/edittext_bg"
                    android:ems="10"
                    android:hint="Enter Price of Item"
                    android:inputType="number"
                    android:paddingStart="20dp"
                    android:paddingLeft="20dp"
                    android:paddingTop="10dp"
                    android:paddingEnd="10dp"
                    android:paddingRight="10dp"
                    android:paddingBottom="10dp"
                    android:textSize="14sp"/>
    
                <EditText
                    android:id="@+id/discountedAmount"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="24dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp"
                    android:background="@drawable/edittext_bg"
                    android:ems="10"
                    android:hint="Enter The Discounted Amount"
                    android:inputType="number"
                    android:paddingStart="20dp"
                    android:paddingLeft="20dp"
                    android:paddingTop="10dp"
                    android:paddingEnd="10dp"
                    android:paddingRight="10dp"
                    android:paddingBottom="10dp"
                    android:textSize="14sp"/>
    
                <EditText
                    android:id="@+id/transportCost"
                    android:layout_width="match_parent"
                    android:layout_height="55dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="24dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginRight="16dp"
                    android:background="@drawable/edittext_bg"
                    android:ems="10"
                    android:hint="Enter the transportation cost"
                    android:inputType="number"
                    android:paddingStart="20dp"
                    android:paddingLeft="20dp"
                    android:paddingTop="10dp"
                    android:paddingEnd="10dp"
                    android:paddingRight="10dp"
                    android:paddingBottom="10dp"
                    android:textSize="14sp"/>
    
    
            </LinearLayout>
        </ScrollView>
    
        <Button
            android:id="@+id/addMore"
            android:layout_width="176dp"
            android:layout_height="43dp"
            android:layout_marginTop="28dp"
            android:layout_marginBottom="53dp"
            android:background="@drawable/curved_button"
            android:onClick="buttonAddMore"
            android:paddingStart="5dp"
            android:paddingLeft="2dp"
            android:paddingEnd="19dp"
            android:paddingRight="10dp"
            android:text="@string/Add More Items"
            android:textAllCaps="false"
            android:textColor="@android:color/background_light"
            android:textSize="18sp"
            android:typeface="serif"
            app:layout_constraintBottom_toTopOf="@+id/bottom_footer"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/scroll"
            app:layout_constraintVertical_bias="0.68" />
    
        <Button
            android:id="@+id/NextButton"
            android:layout_width="176dp"
            android:layout_height="43dp"
            android:layout_marginTop="28dp"
            android:layout_marginBottom="53dp"
            android:background="@drawable/clear_field"
            android:onClick="ProceedToNextPage"
            android:paddingStart="5dp"
            android:paddingLeft="2dp"
            android:paddingEnd="19dp"
            android:paddingRight="10dp"
            android:text="@string/Next Button"
            android:textAllCaps="false"
            android:textColor="@android:color/background_light"
            android:textSize="18sp"
            android:typeface="serif"
            app:layout_constraintBottom_toTopOf="@+id/bottom_footer"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/scroll"
            app:layout_constraintVertical_bias="0.68" />


     
  • Thanks for this sir, it works perfectly.
    But is there a way i can add all the children of LinearLayout below the existing one and they will appear just like the way i defined the default and i can still use all the inputs from both layouts to make calculations
    Thank you very much
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: