I have a function that returns some String for letter 'A' when user chooses option 1 and some different String when user chooses option 2:
private String changeText(int option){
if(option==1)
return "Y";
if(option==2)
return "Z";
}
I want to replace the the character in EdittextView when the user selects option 1 and types 'A', replace A with "Y" and same for option 2 with "Z" and this to be done in real-time.
So I came up with TextWatcher.
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 0)
return;
s.replace(editText.getSelectionStart(),
editText.getSelectionStart()+1, changeText(option));
}
and it's not working. My guess is that I am trying to replace the character with newer one before it is printed(not sure). I just want to replace the last typed character at any cursor position according to the option selected.
Have you tried
beforeTextChanged(CharSequence s, int start, int count, int after)
in this method you can change symbol before in render. Get CharSequence s and change character at position (start, start+count)
Related
I want to allow user to enter only 10 characters inside the EditText. I tried two approaches.
Approach 1:
android:maxLength="10"
Approach 2:
I used InputFilter class.
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
When I am using these approaches, the cursor stops at 10 and no more characters are visible. However, it is still taking the characters I type after those 10 characters. I can see them in the suggestion area above keyboard. To explain clearly let me take an example.
Suppose I entered "abcdefghij", it works fine. Now, suppose I entered "abcdefghijklm", I can see only first 10 characters in the EditText but when press backspace it removes last character "m" instead of removing "j", the last character visible in EditText.
How can I solve this problem? I dont want to keep the extra characters in buffer also. So that when user presses backspace it should delete the 10th character.
You can use edittext.addTextChangedListener.
editTextLimited.addTextChangedListener(new TextWatcher() {
/** flag to prevent loop call of onTextChanged() */
private boolean setTextFlag = true;
#Override
public void afterTextChanged(Editable s) {
// 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 onTextChanged(CharSequence s, int start, int before, int count) {
// add your code here something like this
if(count > 10){
Toast.makeText(context,"10 chars allowed",Toast.LENGTH_LONG).show();
// set the text to a string max length 10:
if (setTextFlag) {
setTextFlag = false;
editTextLimited.setText(s.subSequence(0, 10));
} else {
setTextFlag = true;
}
}
}
});
Your problem should be solved by adding this to your EditText:
android:inputType="textFilter"
In my app I have to format the input of two EditTexts like:
1234 4567 67: Ten digits that grouped by four. (The space is automatically, not inserted by user)
11/14: Four digits that separated by '/'. (The '/' is inserted automatically)
I don't know how to do it. Please help:
Put a listener on the edit text as afterTextChanged.
Get the number of digits by using the length() function.
Once you get the number of digits, you can extract each digit and then insert space of '/' at the appropriate place.
len=editText.getText.toString().length();
then you can do the appropriate change by checking the length.
num=Integer.parseInt(editText.getText.toString());
temp=num;
if(len>=10)
{
A:
if((len-4)>0)
{
for(i=0;i<(len-4);i++)
{
temp=temp/10; //we get the first 4 digits
}
editText.setText(temp+" "); //place letters and add space
temp=num%(10^(len-4)); //get the num without the first-4 letters
len=len-4; //modify length
goto A; //repeat again
}
editText.setText(temp); //add the last remaining letters
}
else if(len==4)
{
temp=num;
temp=temp%100; //store the last 2 digits
num=num/10; //get the first 2 digits
editText.setText(num+"/"+temp);
}
i havnt tried this but i think this will work.
Hope this will help. :)
I can think of two ways of achieving it:
Use addTextChangedListener:
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Do your tricks here
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
Create custom Edittexts
This link wont do what you are looking for, but will give you an idea how to create custom EditText.
Use "onKeyListener" to get the input event.
EditText OnKeyDown
Then check for correct input and count the digits. Add the whitespace/slash in your code.
Sample code:
if (editText.getText.toString().length() % 4 == 0) editText.setText(editText.getText.toString() + " ");
Didn't try it by myself, but this would be the way i would try. In addition i would check for numeric input too.
In my app, I want the users to give their answers in the form of text through edit text. So for the correct answer I want the letters to turn green (or red for incorrect) on the fly while typing.
For example, if the answer is DOG, I want the the text to turn green if the user types DOG dynamically. Even if the the first letter he types is D then I want the text color to be green. Only when the user's input text is not correct do I want it to be red. The text color should change on the fly while typing.
Create EditText and call addTextChangedListener for it supplying custom TextWatcher where you mostly need to override its onTextChanged.
In this method change your text color according to your logic.
Snapshot :
mEditBox = (EditText) findViewById(R.id.my_edit_box_id);
mEditBox.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
String currentText = mEditBox.getText().toString();
// highligt correct answer in green
if ("DOG".startsWith(currentText)) { // user starts typing "DOG"
mEditBox.setTextColor(Color.GREEN);
} else {
mEditBox.setTextColor(Color.RED); // incorrect input
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
I need to control pressed buttons, before they goes to my EditText widget. It's should work like filter.
For example: I need that user could fill EditText only with digits 1,2,3,4,5 other symbols must be ignored. So the part of buttons on virtual keyboard should be disabled or I need to catch last pressed symbol, analyze it and disable for EditText.
Who knows the way how to solve this problem?
Thanks..
statusEdt.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) {
//do stuff
charTxt.setText(statusEdt.getText().length() + "/140");
}
});
I used this TextChangedListener(TextWatcher) to keep a count of how many characters had been typed into an EditText for a Twitter client I made. You could probably use a listener like this. You'll want to override beforeTextChanged or onTextChanged. These methods will pass you whatever CharSequence has been typed. You can check what was typed in and if it is not valid input you can remove it by calling setText() and passing in whatever has been typed so far minus the invalid characters.
What you probably need is an InputFilter.
For example to allow only digits, sign and decimal point:
editText.setFilters(DigistKeyListener.getInstance(true, true));
I am working on softkeyboard.
My issues are below.
How to get current position of cursor in text(EditText).
How to get total length of value in text(EditText).
If EditText is multi-line then get current line of cursor in text(EditText).
If you want see my code then see this softkeyboard's link. I am following this code.
You should put textwatcher event in edittext this is the event is execute when user type a character (any in put by key board).
In your case when user type a single character in edittext you got hole text then get length of this text it is your cursor position and total length of value in text.
according to your third question you have all the text written in edit text using above method then you convert all the text in ascii value then compare every character with 13(it is the ascii value of enter in keyboard )and increase counter of line when it condition true using this you find no of line in edit text. i am giving a example for you how to put text watcher in edittext you change in this code and convert it according to your condition.
ed.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) {
textlength = ed.getText().length();
);
}
});