Android EditText when a text entered - android

I'm quite new in android and I don't know if it's possible that when user enters sth in edittext it trigers another method.
like
EDİT When I run this in onCrate it crashes
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
anyStringEntered();
}
my methods that checks if there is any change on Edittext
public void anyStringEntered(){
userName = (EditText) findViewById(R.id.userNameTextBox);
userPassword = (EditText) findViewById(R.id.userPasswordTextBox);
rememberMe= (CheckBox) findViewById(R.id.checkBox1);
userName.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(!userPassword.getText().toString().equals("") && !userName.getText().toString().equals("")){
rememberMe.setEnabled(true);
}else{
rememberMe.setEnabled(false);
}
}
});
userPassword.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(!userPassword.getText().toString().equals("") && !userName.getText().toString().equals("")){
rememberMe.setEnabled(true);
}else{
rememberMe.setEnabled(false);
}
}
});
}

Use the TextWatcher:
edittext.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
//method1()
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
//method2()
}
public void onTextChanged(CharSequence s, int start, int before, int count){
//method3()
}
});

It is what you are looking for ?
editTextLogin.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) {
if(edittext.gettext()!="")
{ myMethod(); }
else {
myMethod2();
}
}
};

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

How to update the two Edittext parralley using Textwatcher in android?

public class SampleActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datepicker);
final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
text1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
int total = Integer.parseInt(s.toString()) + 1;
text2.setText(String.valueOf(total));
}catch (Exception e){
e.printStackTrace();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
#Override
public void afterTextChanged(Editable s) {
}
});
text2.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
int total = Integer.parseInt(s.toString()) + 2;
text1.setText(String.valueOf(total));
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
Hi I am facing one problem with multiple edit text updating parallel.If i enter value in first edit-text, the second edit text changed.Same in second edittext. But the key listener of cursor in edit-text has problem. i am unable to get the cursor.How to solve this problem?
I don't get what you are doing (or trying to do).
In the onTextChanged for text1 you are triggering the onTextChanged for text2. Seems like infinite text change listeners being triggered here.
The logic for what is happening ->
Suppose the user types in the character 1 into text1. total is now 2. Now you set text2 as total which is 2. This in turn triggers the Text Change Listener for text2. Now total is 2+2=4. You now set 4 as the text for text1. This in turn triggers the Text Change Listener for text1.
Seems like a never ending infinite loop to me. This is the reason you are getting a StackOverflowError (lol) What did you actually want to do?
Not really sure about what might solve that problem (I don't have a working copy of Android Studio at hand), but moving the block to afterTextChanged might do it.
You can try this way. TextWatcher will update the values to second EditText through Handler through the method afterTextChanged.
EditText text1;
StringBuffer previousChar=new StringBuffer();
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
text1=(EditText)findViewById(R.id.editText);
text1.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(!previousChar.toString().equals(s.toString())){
Message msg=new Message();
msg.obj=s.toString();
localHandler.sendMessage(msg);
previousChar=new StringBuffer(s.toString());
Log.i("TAG", "TextEntered = "+s);
}
}
});}
The handler will be used to update the text in second EditText
private Handler localHandler = new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
String value=(String)msg.obj;
text2.setText(value);
}
};
includeTW = new TextWatcher(){
#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) {
if(!(includedText.getText().toString().equals("")))
{
excludedText.removeTextChangedListener(excludeTW);
String included = includedText.getText().toString();
excludedText.setText(included); //////// Error Line
excludedText.addTextChangedListener(excludeTW);
}
}
};
excludeTW = new TextWatcher(){
#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) {
if(!(excludedText.getText().toString().equals(""))) {
includedText.removeTextChangedListener(includeTW);
String excluded = excludedText.getText().toString();
includedText.setText(excluded);
includedText.addTextChangedListener(includeTW);
}
}
};
includedText.addTextChangedListener(includeTW) ;
excludedText.addTextChangedListener(excludeTW);
This will give the solution exactly what i want.I am getting the value with the cursor moving in each edittext.
Remove TextChangeListener of another EditText when first one is in effective mode and add that after text change..
public class SampleActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datepicker);
final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
text1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
**text2.addTextChangedListener(null);**
try {
int total = Integer.parseInt(s.toString()) + 1;
text2.setText(String.valueOf(total));
}catch (Exception e){
e.printStackTrace();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
#Override
public void afterTextChanged(Editable s) {
**text2.addTextChangedListener(this);**
}
});
text2.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
text1.addTextChangedListener(null);
try {
int total = Integer.parseInt(s.toString()) + 2;
text1.setText(String.valueOf(total));
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s)
{
**text1.addTextChangedListener(this);**
}
});
}
}

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
}

Add dashes after x digits in Edittext

I am new on android and want to when i am entering cnic in edittext then after some digit i can put dash on it like:
12221-1338888-3
et_cnic = (EditText) findViewById(R.id.et_cnic);
How i can achieve this?please help.
Try this
edt.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) {
String str = s.toString();
if(s.length() == 5 || s.length() == 13){
str += "-";
edt.setText(str);
edt.setSelection(str.length());
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et_cnic .addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(String.valueOf(et_cnic.getText()).length>X)//here x is the number of character after which "." will display on edit text
{
et_cnic.setText(String.valueOf(et_cnic.getText).substring(0,X)+"*")//x here same the number of charcter which you do not want to replace with"."
}
// TODO Auto-generated method stub
// Here you have to write your code for adding dashes.
}
#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
}
});
String content="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText edText = (EditText)findViewById(R.id.edText);
edText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if(s.length()%4 == 0){
int starting = content.length()==0?0:content.length();
content = content+s.subSequence(starting, start)+"-";
}
}
#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
}
});
}
Try this:
int x = 2;
edt = (EditText) findViewById(R.id.edt_test);
edt.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) {
if (s.toString().length() == x) {
edt.setText(s + "-");
edt.setSelection(s.toString().length() + 1);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
int a = 0; // write this before on create
#Override
public void afterTextChanged(Editable s) {
if(s.length()==4 && a == 0){ // this logic will allow you to delete the "-" as well
number.setText(s+"-");
number.setSelection(s.length()+1);
a=1;
}
else if(s.length()<4 && a==1){
a=0;
}
}

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

Categories

Resources