Check if EditText is empty in real time - java

In one activity of my app I have 5 EditText fields. However, I want etPlayer5 only to be enabled when the other 4 EditTexts are not empty. I wrote this little code to achieve this:
etPlayer1 = (EditText) findViewById(R.id.etPlayer1);
etPlayer2 = (EditText) findViewById(R.id.etPlayer2);
etPlayer3 = (EditText) findViewById(R.id.etPlayer3);
etPlayer4 = (EditText) findViewById(R.id.etPlayer4);
etPlayer5 = (EditText) findViewById(R.id.etPlayer5);
if (namePlayer1.matches("")||namePlayer2.matches("")||namePlayer3.matches("")||namePlayer4.matches("")) {
etPlayer5.setEnabled(false);
etPlayer5.setFocusable(false);
}
else {
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
Of course this code doesn't work in real time. What do I need to do that when the last of the EditTexts 1 to 4 is filled in, etPlayer5 is automatically set enabled without using a button or something?
Thank you!

You can have textChangeListener with all 4 Edittexts and keep boolean against all edittext. Update booleans to true when you get call of onTextChange and see if the string length is one in this edittext and further check remaining booleans are also true.This is the trick. Now this is the time to make the edittext enable.Hope that helps.Below is the code for above explanation
public class MainActivity extends AppCompatActivity {
EditText et1, et2, et3, et4, et5;
boolean firsEditText, secondEditText, thirdEditText, fourthEditText, fifthEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
et3 = (EditText) findViewById(R.id.et3);
et4 = (EditText) findViewById(R.id.et4);
et5 = (EditText) findViewById(R.id.et5);
et1.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() > 0) {
firsEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
firsEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et2.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() > 0) {
secondEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
secondEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et3.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() > 0) {
thirdEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
thirdEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et4.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() > 0) {
fourthEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
fourthEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
private boolean checkAllTrue() {
if (firsEditText && secondEditText && thirdEditText && fourthEditText)
return true;
return false;
}
}

You could do something like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean player1Changed = false, player2Changed = false;
setContentView(R.layout.main);
etPlayer1 = (EditText) findViewById(R.id.etPlayer1);
etPlayer2 = (EditText) findViewById(R.id.etPlayer2);
etPlayer5 = (EditText) findViewById(R.id.etPlayer5);
etPlayer1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
player1Changed = true;
if(player1Changed && player2Changed){
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
etPlayer2.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
player2Changed = true;
if(player1Changed && player2Changed){
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}

Related

how can add integer from edittext to array In android?

I am using java to code a small program. this program will take data from the keyboard and check the parity of that sequence and this is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edso = findViewById(R.id.so);
btnchan = findViewById(R.id.btnchan);
btnle = findViewById(R.id.btnle);
tvkq=findViewById(R.id.tvkq);
String n=edso.getText().toString();
List<Integer> arr = new ArrayList<Integer>();
arr.add(Integer.parseInt(n));
Integer[] array = arr.toArray(new Integer[5]);
btnchan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i=0;i< array.length;i++){
if (array[i]%2==0){
tvkq.setText("chăn"+array[i]);
}
}
}
});
btnle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i=0;i< array.length;i++){
if (array[i]%2!=0){
tvkq.setText("lẽ"+array[i]);
}
}
}
});
}
help me
i want to use data from edittext to check and display it in textview
int num = Integer.parseInt(mEditText.getText().toString());
arr.add(num);
or
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) {
int num = Integer.parseInt(s.toString());
arr.add(num);
}
#Override
public void afterTextChanged(Editable s) {
}
});

EditText On update/onChange value Add listener

I have 2 EditText in my activity,lets say editText1 ,editText2 and A Double Variable d=200
I want when user change/insert value for editText1 ,editText2 value will update in real Time as editText1*d
Also when user change/insert value for editText2 ,editText1 will be update in real time as editText2*d
I tried to use addTextChangedListener->onTextChangedbut it works fine for one Edit text,when I set this function for both editText then application crash
because its creating an infinite loop,how can I solve this problem?
update :bellow is my code
et1.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) {
Editable a = et1.getText();
Double aa = Double.parseDouble(a.toString())*100;
String aaa = aa.toString();
et2.setText(aaa);
}
#Override
public void afterTextChanged(Editable s) {
}
});
et2.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) {
Editable b=et2.getText();
Double bb=Double.parseDouble(b.toString());
String bbb=bb.toString();
et1.setText(bbb);
}
#Override
public void afterTextChanged(Editable s) {
}
});
EditText et1, et2;
TextWatcher watcher1 = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Editable a = et1.getText();
Double aa = Double.parseDouble(a.toString())*100;
String aaa = aa.toString();
et2.removeTextChangedListener(watcher2);
et2.setText(aaa);
et2.addTextChangedListener(watcher2);
}
#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
}
};
TextWatcher watcher2 = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Editable b=et2.getText();
Double bb=Double.parseDouble(b.toString());
String bbb=bb.toString();
et1.removeTextChangedListener(watcher1);
et1.setText(bbb);
et1.addTextChangedListener(watcher1);
}
#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
}
};
et1.addTextChangedListener(watcher1);
et2.addTextChangedListener(watcher2);
i think you have to create textwatcher separately then in your TextWatcher_EdOne
TextWatcher TextWatcher_EdOne = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//disable editext2 textwatcher
et2.removeTextChangedListener(TextWatcher_EdTwo);
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
///do your stuff
}
#Override
public void afterTextChanged(Editable s) {
// enable textwatcher on et2 again
et2.addTextChangedListener(TextWatcher_EdTwo);
}
}
and will do it for the other textWatcher , this way you will avoid infinite loop
You should implement a TextWatcher, It works pretty much like how it sounds. When the user changes (adds/removes text from) what is existing in the EditText, it will "Activate" this class. To test it out, you can use logs or set a breakpoint at any one of these statements.
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Your action here
edit = fromEditText.getText().toString();
}
#Override
public void afterTextChanged(Editable s) {
}
}
and in onCreate, add this to connect your EditText to the TextWatcher
fromEditText.addTextChangedListener(new MainActivity.MyTextWatcher(fromEditText));
You can add a bunch of EditText's to a single TextWatcher. All you need to do is rinse and repeat. Just change the name of the respective EditText and add it to the TextWatcher. Be aware, that if you have some EditText change the text of another EditText, it can result in an infinite loop.

SwiftKey keyboard and SpannableStringBuilder with end space character issue

I have the strange problem when using SwiftKey Keyboard if I passed a string from EditText to SpannableStringBuilder and set it back to EditText; any Space character at the end will be trimmed automatically, using default keyboard this not happen here the code:
public class MainActivity extends AppCompatActivity implements TextWatcher {
EditText txtEntry;
boolean IsChanged = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtEntry = (EditText) findViewById(R.id.txtEntry);
txtEntry.addTextChangedListener(this);
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if (IsChanged)
return;
IsChanged = true;
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(txtEntry.getText());
txtEntry.setText(sb);
txtEntry.setSelection(txtEntry.getText().length());
IsChanged = false;
}
}
I m doing this as I,m adding dynamic emotions to EditText, but can't add spaces so I made a very simple code to discover the issue.
Ok i fixed the problem, first of all must add inputType="textNoSuggestions" to EditText; this will prevent firing OnTextChanged Event twice, second the processing of string should be throw Editable; not throw EditText itself
Here is the code :
public class MainActivity extends AppCompatActivity implements TextWatcher{
EditText txtEntry;
boolean IsChanged = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtEntry = (EditText) findViewById(R.id.txtEntry);
txtEntry.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
txtEntry.addTextChangedListener(this);
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String ss = "";
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if (IsChanged)
return;
IsChanged = true;
String sInput =editable.toString().replace(" ","#space#");
SpannableStringBuilder ObjBuilder = new SpannableStringBuilder();
ObjBuilder.append(sInput);
while (true)
{
int iStartIndex = sInput.indexOf("#space#");
int iEndIndex = iStartIndex + "#space#".length();
if (iStartIndex == -1)
break;
sInput = sInput.replaceFirst("#space#","[space]");
Drawable ObjDraw = getResources().getDrawable(R.drawable.space);
ObjDraw.setBounds(0,0,10,60);
ImageSpan Image = new ImageSpan(ObjDraw);
ObjBuilder.setSpan(Image,iStartIndex,iEndIndex,0);
}
editable.clear();
editable.append( ObjBuilder);
txtEntry.setSelection(txtEntry.getText().length());
IsChanged = false;
}
}

Issues in EditText

My issue is when first EditText length greater than 10 then second EditText will be enabled and second EditText length greater than 8 then after Button will be enabled what is the problem in my code? Please any one can solve this problem.
public void loginPage(View v) {
editText = (EditText) findViewById(R.id.username);
editText1 = (EditText) findViewById(R.id.pasword);
username = editText.getText().toString();
password = editText1.getText().toString();
btn = (Button) findViewById(R.id.login1);
editText1.setEnabled(false);
btn.setEnabled(false);
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) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() >= 10) {
editText1.setEnabled(true);
btn.setEnabled(false);
}
}
});
editText1.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.length() >= 8) {
btn.setEnabled(true);
}
}
});
}
Use this for the equal or greater than 10 word
paste this method in OnCreate() method
editText.addTextChangedListener(new TextWatcher() {
#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) {
if (s.length() >= 10)
editText1.setEnabled(true);
else
editText1.setEnabled(false);
}
});
and for the button for 8 words
paste this method in OnCreate() method
editText1.addTextChangedListener(new TextWatcher() {
#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) {
if (s.length() >= 8)
btn.setEnabled(true);
else
btn.setEnabled(false);
}
});
enjoy coding .........
You need to use Text Watcher with Edit Text.
Try this code.
public class Login extends AppCompatActivity {
EditText editText, editText1;
Button btn;
String username, password;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
loginPage();
}
public void loginPage()
{
editText = (EditText) findViewById(R.id.username);
editText1 = (EditText) findViewById(R.id.password);
username = editText.getText().toString();
password = editText1.getText().toString();
btn = (Button) findViewById(R.id.login1);
editText1.setEnabled(false);
btn.setEnabled(false);
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) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() >= 10) {
editText1.setEnabled(true);
btn.setEnabled(false);
}
}
});
editText1.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.length() >= 8) {
btn.setEnabled(true);
}
}
});
}
}
EDIT :
Create new function to Change from Login page on Button Click.
public void changeLoginPage(){
Intent intentNew = new Intent(First.this,Destinatin.this);
startActivity(intentNew);
}
and add this on onCreate method.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeLoginPage();
}
});

EditText - change text while typing

I need to replace the text inside the EditText while typing :
Example : if the user pressed "A" it would be stored into a buffer and on the EditText "D" is displayed instead (looks like he pressed "D").
Now I can read the pressed character but I can't display any character in the et to avoid stackoverflow :
final EditText et = (EditText) findViewById(R.id.editTexts);
final TextView tv = (TextView) findViewById(R.id.textView2);
et.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.length() > 0) {
tv.setText(s.toString().substring(s.length()-1));
et.setText("");
}
}
});
You can change it as required::
public class SampleActivity extends Activity {
TextWatcher tt = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et = (EditText) findViewById(R.id.editText1);
final TextView tv = (TextView) findViewById(R.id.textView1);
tt = new TextWatcher() {
public void afterTextChanged(Editable s){
et.setSelection(s.length());
}
public void beforeTextChanged(CharSequence s,int start,int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count) {
et.removeTextChangedListener(tt);
et.setText(et.getText().toString().replace("A", "C"));
et.addTextChangedListener(tt);
}
};
et.addTextChangedListener(tt);
}
}
In order to change the text interactively, you need to register a TextWatcher. But trying to change the text inside the watcher creates further calls to the watcher. My hack is to temporarily remove the watcher when I want to change the text, and re-register it right after
mEditText.addTextChangedListener(new TextWatcher() {
#Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
#Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
#Override public void afterTextChanged(Editable editable) {
mEditText.removeTextChangedListener(this);
mEditText.setText(//TODO change whatever you like here);
mEditText.setSelection(editable.length()); //moves the pointer to end
mEditText.addTextChangedListener(this);
}

Categories