I'm new to Android development and I want to create a method that runs every time my editText is changed.
In swift I use this function:
func fieldChanged(textfieldChange: UITextField){
}
I can't find anywhere a similar function for Java and Android Studio, are there any suggestions?
// Globally declare a variable
private TextWatcher mTextWatcher;
// Call textChanger() method in onCreate
private void textChanger() {
mTextWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() == 6 && mEditText.getText().toString().trim().equals("some value to check"))
mEditTextOther.requestFocus();
else
mEditText.setError("enter correct");
}
#Override
public void afterTextChanged(Editable s) { }
};
mEditText.addTextChangedListener(mTextWatcher);
}
try this
editText.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) {}
});
Related
I am trying to implement something similar to a code editor where keywords are automatically highlighted. I am going to have a string array and I want to change the color and font of the editText string when the user types the text and it matches a string from the string array. I am using the addTextChangeListener but the text of the whole editText changes. I want just the matched word to be highlighted. Here is my code:
inputCodeEditText.addTextChangedListener(new TextWatcher() {
#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) {
if (s.toString().contains("for"))
{
inputCodeEditText.setTextColor(getResources().getColor(R.color.indigo));
}
}
});
I understand I have to use spans but the code crashes. Can anyone help me with the correct usage of spannable strings with addTextChangedListener() ?
Use addTextChangeListener API eg:
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// The changed text comes in "s" parameter.
// Here you can watch the changes and take an action you want...
}
#Override
public void afterTextChanged(Editable s) {
}
});
I am doing a chat app and got stocked on how to make textWatcher which will push on the firebase data structure under user-typing
. I want to push a data structure wherein on the data structure you will see if the user is typing. when the user is typing the data structure under user-typing is true. if the user is not typing then it will become false. i tried this code but it seems wrong because every time i run the program. When I click the ediText. it will automatically make a data structure key for a user
final Firebase test = firebase.child("room-typing").push();
test.setValue("true");
final EditText editText = (EditText) findViewById(R.id.editText);
editText.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 == editText) {
firebase.child("room-typing").child(test.getKey()).child("test").setValue("true");
} else {
firebase.child("room-typing").child(test.getKey()).child("test").setValue("false");
}
}
});
You're creating that new key yourself, by calling push(). From the documentation for push():
Generates a new child location using a unique key and returns a Firebase reference to it.
I added some comments to you code, to mark where things happen:
// This next line creates a new key
final Firebase test = firebase.child("room-typing").push();
test.setValue("true");
final EditText editText = (EditText) findViewById(R.id.editText);
editText.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 == editText) {
// This next line uses the new key you created above
firebase.child("room-typing").child(test.getKey()).child("Onediver").setValue("true");
} else {
// As does the next line here
firebase.child("room-typing").child(test.getKey()).child("Onediver").setValue("false");
}
}
});
To prevent the creation of a new child, you should not call push, but depend on a known child, such as:
final Firebase test = firebase.child("room-typing").child("jaymee");
There are 2 ways you can do this. An easy way and there is an elegant way.
For elegant way look into: http://reactivex.io/documentation/operators/debounce.html
Here's the easy way:
Create somewhere a custom countdown timer:
public class MyCountDownTimmer extends CountDownTimer {
public MyCountDownTimmer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override public void onTick(long l) {
}
#Override public void onFinish() {
databaseReference.child("room-typing").child(room_name).child(user_id_or_name).setValue("false");
isTyping = false;
}
}
Declare your countdown timer
private MyCountDownTimmer isTypingTimmer = new MyCountDownTimmer(1000, 1000);
private boolean isTyping = false;
editText_message.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) {
isTypingTimmer.cancel();
isTypingTimmer.start();
if (!isTyping) {
databaseReference.child("room-typing").child(room_name).child(user_id_or_name).setValue("true");
isTyping = true;
}
}
});
I tried the following code, but android doesn't let me do that, as I'd enter into an infinite loop.
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.addTextChangedListener(new TextWatcher(){
#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) {
//I want to format the already existing text in s in a certain way as the user is typing
mEditText.setText(s.toString() + " \n");
}
});
Please any ideas would be very helpful. Thanks
3 possible solutions :
Check for the carriage return presence in the string
Like this :
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.addTextChangedListener(new TextWatcher()
{
#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)
{
//I want to format the already existing text in s in a certain way as the user is typing
if (s.toString().charAt(s.length() - 1 ) != '\n')
{
mEditText.setText(s.toString() + " \n");
}
}
});
Not the best solution, according to what's in your input string
Do it in beforeTextChanged
Like this :
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.addTextChangedListener(new TextWatcher()
{
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
//I want to format the already existing text in s in a certain way as the user is typing
mEditText.setText(s.toString() + " \n");
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void afterTextChanged(Editable s) {}
});
Cancel the listener, temporarily
Like this :
final TextWatcher tw = new TextWatcher()
{
#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)
{
//I want to format the already existing text in s in a certain way as the user is typing
mEditText.removeTextChangedListener(tw);
mEditText.setText(s.toString() + " \n");
mEditText.addTextChangedListener(tw);
}
});
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.addTextChangedListener(tw);
You are most likely entering an infinite loop because you are changing the text in public void afterTextChanged(Editable s) which then causes the method to be called again.
The best solution would be to change the text in one of the other methods, or on the loss of focus of the editable. See View.OnFocusChangeListener for more details on this.
Here is an example:
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
textView.setText(s.toString() + " \n");
}
}
});
I have a number of EditText's with an inputType="numberDecimal" and i'm wanting to change the input into currency (TextWatcher maybe?). Can anyone help with this? 99% of my project is done bar this. Thanks guys.
try below code:-
tv.addTextChangedListener(new TextWatcher()
{
private String current = "";
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if(!s.toString().equals(current))
{
if(tv.getText().toString().trim().length()>0)
{
tv.removeTextChangedListener(this);
String formated = tv.getText().toString().trim().replace("$", "");
current = formated;
tv.setText("$"+formated);
tv.setSelection(formated.length()+1);
tv.addTextChangedListener(this);
}
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void afterTextChanged(final Editable s)
{
}
});
I'm trying to get what modification user has made to EditText, either insert or delete. I use TextWatcher but I don't get right result, moreover sometimes "getChar(start, end) has end before start" error.
editText = (EditText) findViewById(R.id.MyEditText);
editText.addTextChangedListener(new TextWatcher() {
#override
public void afterTextChanged(Editable s){}
#override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
showToast("text removed: " + s.subSequence(start, count));
}
#override
public void onTextChanged(CharSequence s, int start, int before, int count){
showToast("text added: " + s.subSequence(start, count));
}
}
As you can see I use beforeTextChanged to get any text that's removed by user, and onTextChanged for insertion. Please shed some light here. Thanks!
API is right here: http://developer.android.com/reference/android/text/TextWatcher.html#afterTextChanged(android.text.Editable)
EDIT:
I seem to figure it out...it's quite silly: s.subSequence(start, count)) should really be s.subSequence(start, start+count))
Just keep your functions inside the afterTextChanged and see what happens
Sample Code
seachbox.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
fillData(SEARCH_ORDER ,s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
});
I hope it will work
Try this, I'm not sure whether you want the remaining word(after insert/update) or the letter(added/removed).
public class MainActivity extends Activity implements TextWatcher {
private EditText myEditText;
private String inputText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myEditText = (EditText) findViewById(R.id.testEditText);
myEditText.addTextChangedListener(this);
}
#Override
public void afterTextChanged(Editable s) {
if (inputText.length() < s.toString().length()) {
Toast.makeText(
this,
("Text Added: " + s.toString().substring(inputText.length(),
s.toString().length())), Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(
this,
("Text Removed: " + inputText.substring(s.toString().length(),
inputText.length())), Toast.LENGTH_SHORT).show();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
inputText = s.toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}