Format Edittext Android
- EditText has an attribute called android:inputType. Providing android:inputType with value phone can display numbered keyboard when the EditText gets focus. Create an Android Project and replace layout (activitymain.xml) and Kotlin file (MainActivity.kt) with the following code.
- 2) Now initialize Edit Text in your java file. Here I have used the method DateDialog to open the calendar popup and select the date. Here I have used the method DateDialog to.
//SOURCE: http://stackoverflow.com/questions/16889502/how-to-mask-an-edittext-to-show-the-dd-mm-yyyy-date-format |
EditText date; |
date = (EditText)findViewById(R.id.whichdate); |
date.addTextChangedListener(tw); |
TextWatcher tw =newTextWatcher() { |
privateString current =''; |
privateString ddmmyyyy ='DDMMYYYY'; |
privateCalendar cal =Calendar.getInstance(); |
When user changes text of the EditText |
@Override |
publicvoid onTextChanged(CharSequence s, int start, int before, int count) { |
if (!s.toString().equals(current)) { |
String clean = s.toString().replaceAll('[^d.]', ''); |
String cleanC = current.replaceAll('[^d.]', ''); |
int cl = clean.length(); |
int sel = cl; |
for (int i =2; i <= cl && i <6; i +=2) { |
sel++; |
} |
//Fix for pressing delete next to a forward slash |
if (clean.equals(cleanC)) sel--; |
if (clean.length() <8){ |
clean = clean + ddmmyyyy.substring(clean.length()); |
}else{ |
//This part makes sure that when we finish entering numbers |
//the date is correct, fixing it otherwise |
int day =Integer.parseInt(clean.substring(0,2)); |
int mon =Integer.parseInt(clean.substring(2,4)); |
int year =Integer.parseInt(clean.substring(4,8)); |
if(mon >12) mon =12; |
cal.set(Calendar.MONTH, mon-1); |
year = (year<1900)?1900:(year>2100)?2100:year; |
cal.set(Calendar.YEAR, year); |
// ^ first set year for the line below to work correctly |
//with leap years - otherwise, date e.g. 29/02/2012 |
//would be automatically corrected to 28/02/2012 |
day = (day > cal.getActualMaximum(Calendar.DATE))? cal.getActualMaximum(Calendar.DATE):day; |
clean =String.format('%02d%02d%02d',day, mon, year); |
} |
clean =String.format('%s/%s/%s', clean.substring(0, 2), |
clean.substring(2, 4), |
clean.substring(4, 8)); |
sel = sel <0?0: sel; |
current = clean; |
date.setText(current); |
date.setSelection(sel < current.length() ? sel : current.length()); |
} |
} |
We also implement the other two functions because we have to |
@Override |
publicvoid beforeTextChanged(CharSequence s, int start, int count, int after) {} |
@Override |
publicvoid afterTextChanged(Editable s) {} |
}; |
Android Show DatePicker on EditText Click Example. Following is the example of open or popup datepicker dialog when we click on EditText control and get the selected date value on Button click in the android application. Create a new android application using android studio and give names as DatePickerExample. 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.
commented Nov 27, 2019
when one character in middle is deleted cursor deletes the last character.how to fix this? |
- Related Questions & Answers
- Selected Reading
This example demonstrates about How to limit Decimal Places in Android EditText.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
Step 3 − Add the following code to src/MainActivity.java
Step 4 − Add the following code to androidManifest.xml
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −
Edittext Android Studio
Click here to download the project code.