Android EditText

 

Android EditText widget is better known as TextBox in case of HMTL. It provides interface to collect data in form of text entered by user. By specifying property inputType we can customize android edittext as plain text, password, numeric, phone No., Address field , Date field, etc. based on requirement of data from user.


 

Download Source Code : EditText.zip


 

Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) Edittext which you might have downloaded from above or you can follow the scripts below by copying it to implement in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/EditText/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.

 

 

1. activity_main.xml : Layout file

 

Explaining above code separately :

 Edittext – 1

  •  This is a basic Syntax for declaring a Android EditText.
  • Line 6 declares ems value 10, i.e. it specifies width of our EditText.
  • Line 7 <requestFocus/> tag brings a virtual soft Keyboard immediately on page load for our current EditText. Remember that if we have multiple EditText, it should be added only to the 1st EditText.

 

Edittext – 2

  •  Line 6 To tell user about what kind of data should be entered in our EditText we can provide a prmopt message called Hint which is shown by default over the EditText and wipes automatically when user types.
  • Line 7 Using property android:textColorHint we can specify any shade using 8-digit Hex. code for our hint

 

Edittext – 3

  • Line – 9 Whenever we want to get Sensitive data from user like passwords, PIN, etc. we have to use property android:inputType=”textPassword”, due to this EditText will display **** instead of characters entered by user.

 

Edittext – 4

  • Line – 9 If our EditText needs Numeric data from user like  numeric digits, pin-code, mobile No., etc. we have to use property android:inputType=”number”

 

Edittext – 5

  • Line – 9 So as to get Date from user, we have to use property  android:inputType=”date”

 

Edittext – 6

  • Line 9 By default EditText will scale as multi-line when text length increases its size. We can restrict it using property android:singleLine=”true”.

 

2. MainActivity.java

Explaining above Code :

  • Line 13 & 26-31 We have declared 6 EditText, and linked them with EditText of XML Layout file(activity_main.xml).
  • Line 16 Declaring 6 String variables to hold values of our EditText.
  • Line 38-46 It sets a Focus Change Listener for 1st EditText, i.e. either ‘we click on it’ or ‘having focus at 1st EditText we click at another place’ Focus change event will happen. Using focus change listener we catch such events. Such listeners are used while performing validations. Try it by removing the comments.
  • Line 60-65 On button click from user we fetch data from our EditText and store them in string variables. For this we follow syntax :  editText_Name.getText().toString() where getText() extracts value from our EditText in raw Object format and toString() converts it into string value.
  • Line 69-70 We have made provision of displaying values of all our EditText using Toast.

 

Final Output :


android edittext
                                  android edittext

 

Do It Yourself :
Try focus change Listener from MainActivity.java Line 38-46. Also you can try applying validations to them (Hint : Inside OnFocusChange method  validate value of EditText using If-condition )

 

Report Errors + Bugs & Become Insider for Nestedif.com

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to insider@nestedif.com you could help other peers.