{"id":762,"date":"2016-02-01T08:47:49","date_gmt":"2016-02-01T08:47:49","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=762"},"modified":"2019-06-14T10:45:31","modified_gmt":"2019-06-14T10:45:31","slug":"edittext","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/edittext","title":{"rendered":"EditText Tutorial With Example In Android Studio: Input Field"},"content":{"rendered":"<p>In Android, EditText is a standard entry widget in android apps. It is an overlay over TextView that configures itself to be editable. EditText is a subclass of TextView with text editing operations. <strong>We often use EditText in our applications in order to provide an input or text field, especially in forms.<\/strong> The most simple example of EditText is Login or Sign-in form.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-781\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/EditText-Input-Field-Example-in-Android.jpg\" alt=\"EditText Input Field Example in Android\" width=\"312\" height=\"270\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/EditText-Input-Field-Example-in-Android.jpg 312w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/EditText-Input-Field-Example-in-Android-300x260.jpg 300w\" sizes=\"auto, (max-width: 312px) 100vw, 312px\" \/><\/center>Text Fields in Android Studio are basically EditText:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-782\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/EditText-in-Android-Studio.jpg\" alt=\"EditText in Android Studio\" width=\"410\" height=\"259\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/EditText-in-Android-Studio.jpg 410w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/EditText-in-Android-Studio-300x190.jpg 300w\" sizes=\"auto, (max-width: 410px) 100vw, 410px\" \/><\/center><span style=\"color: #ff0000;\"><strong>Important Note: <\/strong><\/span>An\u00a0EditText\u00a0is simply a thin extension of a\u00a0<a href=\"\/ui\/textview\">TextView<\/a>. An EditText inherits all the properties of a TextView.<\/p>\n<hr \/>\n<h4><strong>EditText Code:<\/strong><\/h4>\n<p>We can create a EditText\u00a0instance by declaring it inside a layout(XML file) or by instantiating it programmatically (i.e. in Java Class).<\/p>\n<p><strong>EditText code in XML:<\/strong><\/p>\n<pre>&lt;EditText\r\nandroid:id=\"@+id\/simpleEditText\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_width=\"match_parent\"\/&gt;\r\n<\/pre>\n<p><strong>Retrieving \/ Getting the Value From EditText In Java Class:<\/strong><\/p>\n<p>Below is the example code of EditText in which we retrieve the value from a EditText in Java class. We have used this code in the example you will find at the end of this post.<\/p>\n<pre>EditText simpleEditText = (EditText) findViewById(R.id.simpleEditText);\r\nString editTextValue = simpleEditText.getText().toString();\r\n<\/pre>\n<hr \/>\n<h4><strong>Attributes of EditText:<\/strong><\/h4>\n<p>Now let\u2019s \u00a0we discuss few attributes that helps us to configure a EditText in your xml.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. id:<\/strong><\/span> id is an attribute used to uniquely identify a text EditText. Below is the example code in which we set the id of a edit text.<\/p>\n<pre>&lt;EditText\r\nandroid:id=\"@+id\/simpleEditText\" \r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_width=\"match_parent\"\/&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. gravity: <\/strong><\/span>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.<\/p>\n<p>Below is the example code with explanation included in which we set the right gravity for text of a EditText.<\/p>\n<pre>&lt;EditText\r\n    android:id=\"@+id\/simpleEditText\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Enter Email\"\r\n    android:gravity=\"right\"\/&gt;&lt;!--gravity of a edit text--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-765\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/gravity-in-EditText-Android.jpg\" alt=\"gravity in EditText Android\" width=\"357\" height=\"250\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/gravity-in-EditText-Android.jpg 357w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/gravity-in-EditText-Android-300x210.jpg 300w\" sizes=\"auto, (max-width: 357px) 100vw, 357px\" \/><\/center><span style=\"color: #008000;\"><strong>3. text:<\/strong><\/span> text attribute is used to set the text in a EditText. We can set the text in xml as well as in the java class.<\/p>\n<p>Below is the example code in which we set the text \u201cUsername\u201d in a edit text.<\/p>\n<pre>&lt;EditText\r\n    android:id=\"@+id\/simpleEditText\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:layout_centerInParent=\"true\"\r\n    android:text=\"Username\"\/&gt;&lt;!--set text in edit text--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-766\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/text-in-EditText-Android.jpg\" alt=\"text in EditText Android\" width=\"365\" height=\"430\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/text-in-EditText-Android.jpg 365w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/text-in-EditText-Android-255x300.jpg 255w\" sizes=\"auto, (max-width: 365px) 100vw, 365px\" \/><\/center><strong>Setting text in EditText In Java class:<\/strong><\/p>\n<p>Below is the example code in which we set the text in a\u00a0 text view programmatically means in java class.<\/p>\n<pre>EditText editText = (EditText)findViewById(R.id.simpleEditText);\r\neditText.setText(\"Username\");\/\/set the text in edit text<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. hint:<\/strong><\/span> hint is an attribute 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.<\/p>\n<p>Below is the example code with explanation in which we set the hint of a edit text.<\/p>\n<pre>&lt;EditText\r\n    android:id=\"@+id\/simpleEditText\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:layout_centerInParent=\"true\"\r\n    android:hint=\"Enter Your Name Here\" \/&gt;&lt;!--display the hint--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-767\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/hint-in-EditText-Android.jpg\" alt=\"hint in EditText Android\" width=\"355\" height=\"432\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/hint-in-EditText-Android.jpg 355w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/hint-in-EditText-Android-247x300.jpg 247w\" sizes=\"auto, (max-width: 355px) 100vw, 355px\" \/><\/center><strong>Setting hint in EditText In Java class:<\/strong><\/p>\n<p>Below is the example code in which we set the text in a\u00a0 text view programmatically means in java class.<\/p>\n<pre>EditText editText = (EditText)findViewById(R.id.simpleEditText);\r\neditText.setHint(\"Enter Your Name Here\");\/\/display the hint\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. textColor:<\/strong><\/span> textColor attribute is used to set the text color of a text edit text. Color value is in the form of &#8220;#argb&#8221;, &#8220;#rgb&#8221;, &#8220;#rrggbb&#8221;, or &#8220;#aarrggbb&#8221;.<\/p>\n<p>Below is the example code with explanation included in which we set the red color for the displayed text of a edit text.<\/p>\n<pre>&lt;EditText\r\n    android:id=\"@+id\/simpleEditText\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:layout_centerInParent=\"true\"\r\n    android:text=\"Password\"\r\n    android:textColor=\"#f00\"\/&gt;&lt;!--set the red text color--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-771\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/textColor-in-EditText-Android-241x300.jpg\" alt=\"textColor in EditText Android\" width=\"241\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/textColor-in-EditText-Android-241x300.jpg 241w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/textColor-in-EditText-Android.jpg 364w\" sizes=\"auto, (max-width: 241px) 100vw, 241px\" \/><\/center><strong>Setting textColor in EditText In Java class:<\/strong><\/p>\n<p>Below is the example code in which we set the text color of a edit text programmatically means in java class.<\/p>\n<pre>EditText simpleEditText=(EditText)findViewById(R.id.<em>simpleEditText<\/em>);\r\nsimpleEditText.setTextColor(Color.<em>RED<\/em>);\/\/set the red text color\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. textColorHint:<\/strong><\/span> textColorHint is an attribute used to set the color of displayed hint.<\/p>\n<p>Below is the example code with explanation included in which we set the green color for displayed hint of a edit text.<\/p>\n<pre>&lt;EditText\r\n    android:id=\"@+id\/simpleEditText\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:layout_centerInParent=\"true\"\r\n    android:hint=\"Enter Your Name Here\"\r\n    android:textColorHint=\"#0f0\"\/&gt;&lt;!--set the hint color green--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-773\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textColorHint-in-EditText-Android.jpg\" alt=\"textColorHint in EditText Android\" width=\"353\" height=\"417\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textColorHint-in-EditText-Android.jpg 353w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textColorHint-in-EditText-Android-254x300.jpg 254w\" sizes=\"auto, (max-width: 353px) 100vw, 353px\" \/><\/center><strong>Setting textColorHint in EditText In Java class:<\/strong><\/p>\n<p>Below is the example code in which we set the hint color of a edit text programmatically means in java class.<\/p>\n<pre>EditText simpleEditText=(EditText)findViewById(R.id.<em>simpleEditText<\/em>);\r\nsimpleEditText.setHintTextColor(Color.<em>green<\/em>(0));\/\/set the green hint color\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. textSize:<\/strong><\/span> textSize attribute is used to set the size of text of a edit text. We can set the text size in sp(scale independent pixel) or dp(density pixel).<\/p>\n<p>Below is the example code in which we set the 25sp size for the text of a edit text.<\/p>\n<pre>&lt;EditText\r\n    android:id=\"@+id\/simpleEditText\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:layout_centerInParent=\"true\"\r\n    android:text=\"AbhiAndroid\"\r\n    android:textSize=\"25sp\" \/&gt;&lt;!--set 25sp text size--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-774\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Setting-textSize-in-EditText-Android-255x300.jpg\" alt=\"Setting textSize in EditText Android\" width=\"255\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Setting-textSize-in-EditText-Android-255x300.jpg 255w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Setting-textSize-in-EditText-Android.jpg 359w\" sizes=\"auto, (max-width: 255px) 100vw, 255px\" \/><\/center><strong>Setting textSize in EditText in Java class:<\/strong><\/p>\n<p>Below is the example code in which we set the text size of a edit text programmatically means in java class.<\/p>\n<pre>EditText simpleEditText=(EditText)findViewById(R.id.<em>simpleEditText<\/em>);\r\nsimpleEditText.setTextSize(25);\/\/set size of text\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>8. textStyle:<\/strong><\/span> textStyle attribute is used to set the text style of a edit text. The possible text styles are bold, italic and normal. If we need to use two or more styles for a edit text then \u201c|\u201d operator is used for that.<\/p>\n<p>Below is the example code with explanation included, in which we set the bold and italic text styles for text.<\/p>\n<pre>&lt;EditText\r\n    android:id=\"@+id\/simpleEditText\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:layout_centerInParent=\"true\"\r\n    android:text=\"Email\"\r\n    android:textSize=\"25sp\"\r\n    android:textStyle=\"bold|italic\"\/&gt;&lt;!--set bold and italic text style--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-775\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Setting-textStyle-in-EditText-Android-242x300.jpg\" alt=\"Setting textStyle in EditText Android\" width=\"242\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Setting-textStyle-in-EditText-Android-242x300.jpg 242w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Setting-textStyle-in-EditText-Android.jpg 357w\" sizes=\"auto, (max-width: 242px) 100vw, 242px\" \/><\/center><span style=\"color: #008000;\"><strong>9. background:<\/strong><\/span> 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.<\/p>\n<p>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\u2019s for edit text.<\/p>\n<pre>&lt;EditText\r\n    android:id=\"@+id\/simpleEditText\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:layout_centerInParent=\"true\"\r\n    android:hint=\"Enter Your Name Here\"\r\n    android:padding=\"15dp\"\r\n    android:textColorHint=\"#fff\"\r\n    android:textStyle=\"bold|italic\"\r\n    android:background=\"#000\"\/&gt;&lt;!--set background color black--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-776\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Background-in-EditText-Android-247x300.jpg\" alt=\"Background in EditText Android\" width=\"247\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Background-in-EditText-Android-247x300.jpg 247w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Background-in-EditText-Android.jpg 359w\" sizes=\"auto, (max-width: 247px) 100vw, 247px\" \/><\/center><strong>Setting Background in EditText In Java class:<\/strong><\/p>\n<p>Below is the example code in which we set the background color of a edit text programmatically means in java class.<\/p>\n<pre>EditText simpleEditText=(EditText)findViewById(R.id.<em>simpleEditText<\/em>);\r\nsimpleEditText.setBackgroundColor(Color.<em>BLACK<\/em>);\/\/set black background color\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>10. padding:<\/strong><\/span> 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\u2019s of edit text.<\/p>\n<hr \/>\n<h4><strong>Example I &#8211; EditText in Android Studio<\/strong><\/h4>\n<p>Below is the example of edit text in which we get the value from multiple edittexts and on button click event the Toast will show the data defined in Edittext.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"http:\/\/www.mediafire.com\/file\/et66med6yto5rlo\/EdittextExample.zip\/file\" target=\"_blank\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2388\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/EditText-Example-In-Android-Studio.gif\" alt=\"EditText Example In Android Studio\" width=\"269\" height=\"419\" \/><br \/>\n<span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project in Android Studio and name it EditTextExample.<br \/>\n<span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Now Open res -&gt; layout -&gt;\u00a0xml (or) activity_main.xml\u00a0and add following code. In this code we have added multiple edittext and a button with onclick functionality.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:id=\"@+id\/activity_main\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\"com.example.edittextexample.MainActivity\"&gt;\r\n\r\n    &lt;EditText\r\n        android:id=\"@+id\/editText1\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_alignParentLeft=\"true\"\r\n        android:layout_alignParentStart=\"true\"\r\n        android:layout_alignParentTop=\"true\"\r\n        android:layout_marginLeft=\"50dp\"\r\n        android:layout_marginStart=\"50dp\"\r\n        android:layout_marginTop=\"24dp\"\r\n        android:ems=\"10\"\r\n        android:hint=\"@string\/name\"\r\n        android:inputType=\"textPersonName\"\r\n        android:selectAllOnFocus=\"true\" \/&gt;\r\n\r\n    &lt;EditText\r\n        android:id=\"@+id\/editText2\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_alignLeft=\"@+id\/editText1\"\r\n        android:layout_alignStart=\"@+id\/editText1\"\r\n        android:layout_below=\"@+id\/editText1\"\r\n        android:layout_marginTop=\"19dp\"\r\n        android:ems=\"10\"\r\n        android:hint=\"@string\/password_0_9\"\r\n        android:inputType=\"numberPassword\" \/&gt;\r\n\r\n    &lt;EditText\r\n        android:id=\"@+id\/editText3\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_alignLeft=\"@+id\/editText2\"\r\n        android:layout_alignStart=\"@+id\/editText2\"\r\n        android:layout_below=\"@+id\/editText2\"\r\n        android:layout_marginTop=\"12dp\"\r\n        android:ems=\"10\"\r\n        android:hint=\"@string\/e_mail\"\r\n        android:inputType=\"textEmailAddress\" \/&gt;\r\n\r\n    &lt;EditText\r\n        android:id=\"@+id\/editText4\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_alignLeft=\"@+id\/editText3\"\r\n        android:layout_alignStart=\"@+id\/editText3\"\r\n        android:layout_below=\"@+id\/editText3\"\r\n        android:layout_marginTop=\"18dp\"\r\n        android:ems=\"10\"\r\n        android:hint=\"@string\/date\"\r\n        android:inputType=\"date\" \/&gt;\r\n\r\n    &lt;EditText\r\n        android:id=\"@+id\/editText5\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_alignLeft=\"@+id\/editText4\"\r\n        android:layout_alignStart=\"@+id\/editText4\"\r\n        android:layout_below=\"@+id\/editText4\"\r\n        android:layout_marginTop=\"18dp\"\r\n        android:ems=\"10\"\r\n        android:hint=\"@string\/contact_number\"\r\n        android:inputType=\"phone\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/button\"\r\n        style=\"@android:style\/Widget.Button\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_alignParentLeft=\"true\"\r\n        android:layout_alignParentStart=\"true\"\r\n        android:layout_below=\"@+id\/editText5\"\r\n        android:layout_marginTop=\"62dp\"\r\n        android:text=\"@string\/submit\"\r\n        android:textSize=\"16sp\"\r\n        android:textStyle=\"normal|bold\" \/&gt;\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Now open app -&gt; java -&gt; package -&gt; MainActivity.java and add the below code.<br \/>\nIn this we just fetch the text from the edittext, further with the button click event a toast will show the text fetched before.<\/p>\n<pre>package com.example.edittextexample;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.EditText;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    Button submit;\r\n    EditText name, password, email, contact, date;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        name = (EditText) findViewById(R.id.editText1);\r\n        password = (EditText) findViewById(R.id.editText2);\r\n        email = (EditText) findViewById(R.id.editText3);\r\n        date = (EditText) findViewById(R.id.editText4);\r\n        contact = (EditText) findViewById(R.id.editText5);\r\n        submit = (Button) findViewById(R.id.button);\r\n\r\n        submit.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                if (name.getText().toString().isEmpty() || password.getText().toString().isEmpty() || email.getText().toString().isEmpty() || date.getText().toString().isEmpty()\r\n                        || contact.getText().toString().isEmpty()) {\r\n                    Toast.makeText(getApplicationContext(), \"Enter the Data\", Toast.LENGTH_SHORT).show();\r\n                } else {\r\n                    Toast.makeText(getApplicationContext(), \"Name -  \" + name.getText().toString() + \" \\n\" + \"Password -  \" + password.getText().toString()\r\n                            + \" \\n\" + \"E-Mail -  \" + email.getText().toString() + \" \\n\" + \"Date -  \" + date.getText().toString()\r\n                            + \" \\n\" + \"Contact -  \" + contact.getText().toString(), Toast.LENGTH_SHORT).show();\r\n                }\r\n            }\r\n        });\r\n    }\r\n}\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">Output:<\/span><\/strong><\/p>\n<p>Now start the AVD in Emulator and run the App. You will see screen asking you to fill the data in required fields like name, password(numeric), email, date, contact number. Enter data and click on button. You will see the data entered will be displayed as Toast on screen.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2390\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/EditText-Example-Output-In-Android-Studio-1.png\" alt=\"\" width=\"323\" height=\"511\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/EditText-Example-Output-In-Android-Studio-1.png 323w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/EditText-Example-Output-In-Android-Studio-1-190x300.png 190w\" sizes=\"auto, (max-width: 323px) 100vw, 323px\" \/><\/p>\n<hr \/>\n<h4><strong>Example II &#8211; EditText in Android Studio<\/strong><\/h4>\n<p>Below is the example of edit text in which we get the value from a edit text on button click event and then display it in a Toast. Below is the final output and code.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/EdittextExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-777\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/EditText-Example-With-Code-in-Android-Studio.jpg\" alt=\"EditText Example With Code in Android Studio\" width=\"248\" height=\"426\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/EditText-Example-With-Code-in-Android-Studio.jpg 248w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/EditText-Example-With-Code-in-Android-Studio-175x300.jpg 175w\" sizes=\"auto, (max-width: 248px) 100vw, 248px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project in Android Studio and name it EditTextExample.<\/p>\n<pre>Select File -&gt; New -&gt; New Project and Fill the forms and click \"Finish\" button.\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Now Open res -&gt; layout -&gt;\u00a0xml (or) activity_main.xml\u00a0and add following code. Here we will design one EditText for filling name and one Button which will be used to display the name entered by the user.<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;EditText\r\n        android:id=\"@+id\/simpleEditText\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:background=\"#F2F2F2\"\r\n        android:hint=\"Enter Your Name Here\"\r\n        android:padding=\"15dp\"\r\n        android:textColorHint=\"#000\"\r\n        android:textStyle=\"bold|italic\"\r\n        android:layout_marginTop=\"100dp\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/displayText\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerInParent=\"true\"\r\n        android:background=\"#000\"\r\n        android:padding=\"10dp\"\r\n        android:text=\"Display Text\"\r\n        android:textColor=\"#0f0\"\r\n        android:textStyle=\"bold\" \/&gt;\r\n\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Now open app -&gt; java -&gt; package -&gt;\u00a0MainActivity.java and add the below code. The explanation is included in the code itself as comment.<strong><br \/>\n<\/strong><\/p>\n<pre>package example.abhiandriod.edittextexample;\r\n\r\nimport android.graphics.Color;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.EditText;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        final EditText simpleEditText = (EditText) findViewById(R.id.simpleEditText);\/\/get the id for edit text\r\n        Button displayText = (Button) findViewById(R.id.displayText);\/\/get the id for button\r\n        displayText.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View view) {\r\n                if (simpleEditText.getText().toString() != null)\/\/check whether the entered text is not null\r\n                {\r\n                    Toast.makeText(getApplicationContext(), simpleEditText.getText().toString(), Toast.LENGTH_LONG).show();\/\/display the text that you entered in edit text\r\n                }\r\n            }\r\n        });\r\n    }\r\n\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now start the AVD in Emulator and run the App. You will see screen asking you to fill your name. Enter your name and click on button. You will see the name entered will be displayed as Toast on screen.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-778\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/EditText-Example-Android-Studio-Output.jpg\" alt=\"EditText Example Android Studio Output\" width=\"274\" height=\"482\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/EditText-Example-Android-Studio-Output.jpg 274w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/EditText-Example-Android-Studio-Output-171x300.jpg 171w\" sizes=\"auto, (max-width: 274px) 100vw, 274px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>TextInputLayout \/ Floating Labels In EditText:<\/strong><\/h4>\n<p>TextInputLayout is a new element introduced in Material Design Support library to display the floating label in\u00a0EditText. Read our advance\u00a0<a href=\"\/materialdesign\/textinputlayout-floating-labels-edittext\">Floating Labels tutorial <\/a>to learn how to use it in your App.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"\/materialdesign\/wp-content\/uploads\/2016\/06\/Floating-Labels-In-EditText-TextInputLayout-Android.gif\" alt=\"Floating Labels in EditText\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, EditText is a standard entry widget in android apps. It is an overlay over TextView that configures itself to be editable. EditText is a subclass of TextView with text editing operations. We often use EditText in our applications in order to provide an input or text field, especially in forms. The most simple &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/edittext\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">EditText Tutorial With Example In Android Studio: Input Field<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-762","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>EditText Tutorial With Example In Android Studio: Input Field \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"EditText is used to provide an input or text field, especially in forms. Learn the concept and attributes in detail with example and code in Android Studio.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/edittext\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=762"}],"version-history":[{"count":5,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/762\/revisions"}],"predecessor-version":[{"id":2846,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/762\/revisions\/2846"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}