Android EditText

EditText is a standard entry widget used in android apps for entering and modifying text input. EditText is a subclass of TextView with text editing operations.Generally EditText is used in our applications to provide an input or text field.

Android EditText Example


XML Layout for EditText Example

Drag an EditText and Button in your XML Layout by taking a RelativeLayout.

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="dzone.jaipur.myapp1.MainActivity" >

      <EditText
        android:id="@+id/MyEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#F2F2F2"
        android:hint="Enter Your Name"
        android:padding="15dp"
        android:textColorHint="#000"
        android:textStyle="bold|italic"
        android:layout_marginTop="100dp"/>

		<Button
        android:id="@+id/displayText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#000"
        android:padding="10dp"
        android:text="Display Text"
        android:textColor="#0f0"
        android:textStyle="bold"/>

 </RelativeLayout>

Now in your MainActivty Class write code to retrive the data from editText widget. We have a getText() method to extract input from EditText, but it gives us a object of editable class ,which should be converted in String.



public class MainActivity extends AppCompatActivity {
    
	EditText E1;
	Button B1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		 
   E1 = (EditText)findViewById(R.id.editText);
   B1 = (Button)findViewById(R.id.button);

   B1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                               {
                    Toast.makeText(MainActivity.this, E1.getText().toString(), Toast.LENGTH_LONG).show(); 
                }
            }
        });
}

}


Now Run the Application and give some input through keypad in EditText.Now After pressing the Button you will get the Toast Message which show your entered text.



Attributes of Android EditText

Id :


Id is an attribute used to uniquely identify any widget. View the code in which we set the id of a edit text.

<EditText
android:id="@+id/MyEditText" 
android:layout_height="wrap_content"
android:layout_width="match_parent"/>

Gravity:


The gravity attribute is an optional attribute which is used to control the alignment of the text like left, right, center, top, bottom, center_vertical, center_horizontal etc.

<EditText
    android:id="@+id/MyEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Enter Email"
    android:gravity="right"/> <!--gravity of a edit text-->

Text :

Text attribute is used to set the text in a EditText widget. We can set the text in xml Layout as well as in the java class (Activity) through code. Below is the example code in which we set the text "First Name" in a edit text.

<EditText
    android:id="@+id/MyEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="First Name"/> <!--set text in edit text-->

Hint:

Below example describe hint attribute which is used to set the hint i.e. what you want user to enter in this edit text. Whenever user start to type in edit text the hint will automatically disappear.So Hint is just like placeholder. Below is the example code with explanation in which we set the hint of a edit text.

<EditText
    android:id="@+id/MyEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:hint="Enter Your Name Here" /> <! !--display the hint-->

TextColor:

TextColor attribute is used to set the text color of a text view. Color value is in the form of "#argb", "#rgb", "#rrggbb", or "#aarrggbb".

<EditText
    android:id="@+id/MyEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Password"
    android:textColor="#f00"/><!--set the red text color-->

TextColorHint:

TextColorHint is an attribute used to set the color of displayed hint. Below is the example code with explanation included in which we set the green color for displayed hint of a edit text.

<EditText
    android:id="@+id/MyEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:hint="Enter Your Name Here"
    android:textColorHint="#0f0"/><!--set the hint color green-->

Background:

Background attribute is used to set the background of a edit text. We can set a color or a drawable in the background of a edit text. Below is the example code with explanation included in which we set the black color for the background, white color for the displayed hint and set 10dp padding from all the side's for edit text.

<EditText
    android:id="@+id/MyEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:hint="Enter Your Name Here"
    android:padding="15dp"
    android:textColorHint="#fff"
    android:textStyle="bold|italic"
    android:background="#000"/><!--set background color black-->

Padding:

Padding attribute is used to set the padding from left, right, top or bottom. In above example code of background we also set the 10dp padding from all the side�s of edit text.

< EditText
android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AbhiAndroid"
android:layout_centerInParent="true"
android:textSize="40sp" 
android:padding="10dp"
android:textColor="#fff"
android:background="#000"/> <--red color for background of text view-->

Why Us?

Address

  • 257,katewa nagar,gujjar ki thadi,new sanganer road, Jaipur, Jaipur
  • Phone:+919829708506
  • Email:[email protected]

Follow Us