TextWatcher - Increase value of last char by one - android

What I am trying to do is this: User enters text in an edittext. Example: "ab" then when he enters a character like '<' let's say, that would cause the last char typed before that to be increased by one. In this example, it'd do "ac" ('<' should be deleted too). On onTextChanged method, I use:
if (s.length() > 0 && s.toString().charAt(s.length() - 1) == '<') { //ab<
character = s.toString().charAt(s.length() - 2); //keep b
current_string = s.toString().substring(0, (s.length() - 2)); //a
et.setText(current_string); //set a
next_character_to_integer = (int) character + 1; //b is 98 ascii, need 99
integer_to_character = (char) next_character_to_integer; // 99 = char c
et.setText(s + String.valueOf(integer_to_character)); //set a+c
length = s.length();
et.setSelection(length); //move cursor after c
}
But this gives "abqc"!!! I might be close enough but still can't find the solution. Any ideas? Thanks a lot

Try this:
if (s.length() > 0 && s.toString().charAt(s.length() - 1) == '<') {
character = s.toString().charAt(s.length() - 2);
current_string = s.toString().substring(0, (s.length() - 2));
character += 1;
et.setText(current_string + character);
length = et.getText().toString().length();
et.setSelection(length);
}
This should work.

When you call:
et.setText(s + String.valueOf(integer_to_character)); //set a+c
you are using the original value of s which you have not updated. Change the line to:
et.setText(current_string + String.valueOf(integer_to_character)); //set a+c
and see what you get.

Related

EditText number validation in Android

I have three EditText controls and I need to make sure that each is the correct number input.
The first has to be a number between 0 and 23
The second has to be a number between 0 and 59
And the third has to be a number between 0 and 1500
I saw someone make a post about an easy EditText validation using setError, example:
EditText firstName = (EditText)findViewById(R.id.first_name);
if (firstName.getText().toString().length() == 0)
firstName.setError("First name is required!");
So is there an easy way to do it like above, but making sure a number isn't > 23, 59, or 1500 (individually)?
EditText firstEditText = (EditText)findViewById(R.id.first_edit_text);
EditText secondEditText = (EditText)findViewById(R.id.second_edit_text);
EditText thirdEditText= (EditText)findViewById(R.id.third_edit_text);
int value;
value = Integer.parseInt(firstEditText .getText().toString());
if (! value > 0 && value < 23)
firstEditText.setError("Error");
value = Integer.parseInt(secondEditText .getText().toString());
if (! value > 0 && value < 59)
secondEditText.setError("Error");
value = Integer.parseInt(thirdEditText.getText().toString());
if (! value > 0 && value < 1500)
thirdEditText.setError("Error");
Integer.parseInt(String s)can convert string to integer.Then, you validate it with if, else.
However, the input may not be an integer, and you have to set android:numeric="integer" in <EditText /> tag in the .xml file.
Be careful about this, if the input string is not integer, Integer.parseInt() will throw an exception, which will cause a crash.
You want to make sure you're performing some error checking since the value is coming from the user. Try something like this:
EditText firstText = (EditText) findViewById(R.id.first);
try
{
long firstVal = Long.parseLong(firstText.getText());
if (firstVal < 0 || firstVal > 23)
firstText.setError("The value must be between 0 and 23!");
}
catch (NumberFormatException e)
{
firstText.setError("Enter an integer value!");
}
// very similar for your remaining cases
String value = firstName.getText().toString();
int int_value = Integer.parseInt(value);
if (firstName.getText().toString().length() != 0)
if (int_value < 1500) {
// do what you want
}
else if (int_value < 59) {
// do what you want
}
else if (int_value < 23) {
// do what you want
}
}else {
firstName.setError("First name is required!");
}

String Multiline - Android

i got this issue and i don't know how to solve it. Here is the problem:
1 - i have a data in my database who i split into a strings[] and then i split this strings[] into another 2 strings[] (even and odd lines). Everything works fine but when i want to join all the lines into a single String i got a multi line string intead of a single line. Someone can help me?
data
abcdef//
123456//
ghijkl//
789012
code:
String text = "";
vec1 = data.split("//"); //split the data
int LE = 0;
for (int a = 0; a < vec1.length; a++) { //verify how many even and odds line the data have
if (a % 2 == 0) { //if 0, LE++
LE++;
}
}
resul1 = new String[LE];
int contA = 0, contB = 0;
for (int c = 0; c < resul1.length; c++) {
if (c % 2 != 0) {
text += " " + resul1[c].toLowerCase().replace("Á","a").replace("Ã","a").replace("ã","a").replace("â","a").replace("á","a").replace("é","e").replace("É","e")
.replace("ê","e").replace("í","i").replace("Í","i").replace("ó","o").replace("Ó","o").replace("õ","o").replace("Õ","o").replace("ô","o").replace("Ô", "o")
.replace("Ú","u").replace("ú","u").replace("ç","c").replace("_","").replace("<","").replace(">","");
contA++;
}
}
And the String looks like
abcdef
ghijkl
instead of
abcdefghijkl
You should use replaceAll() method.
text.replaceAll("\\r\\n|\\r|\\n", ""); // the method removes all newline characters

Check length of a String in Android

I want to check whether the entered strings length is between 3 to 8 characters. Previously I used if condition and it worked. However when I introduced some substring from the string , one of the if statements doesnt work. Can some one help me to understand why. Thanks.
My codes is
Working Code:
text = et.getText().toString();
l = text.length();
a = text.substring(0, 1);
if (l >=9) tv.setText("Invalid length!!! Please check your code");
if (l <= 2) tv.setText("Invalid length! Please check your code");
And here, the second if statement doesnt work.
text = et.getText().toString();
l = text.length();
a = text.substring(0, 1);
c = text.substring(1, 2);
d = text.substring(3, 4);
e = text.substring(4);
if (l >=9) tv.setText("Invalid length!!! Please check your code");
if (l <= 2) tv.setText("Invalid length! Please check your code");
You will want to ensure that you handle a null string as well as ensuring your string is within the limits you want. consider:
text = et.getText().toString();
if (text == null || text.length() < 3 || text.length > 8) {
tv.setText("Invalid length, should be from 3 to 8 characters. Please check your code");
} else {
a = text.substring(0,1);
b = text.substring(1,2);
c = text.substring(3,4);
if (text.length() > 3) {
d = text.substring(4);
} else {
d = null;
}
}
You need to check the length before trying to create substrings, since if the length is too short the substring indexes are invalid. Try this:
text = et.getText().toString();
l = text.length();
if (l >= 9 || l <= 2) {
tv.setText("Invalid length!!! Please check your code");
} else {
a = text.substring(0, 1);
c = text.substring(1, 2);
d = text.substring(3, 4);
e = text.substring(4);
}
You can use like this:
editText.getText().toString().length() < 3
EditText etmobile_no;
if (etmobile_no.getText().toString("") ||
etmobile_no.getText().toString().length() <3 ||
etmobile_no.getText().toString().length() >8)
{
tv.setText("Invalid length, should be from 3 to 8 characters. Please check your code");
}

setSelectionRange workaround doesn't work for android 4.0.3

I am trying to mask the phone number as the user types. I have used the javascript code below with jquery and the setTimeout workaround successfully on android 2.x devices, but I have not found a workaround for that works for android 4.0.3.
if (navigator.userAgent.toLowerCase().indexOf("android") >= 0) {
$.fn.usphone = function() {
this.keyup(function(e) {
// do not process del, backspace, escape, arrow left and arrow right characters
var k = e.which;
if (k == 8 || k == 46 || k == 27 || k == 37 || k == 39)
return;
// remove invalid characters
var value = "";
for (var i = 0; i < this.value.length; i++) {
var ch = this.value[i];
if (ch >= "0" && ch <= "9")
value += ch;
}
// remove extra characters
if (value.length > 10)
value = value.substring(0, 10);
// insert formatting characters
if (value.length >= 3)
value = "(" + value.substring(0, 3) + ")" + value.substring(3);
if (value.length > 5)
value = value.substring(0, 5) + " " + value.substring(5);
if (value.length > 9)
value = value.substring(0, 9) + "-" + value.substring(9);
// set new value
var $this = this;
var length = value.length;
setTimeout(function() {
$this.value = value;
$this.setSelectionRange(length, length);
}, 0);
});
};
$('#contact_edit_page, #contact_new_page, #callback_create, #callback_edit, #new_phonecall_contact_page, #new_phonecall').live('pagecreate', function() {
$('[type^="tel"]').usphone();
});
}
I just met the same problem. My solution is as follows,
.input {
-webkit-user-modify: read-write;
}
It works in android 4.0.3 in my HTC.

android app Search operation with broken words

Hi i am doing a search operation in the app using a specific keyword and listing the result in a list view activity. The result will be displayed with 50 characters before and after the searched keyword but when i take 50 characters it breaks the words and in the result half words or single letters are displayed in the beginning and after. How could i ignore and get complete words
Below is my code. please do help me on this. thanks in advance
int endindex = searchTextStartIndex + searchTextLength + 50;
if (searchTextStartIndex > 50) {
startindex = searchTextStartIndex - 50;
}
if (endindex > datalength) {
endindex = datalength;
}
searchdata = searchdata.substring(startindex, endindex);
} catch (Exception e)
Assuming a space as word seperator:
if (searchTextStartIndex > 50) {
startindex = searchdata.lastIndexOf( " ", (searchTextStartIndex - 50) );
}
if (startindex == -1) startindex = 0;
int endindex = searchdata.indexOf( " ", (searchTextStartIndex + searchTextLength + 50) );
if (endindex == -1 || endindex > datalength) {
endindex = datalength;
}
searchdata = searchdata.substring(startindex, endindex);

Categories

Resources