remove special charecter in the while entring in a edittext in android; - android

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.

Related

Add dash/hyphen after 4 digits in edittext in android

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.

String encryption in editext

I have got an EditText in my Android app where you can type in some text. What I want to do is change only first character to another one (some kind of enciphering). Therefore I first have to read every single character.
How can I do that?
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.answer);
ed1=(EditText)findViewById(R.id.answer);
String s=ed1.getText().toString();
abc(s);
}
public void abc(String s){
//get your string
String str = s;
//turn it into an array of chars
char[] strChars = str .toCharArray();
//set array at position 1 to an x
strChars[1] = 'x';
str = String.valueOf(strChars);
ed1.setText(str);
}
Something like this would work:
//get your string
String str = "whatevertextyouwanthere";
//turn it into an array of chars
char[] strChars = str .toCharArray();
//set array at position 1 to an x
//Check to make sure the length is above -
if (str.length() != 0)
{
//Check that we have a value of some sorts in the char array
if (strChars[0] != ''
{
//replace the char at place 0 with X
strChars[0] = 'x';//this might need to be 1
}
}
//turn the array back into a string
str = String.valueOf(strChars);
Ideally you should have this within a function for ease of use and re-usability. It should return whatever the value of str is.
Post this in your onCreate:
ed1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
abc(ed1.getText().toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
The above was just some code I wrote really quickly but should do what you need it to do. You need to have a listener watching for changes to the edit text.

NumberFormatException in EditText

I have a TextView that calculates two EditText. It works as long as a digit is in the EditText but as soon all numbers are deleted I get this error
java.lang.NumberFormatException: unable to parse '' as integer
I understand why I'm getting the error but I cant figure out how to fix it. I've googled and searched for answers on this site but they don't seem to work for my situation. I've tried to catch the NumberFormatException but I cant do it. Any help?
items = (EditText)findViewById(R.id.items);
itemcost = (EditText)findViewById(R.id.itemcost);
inventoryvalue = (TextView)findViewById(R.id.inventoryvalue);
TextWatcher textWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
calculateResult();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
};
items.addTextChangedListener(textWatcher);
itemcost.addTextChangedListener(textWatcher);
}
private void calculateResult() throws NumberFormatException {
String s1 = items.getText().toString();
String s2 = itemcost.getText().toString();
int value1 = Integer.parseInt(s1);
int value2 = Integer.parseInt(s2);
int result = value1 * value2; {
// Calculates the result
result = value1 * value2;
// Displays the calculated result
inventoryvalue.setText(String.valueOf(result));
}
Check if your String contains only number:
s1 = s1.trim();
if (s1.matches("[0-9]+") {
value1 = Integer.parseInt(s1);
}
in the calculateResult method put everythin in an if block:
if(items.getText().tostreing().length>0 && itemcost.getText().toString().length>0){
//your current method definition
}
Change your afterTextChanged method to:
public void afterTextChanged(Editable s) {
if (s.length > 0)
calculateResult();
}
In calculateResult() you do Integer.parseInt(s1); without checking if String s1 or s2 are empty?
Thus you cant convert an empty String to Int. Try checking if s1 or s2 are empty before trying to convert them to Integers and calculating with them...
You can use : .equals(String s) to check if Strings are equal to others.

Customize editext input in android

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();
}
});

How can I implement digit grouping input mask using InputFilter?

I am using InputFilter class to make a masked EditText supporting digit grouping. For example when the user inserts" 12345" I want to show "12,345" in EditText. How can I implement it?
This is my incomplete code:
InputFilter IF = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
if (dest.length() > 0 && dest.length() % 3 == 0)
{
return "," + source;
}
return null;
}
};
edtRadius.setFilters(new InputFilter[] { IF });
Is there any other way to implement this kind of input mask?
This an improvement on the response from #vincent. It adds checks on deleting spaces in a number in the format 1234 5678 9190 so when trying to delete a space it just moves the cursor backon character to the digit before the space. It also keeps the cursor in the same relative place even if spaces are inserted.
mTxtCardNumber.addTextChangedListener(new TextWatcher() {
private boolean spaceDeleted;
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
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());
}
public void afterTextChanged(Editable editable) {
// disable text watcher
mTxtCardNumber.removeTextChangedListener(this);
// record cursor position as setting the text in the textview
// places the cursor at the end
int cursorPosition = mTxtCardNumber.getSelectionStart();
String withSpaces = formatText(editable);
mTxtCardNumber.setText(withSpaces);
// set the cursor at the last position + the spaces added since the
// space are always added before the cursor
mTxtCardNumber.setSelection(cursorPosition + (withSpaces.length() - editable.length()));
// if a space was deleted also deleted just move the cursor
// before the space
if (spaceDeleted) {
mTxtCardNumber.setSelection(mTxtCardNumber.getSelectionStart() - 1);
spaceDeleted = false;
}
// enable text watcher
mTxtCardNumber.addTextChangedListener(this);
}
private String formatText(CharSequence text)
{
StringBuilder formatted = new StringBuilder();
int count = 0;
for (int i = 0; i < text.length(); ++i)
{
if (Character.isDigit(text.charAt(i)))
{
if (count % 4 == 0 && count > 0)
formatted.append(" ");
formatted.append(text.charAt(i));
++count;
}
}
return formatted.toString();
}
});
In case you're still searching, I ran into this problem the last day, and found that using a TextWatcher is the best (still not really good) option. I had to group digits of credit card numbers.
someEditText.addTextChagedListener(new TextWatcher()
{
//According to the developer guide, one shall only edit the EditText's
//content in this function.
#Override
public void afterTextChanged(Editable text)
{
//You somehow need to access the EditText to remove this listener
//for the time of the changes made here. This is one way, but you
//can create a proper TextWatcher class and pass the EditText to
//its constructor, or have the EditText as a member of the class
//this code is running in (in the last case, you simply have to
//delete this line).
EditText someEditText = (EditText) findViewById(R.id.someEditText);
//Remove listener to prevent further call due to the changes we're
//about to make (TextWatcher is recursive, this function will be
//called again for every change you make, and in my experience,
//replace generates multiple ones, so a flag is not enough.
someEditText.removeTextChangedListener(this);
//Replace text with processed the processed string.
//FormatText is a function that takes a CharSequence (yes, you can
//pass the Editable directly), processes it the way you want, then
//returns the result as a String.
text.replace(0, text.length(), FormatText(text));
//Place the listener back
someEditText.addTextChangedListener(this);
}
#Override
public void beforeTextChaged(CharSequence s, int start, int count, int after)
{
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
});
My formatting function for the credit card numbers looked like this:
String FormatText(CharSequence text)
{
StringBuilder formatted = new StringBuilder();
int count = 0;
for (int i = 0; i < text.length(); ++i)
{
if (Character.isDigit(text.charAt(i)))
{
//You have to be careful here, only add extra characters before a
//user-typed character, otherwise the user won't be able to delete
//with backspace, since you put the extra character back immediately.
//However, this way, my solution would put a space at the start of
//the string that I don't want, hence the > check.
if (count % 4 == 0 && count > 0)
formatted.append(' ');
formatted.append(text.charAt(i));
++count;
}
}
return formatted.toString();
}
You might have to mind other issues as well, since this solution actually rewrites the EditText's content every time a change is made. For example, you should avoid processing characters you inserted yourself (that is an additional reason for the isDigit check).
use simple function:
public String digit_grouping(String in_digit){
String res = "";
final int input_len = in_digit.length();
for(int i=0 ; i< input_len ; i++)
{
if( (i % 3 == 0) && i > 0 )
res = "," + res;
res = in_digit.charAt(input_len - i - 1) + res;
}
return res;
}

Categories

Resources