Android EditText Control With Example

In Android, EditText Control is a input field like HTML input for  text to a user because it is a user interface element. EditText is an overlay over TextView that allow him to be editable. EditText is a complete text editor. EditText is a subclass of TextView. So, In this Android tutorial, we are going to discuss each and every aspect of Android EditText.

EditText has a parent class called View. Being a subclass of View, EditText control can be used in our Android Studio App GUI ( Graphical User Interface ) inside a ViewGroup, or as the content view of an activity.

Android EditText Control

Android EditText Control

EditText Code IN XML:

The following code shows a use of EditText with an XML layout and code to modify the content of the text view:

In above example, EditText has an id attribute whose value is edittext_id and layout_height and layout_width having the same value wrap_content which means it only covers the space equal to its text attribute text.

EditText Code IN JAVA:

The following code shows a use of EditText with a Java and code to modify the content of the EditText programmatically:

Now Let’s discuss Android EditText attributes one by one.

Android EditText Attributes

1. android:id – This is the id attribute which uniquely identifies the EditText control. It is used for get and set the properties of EditText Control in JAVA class.

Below is the example code in which we set the id attribute of a EditText to a edittext_id.

2. android:layout_width – This is the layout_width attribute which defines the width of the EditText . Its value can be wrap_content, match_parent and fill_parent.

Below is the example code in which we set the layout_width attribute of a EditText to a wrap_content.

3. android:layout_height – This is the layout_height attribute which defines the height of the EditText . Its value can be wrap_content, match_parent and fill_parent.

Below is the example code in which we set the layout_height attribute of a EditText to a wrap_content.

4. android:text – This is the text attribute which is used to display text to a  user. We can set text in XML as well as in a JAVA class.

Below is the example code in XML where we set the text “Codekila Android Tutorials” in a EditText Control in XML.

 

IN JAVA –

Let’s take an example in which we set the text of a EditText programmatically means in JAVA class.

5. android:textSize – This is the textSize attribute which defines the size of the text and can be set in XML. We can set the textSize in sp (scale pixel) or either it can be in dp (density pixel). Recommended dimension type for text is “sp” for scaled-pixels (example: 12sp).

Let’s take an example in which we set the text size of a EditText in XML.

IN JAVA –

Let’s take an example in which we set the text size of a EditText programmatically means in JAVA class.

6. android:textColor –  This is the textColor attribute which defines the color of the text and can be set in XML. Color of EditText value is in “#rgb”, “#ffff”

Let’s take an example in which we set the color of a EditText in XML.

 

IN JAVA –

Let’s take an example in which we set the textColor of a EditText programmatically means in JAVA class.

 

Android EditText Example

Now, we take a full example of Android EditText Control in an android studio. Below is the screenshot of the android EditText example which we are going to show you now step by step. In this example, we will create a EditText and Button when we click on Button We display a Toast (Message) to show that what is in EditText.

 

EditText Before Click Example

Step 1: Open Android Studio and Create a new project and name it androidEditText .

Step 2: Open res >  layout > xml (or) activity_main.xml and add below mention code. Here we will create a EditText in Relative Layout.

Step 3: Open app  > java  > package ( in my case com.codekila.androidedittext ) and open MainActivity.java and add the below mentioned code. Here we will change the text of EditText after the user clicks on EditText .

Step 4: Below is the default content of AndroidManifest.xml  which is created at the time of creating a new android studio project. −

Result:

Now run the app in Emulator and click on the EditText . You will see text will change to “Codekila.com After Click”.

EditText After Click

 

Conclusion:

Here we can see that how android EditText control used to input text on the EditText which is  user interface side.If you have any query related to  Android EditText Control With Example post feel free to comment on the comment box. Do not forget to share this post because someone else also needs this. And don’t forget to like our Facebook Page.

Also Read:

  1. Android Tutorial For Beginners
  2. Android TextView Control Full Tutorial With Example

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *