Set addTextChangedListener in a function Android - android

I have 5 addTextChangedListener for 5 edit text .I want to customize them in a single function so that i have to just pass the id of the editttext and addTextChangedListener get applied on that.How could i do this i am not getthing this .Please help me on this .
Code
mobileNumber.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mobileNumber.setError(null);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
mobileNumber.setError(null);
}
});
mobileNumber2.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mobileNumber2.setError(null);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
mobileNumber2.setError(null);
}
});
mobileNumber1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mobileNumber1.setError(null);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
mobileNumber1.setError(null);
}
});
All addTextChangedListener are doing the same thing for the different id .So is this possible in android to customize it as a function and we have to just pass the id on that and addTextChangedListener will be applied on that
As suggest
public class BookingClassAddListenerOnTextChange implements TextWatcher {
private Context mContext;
EditText mEdittextview;
public BookingClassAddListenerOnTextChange(Context context, EditText edittextview) {
super();
this.mContext = context;
this.mEdittextview= edittextview;
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void addTextChangedListener(
BookingClassAddListenerOnTextChange bookingClassAddListenerOnTextChange) {
// TODO Auto-generated method stub
}
}

You should create a Listener class like so, Just modify the parameters in the constructor
to accept the EditText ID you want to add a listener to.
public class addListenerOnTextChange implements TextWatcher {
private Context mContext;
EditTextView mEdittextview;
public addListenerOnTextChange(Context context, EditTextView edittextview) {
super();
this.mContext = context;
this.mEdittextview= edittextview;
}
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//What you want to do
}
}
}
You call them like this in your activity
mobileNumber2.addTextChangedListener(new addListenerOnTextChange(this, mobileNumber2));
Again modify the parameters as needed.

If all the five behave in the same way, then you can create an object of TextWatcher and pass the same to all.
TextWatcher tw = new TextWatcher() {
/* code goes here */
/* viewOnFocus can be used here */
}
mobilenum1.addTextChangedListener(tw);
mobilenum2.addTextChangedListener(tw);
mobilenum3.addTextChangedListener(tw);
The viewOnFocus variable is set via an onFocusChange listener
OnFocusChangeListener of = new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
viewInFocus = view;
}
}
};
mobile1.setOnFocusChangeListener(of);
mobile2.setOnFocusChangeListener(of);
mobile3.setOnFocusChangeListener(of);

Related

Auto insert characters into an EditText

Is it possible to insert auto characters into an EditText? I have addTextChangedListener method. Input type is numberDecimal. I try to add 0 if input starting .(dot) I want to get like this result (0.4,0.3 etc). I wrote this code but it's not working.
countunicoinsSell.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) {
countCourse(s.toString());
}
});
private void countCourse(String value) {
if (value == null)
return;
if (value.startsWith(".")) {
countunicoinsSell.append("0");
}
}
How can I solve this problem?
This may work for your try it out!
etMobileNumber.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) {
// TODO Auto-generated method stub
if(!s.toString().contains("0.")){
etMobileNumber.setText("0.");
Selection.setSelection(etMobileNumber.getText(), etMobileNumber.getText().length());
}
}
});
if (value.startsWith(".")) {
countunicoinsSell.setText("0"+value);
}
Hope it help
private TextWatcher textWatcher = 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().startsWith(".")){
// append with 0
mEditText.setText("0" + mEditText.getText().toString());
// move the cursor to the end of the string
mEditText.setSelection(mETEmail.length());
}
}
};

android: How to replace a word with another text in edittext using addTextChangedListener?

I have an edittext. I want when the use type a sentence. After a space some words will be changed into another. I want to perform this using addTextChangedListener only.
I'm using the following code:
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) {
// TODO Auto-generated method stub
}
});
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable str) {
Editable ab = new SpannableStringBuilder(str.toString().replace("A", "a"));
editText.setText(ab);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
});
You can write a method like this. first check if user entered space. then split the word from spaces and replace your words.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(s.subSequence(s.length()-1, s.length()).length()==0)
{
replaceWord(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
}
});
private void replaceWord(String word)
{
String wordsList[] = word.split(" ");
//Replace words here
}

How can I control a EditText in Android to don't enter the same number twice?

I tried with Using TextChangedListener, beforeTextChanged and afterTextChanged! but I had no result
TextWatcher class can help you:
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int a, int b, int c) {
// TODO Auto-generated method stub
output.setText(s);
if(a == 9){
Toast.makeText(getApplicationContext(), "Maximum Limit Reached", Toast.LENGTH_SHORT).show();
}
}};
Complete reference could be found here.
this work for me :)
txtwt = new TextWatcher()
{
char last ;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
if (s.toString().length() >= 1)
EntredNumber = s.toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if (s.toString().length() >= 2)
{
last = (s.toString().charAt(s.toString().length() - 1));
// Toast.makeText(getApplicationContext(), last +"", Toast.LENGTH_SHORT).show();
if (EntredNumber.contains((last + "")))
{
txtImputNumber.removeTextChangedListener(txtwt);
txtImputNumber.setText(EntredNumber);
txtImputNumber.setSelection(EntredNumber.length());
txtImputNumber.addTextChangedListener(txtwt);
}
}
}
#Override
public void afterTextChanged(Editable s)
{
}
};
txtImputNumber.addTextChangedListener(txtwt);

android: making edittext not editable and then make it back editable again

I am trying to make edittext_2 not editable if edittext_1 is empty, but if edittext_1 is not empty I want edittext_2 to be editable.
I tried this code:
if (edittext_1.getText().toString().equals(""))
{
edittext_2.setEnabled(false);
}
else
{
edittext_2.setEnabled(true);
}
In the beginning the code works fine it makes edittext_2 not editable, but when I enter a value in edittext_1, edittext_2 still not editable.
You need to add edittext_1 on TextWatcher().
Try this
edittext_1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if (s.toString().equals("")) {
edittext_2.setEnabled(false);
} else {
edittext_2.setEnabled(true);
}
}
#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
}
});
Use this.
if (edittext_1.getText().equalsIgnoreCase(""))
{
edittext_2.setEnabled(false);
edittext_2.setFocusable(false);
}
else
{
edittext_2.setEnabled(true);
edittext_2.setFocusable(true);
}
Try this
editText2.setEnabled(false);
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (!editText1.getText().toString().equals("")) {
editText2.setEnabled(true);
} else {
editText2.setEnabled(false);
}
}
#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
}
});
What you have initialized will work for first time but when you make some changes in the edittext_1, you should again enable the edittext_2.Add the following code also,
edittext_1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (edittext_1.getText().toString().equals(""))
{
edittext_2.setEnabled(false);
}
else
{
edittext_2.setEnabled(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});

Lose Focus on Moving Next Text Field using Text Switcher

i m Using TextSwitcher to Move to Next Text Field Automatically it moves to the next Field perfectly
but when cursor moves to the next field it still focus the Previous textfield
Code for this is following
public void textWatcher(){
pin.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(pin.getText().toString().length()==1)
{
pin2.requestFocus();
pin.clearFocus();
}
}
#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
}
});
}
Any Suggestion help will be greatful
Regards
Usman Kurd
public void textWatcher(){
pPin1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
if(pPin1.getText().toString().length()==1)
{
pPin2.requestFocus();
}
// TODO Auto-generated method stub
}
});
}
Please try this code.. It will work perfect.
please use
pin2.setFocusable(true);
then it will work perfectly.
please try

Categories

Resources