I was implemented like after 4 digits hyphen display automatically like(2015-07) in edittext. my code works fine, but problem is while i delete before 4 digits value and again type it not working. addTextChangedListener not trigger when i edidtext retype like 2015-07 to 2014-07. But while i using "/" instead of "-" i can retype value. What is the problem?
mEdtProductionCode.addTextChangedListener(new TextWatcher() {
int prevL = 0;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
prevL = mEdtProductionCode.getText().toString().length();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
int length = s.length();
if ((prevL < length) && length == 4) {
String data = mEdtProductionCode.getText().toString();
mEdtProductionCode.setText(data + "-");
mEdtProductionCode.setSelection(length + 1);
}
}
});
You should just move your character checking to the character after the fifth character has been entered, and then chop down String to put the custom character in between:
#Override
public void afterTextChanged(Editable s) {
int length = s.length();
if ((prevL <= length) && length == 5) {
String data = mEditProductionCode.getText().toString();
String beginData = data.substring(0,4);
String endData = Character.toString(data.charAt(length-1));
mEditProductionCode.setText(beginData + "-" + endData);
mEditProductionCode.setSelection(length + 1);
}
}
You can also use data.charAt(length-1) != '-' to check if user manually made dash input, in which case you just ignore and do not make changes to TextEdit.
Related
In android edit text, how to separate 10 digit number input by space? I am using android text watcher and I am trying to input multiple 10 digit numbers in the field. The issue arises when multiple numbers are copied and pasted in the field and that time, it doesn't take those spaces. Kindly let me know a solution in order to allow multiple number input with a space after every 10 digit number, when the number is copied from other place.
This will work for both type and copy/paste from other place.
yourEditText.addTextChangedListener(new TextWatcher() {
private static final char space = ' ';
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
int pos = 0;
while (true) {
if (pos >= s.length()) break;
if (space == s.charAt(pos) && (((pos + 1) % 11) != 0 || pos + 1 == s.length())) {
s.delete(pos, pos + 1);
} else {
pos++;
}
}
pos = 10;
while (true) {
if (pos >= s.length()) break;
final char c = s.charAt(pos);
if (Character.isDigit(c)) {
s.insert(pos, "" + space);
}
pos += 11;
}
}
});
Edit and Use the following code as per your needs
StringBuilder s;
s = new StringBuilder(yourTxtView.getText().toString());
for(int i = 10; i < s.length(); i += 10){
s.insert(i, " "); // this line inserts a space
}
yourTxtView.setText(s.toString());
and when you need to get the String without spaces do this:
String str = yourTxtView.getText().toString().replace(" ", "");
Hey guys I'm developing an application for Market Transactions and stuff, and the client wants to have a condition on the edit text of type number decimal.
He wants the user to enter only number 5 after the dot if the number is a decimal and only one digit after the dot.
i.e.
15
14
1949
12.33 (not accepted)
12.5 (accepted)
so how to do so please give me some hints
try this and check whether it works.
final String regex = "^\\-?(\\d{0,5}|\\d{0,5}\\.[5]{0,1})$";
((EditText)rootView.findViewById(R.id.editText1)).setFilters(new InputFilter[] {
new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned destination, int destinationStart, int destinationEnd) {
if (end > start) {
// adding: filter
// build the resulting text
String destinationString = destination.toString();
String resultingTxt = destinationString.substring(0, destinationStart) + source.subSequence(start, end) + destinationString.substring(destinationEnd);
// return null to accept the input or empty to reject it
return resultingTxt.matches(regex) ? null : "";
}
// removing: always accept
return null;
}
}
});
Here user input is checked to match the regular expression and return the input string if it matches and null if not.
<Edittext
android:id="#+id/testing"
android:inputType="numberDecimal"
android:digits="0123456789."
>
</Edittext>
Then, use InputFilter
testing.setFilters(new InputFilter[]{NumberFilter(), new InputFilter.LengthFilter(6)});
//OR
testing.setFilters(new InputFilter[]{NumberFilter()});
Create class for NumberFilter implemenets InputFilter
public class NumberFilter implements InputFilter{
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
// ignore empty space and not number
if (!isNullorEmpty(source.toString()) && !isOnlyNumber(source.toString()))
return "";
//ignore first .
if (!source.toString().charAt(0).equals("."))
return "";
if ((source.toString().charAt(0) != '8' || source.toString().charAt(0) != '9'))
return "";
return null;
}
}
// to allow 1 and 1.2
public static boolean isOnlyNumber(String number) {
return number.matches("(?<=^| )\d+(\.\d+)?(?=$| )");
}
//check empty space or Null
public static boolean isNullorEmpty(String string){
return StringUtils.isEmpty(string);
}
Try this addTextChangedListener.
You need to append 5 to the editText whenever user inputs a dot.
`flag =0;
_field.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.toString().contains(".") && (flag == 0)) {
flag = 1;
_field.setText(_field.getText() + "5");
} else if (s.toString().contains(".") && (flag == 1)) {
} else {
flag = 0;
}
}
});`
I need to get an input of 9 uppercase letters separated by dash like AAA-AAA-AAA and
this input has to be formatted while is entered.
Thanks in advance.
I have found the solution using a TextWatcher. I'm using
android:inputType="textCapCharacters|textNoSuggestions"
for the EditText input type in order to receive only uppercase letters.
txtCode = (EditText) findViewById(R.id.txtCode);
txtCode.addTextChangedListener( new TextWatcher() {
boolean isEdiging;
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
#Override
public void afterTextChanged(Editable s) {
if(isEdiging) return;
isEdiging = true;
// removing old dashes
StringBuilder sb = new StringBuilder();
sb.append(s.toString().replace("-", ""));
if (sb.length()> 3)
sb.insert(3, "-");
if (sb.length()> 7)
sb.insert(7, "-");
if(sb.length()> 11)
sb.delete(11, sb.length());
s.replace(0, s.length(), sb.toString());
isEdiging = false;
}
});
You can do this by text change listner
when text_size%3==0 insert "-" character
How can I customize input type of editext shown in the given figure.Basically my requirement is that edittext should show only the last 3 or 4 digits only initial 12 digit should be in password mode.
You need to add a TextWatcher onto the EditText:
int characterCount = 0;
int asteriskCount = 0;
CharSequence input = null;
input.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
characterCount = count;
//update input sequence based on changes.
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//update input sequence based on changes.
}
#Override
public void afterTextChanged(Editable s) {
if (asteriskCount != characterCount) {
//make the visible sequence here.
CharSequence seq = "";
for (int i = 0; i < (characterCount <= 12 ? characterCount : 12); i++) {
seq = seq + "*";
}
if (characterCount > 12) {
for (int i = 12; i < characterCount; i++) {
seq = seq + characterCount.charAt(i);
}
}
asteriskCount = characterCount;
input.setText(seq);
}
}
});
There is no built in feature like this. So you have to do it by yourself. You have to make change on the text when the text is changed. To do so .
If you create a custom editText by extending EditText then you can overwrite the onTextChanged method and hanlde the changes.
Or you can use a TextWatcher to hadle changes.
So when the text is changed set the data except last 3 digitst to *.
But remember that you have to use a String field to store original data in a field.
Below is the code snippet of my TextWatcher:
private boolean spaceDeleted;
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
characterCount = start;
//update input sequence based on changes.
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// check if a space was deleted
CharSequence charDeleted = s.subSequence(start, start + count);
spaceDeleted = " ".equals(charDeleted.toString());
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()>12){
return;
}
System.out.println("Character Count in afterTextChange->"+characterCount);
System.out.println("Editable Character->"+s);
ccNumber.removeTextChangedListener(this);
// record cursor position as setting the text in the textview
// places the cursor at the end
int cursorPosition = ccNumber.getSelectionStart();
String withSpaces = formatText(s);
ccNumber.setText(withSpaces);
// set the cursor at the last position + the spaces added since the
// space are always added before the cursor
ccNumber.setSelection(cursorPosition + (withSpaces.length() - s.length()));
// if a space was deleted also deleted just move the cursor
// before the space
if (spaceDeleted) {
ccNumber.setSelection(ccNumber.getSelectionStart() - 1);
spaceDeleted = false;
}
// enable text watcher
ccNumber.addTextChangedListener(this);
}
private String formatText(CharSequence s) {
// TODO Auto-generated method stub
StringBuilder formatted = new StringBuilder();
int count = 0;
/* if(s.length()<12){
formatted.append("*");
}else{
formatted.append(s.charAt(characterCount));
}*/
for (int i = 0; i < s.length(); ++i)
{
formatted.append("*");
/*if (Character.isDigit(s.charAt(i)))
{
if (count % 4 == 0 && count > 0)
formatted.append(" ");
formatted.append(s.charAt(i));
++count;
}*/
}
return formatted.toString();
}
});
i need to remove characters if i add any special symbols to the edit text while am entering data to it.for example i am type a word smwinæ æ is a special characters so if i add that chareter edit text should remove the æ and display only smwin by replacing æ .i used text change listener. check the code below
email.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Abstract Method of TextWatcher Interface.
System.out.println("started after");
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
System.out.println("started");
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence searchcontact, int start,
int before, int count) {
System.out.println("sssssssssss"+searchcontact);
String a=searchcontact.toString();
System.out.println("casted "+a);
String[] parts = a.split(" ");
String lastWord = parts[parts.length - 1];
System.out.println("------------------------------"+lastWord);
// String lastWord = a.substring(a.lastIndexOf(" ")+1);
// System.out.println("lastword"+lastWord);
if(a.equals("p"))
{
try {
email.getText().delete(email.getSelectionEnd() - 1, email.getSelectionStart());
} catch (Exception e) {
try {
email.getText().delete(email.length() - 1, email.length());
} catch (Exception myException) {
//textfield.getText().delete(textfield.length(), textfield.length() - 1);
}
}
// Method User For Sort Out THE Words
// in
// The SearchBar
}
}
});
here when text change i try to get the last word in the string but i failed to do this.used
String a=searchcontact.toString();
System.out.println("casted "+a);
String[] parts = a.split(" ");
String lastWord = parts[parts.length - 1];
to get the last word but it prints the entire string in the edittext how can i do this please help
You can add a TextWatcher to an EditText and get notified each time the text is notified. Using that, you can just parse the String to find the characters after a space, and update them to uppercase.
Here's a quick test I made which works pretty well (far from optimal, because each time you edit the Editable it calls the listener again...).
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
String string = s.toString();
int index = -1;
while (((index = string.indexOf(' ', index + 1)) != -1) && (index + 1 < string.length())) {
// Get character
char c = string.charAt(index + 1);
if (Character.isLowerCase(c)) {
// Replace in editable by uppercase version
s.replace(index+1, index + 2, Character.toString(c).toUpperCase());
}
}
}
});
To avoid being called to often, you could make all the changes in a char[] and only commit to the Editable if changes were made.
A simpler solution is probably to just use split(' ') on your String, replace all first letters in the String[] by the uppercase version (if needed), and commit only once to the Editable.
A simpler optimisation would be to add a boolean to your anonymous class, set it to try when you enter afterTextChanged, set it back to false when you exit it, and only process the string if the boolean is false.