Recently i did a task related to edit text for my project.I want to make edit text more readable and understandable to user.
My task is: I need to get the input of user room height.When user enters the data in edit text box like 90 i want to make edit text data "height 90 m" at the time of giving input.
Noticed points:
1)All ways height must be in digits.It is not been characters.
2) And changing the edit text data at the time of entering the data into edit text.
Lets see how can i solved:
1) Declared edit text in my xml file.
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</EditText>
don't add android:inputType="number" property to make edit text input is digits.
2)Add the TextChangedListener to our edit text in java file. like
roomHightEdit.addTextChangedListener(new MyRoomTextWatcher(
roomWidthEdit));
3)Define the MyRoomTextWatcher class like and fallow my comments
private class MyRoomTextWatcher implements TextWatcher {
boolean mEditing;
EditText yourEditText;
//Constructor takes the argument as your edittext Id.
public MyRoomTextWatcher(EditText editext) {
mEditing = false;
yourEditText = editext;
}
@Override
public void afterTextChanged(Editable s) {
if (!mEditing) {
//make editing is true
//get only digits. "\\D" is a regular expression .You can find more details at patterns.link is
// http://developer.android.com/reference/java/util/regex/Pattern.html
String digits = s.toString().replaceAll("\\D", "");
try {
String formatted = "Height\n" + digits + " m";
Log.v("length", s.length() + "formated" + formatted);
s.replace(0, s.length(), formatted);
Log.v("tag", s.toString());
//Making cursor position of your edit text.
yourEditText.setSelection(s.length() - 2);
} catch (NumberFormatException nfe) {
s.clear();
}
mEditing = false;
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}
That's it.May be use full to you.Thanks.
My task is: I need to get the input of user room height.When user enters the data in edit text box like 90 i want to make edit text data "height 90 m" at the time of giving input.
Noticed points:
1)All ways height must be in digits.It is not been characters.
2) And changing the edit text data at the time of entering the data into edit text.
Lets see how can i solved:
1) Declared edit text in my xml file.
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</EditText>
don't add android:inputType="number" property to make edit text input is digits.
2)Add the TextChangedListener to our edit text in java file. like
roomHightEdit.addTextChangedListener(new MyRoomTextWatcher(
roomWidthEdit));
3)Define the MyRoomTextWatcher class like and fallow my comments
private class MyRoomTextWatcher implements TextWatcher {
boolean mEditing;
EditText yourEditText;
//Constructor takes the argument as your edittext Id.
public MyRoomTextWatcher(EditText editext) {
mEditing = false;
yourEditText = editext;
}
@Override
public void afterTextChanged(Editable s) {
if (!mEditing) {
//make editing is true
mEditing = true;
//get only digits. "\\D" is a regular expression .You can find more details at patterns.link is
// http://developer.android.com/reference/java/util/regex/Pattern.html
String digits = s.toString().replaceAll("\\D", "");
try {
String formatted = "Height\n" + digits + " m";
Log.v("length", s.length() + "formated" + formatted);
s.replace(0, s.length(), formatted);
Log.v("tag", s.toString());
//Making cursor position of your edit text.
yourEditText.setSelection(s.length() - 2);
} catch (NumberFormatException nfe) {
s.clear();
}
mEditing = false;
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}
That's it.May be use full to you.Thanks.