I want to set my button active, only when two EditTexts are not empty. I try to use TextWatcher for that, but it doesn't really work fine - button stays active, even if erased all text from price input. Here is my code:
final EditText titleEdit = findViewById(R.id.name);
final EditText priceEdit = findViewById(R.id.price);
final Button addButton = findViewById(R.id.add);
titleEdit.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 (!TextUtils.isEmpty(titleEdit.getText().toString().trim()) && !TextUtils.isEmpty(priceEdit.getText().toString().trim()))
addButton.setEnabled(true);
else addButton.setEnabled(false);
}
#Override
public void afterTextChanged(Editable s) {
}
});
I tried few solutions from Stackoverflow, but it's still the same. What should I change in my code?
Define the following variable as global variable
Boolean text1= false;
Boolean text2 = false;
Complete code
public class classname extends AppCompatActivity {
Boolean text1= false;
Boolean text2 = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
//Your code
titleEdit.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.length() > 0 ){
text1 = true;
if(text1 && text2){
addButton.setEnabled(true);
}
}else{
text1 = false;
addButton.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
priceEdit.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.length() >0 ){
text2 = true;
if(text1 && text2){
addButton.setEnabled(true);
}
}else{
text2 = false;
addButton.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
You should create Both EditText section TextWatcher.
Check if (s.length()>0))
Try with
titleEdit.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.length()>0))
{
addButton.setEnabled(true);
}
else
{
addButton.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
priceEdit.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.length()>0))
{
addButton.setEnabled(true);
}
else
{
addButton.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Related
I have 4 edittexts that act as a "enter your pin" field with each edittext set to only be able to take a single number before requesting focus to the next edittext. If I enter a number for the 1st pin, it then request focus to the 2nd, and then 3rd, etc.
What I am trying to do now is, if the user clicks on the 2nd edittext or 3rd and the 1st field has not been entered, it should go back and focus on the 1st field.
The edittext fields
I tried to do this using an onTouchListener and what I noticed is that after clicking on the 2nd edittext first instead of clicking on the left/1st edittext, I can see the focus going back to the first edittext but then immediately go back to the 2nd edittext almost a split second and ultimately still remain focus on the 2nd edittext.
I also tested using onClickListener. It does work but it takes two clicks. So I'll click on the 2nd edittext, nothing happens, and then I click it again which then moves the focus back to the 1st edittext but I need it to happen on the first click.
Here is part of the code
pin1EditTxt.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(pin1EditTxt.getText().toString().length()==1) //size as per your requirement
{
pin2EditTxt.requestFocus();
}
}
});
// pin2EditTxt.setOnTouchListener(new View.OnTouchListener() {
// #Override
// public boolean onTouch(View v, MotionEvent event) {
// if(pin1EditTxt.length() == 0){
// pin2EditTxt.clearFocus();
// pin1EditTxt.requestFocus();
// }
// return false;
// }
// });
pin2EditTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(pin1EditTxt.length() == 0){
pin1EditTxt.requestFocus();
}
}
});
pin2EditTxt.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(pin2EditTxt.getText().toString().length()==1) //size as per your requirement
{
pin3EditTxt.requestFocus();
}
}
});
pin3EditTxt.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(pin3EditTxt.getText().toString().length()==1) //size as per your requirement
{
pin4EditTxt.requestFocus();
}
}
});
pin4EditTxt.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) {
//Hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
editTxtWrapper.requestFocus();
}
});
I guess you should be using pin1EditTxt.getText().toString().length()==0 instead of pin1EditTxt.length()==0 inside onclick.
I want to detect if something put into EditText was put there using SoftKey.
I've overridden dispatchKeyEvent but it doesn't work ...
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.e("key pressed", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
}
Find the solution
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
// like this you can detect
}
return super.onKeyDown(keyCode, event);
}
yourEditText.addTextChangedListener(watcher);
private TextWatcher watcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(!s.toString().isEmpty()){
//your text is not empty
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
};
So, I want a user to enter his/her number plate of his/her car and I want it to be quite stylish by separating all the characters like this picture: enter image description here
Right now it consists of 6 EditTexts with TextWatchers that change the focus for each input. That part works fine but my problem is with the deletion och characters.
When a user want to edit one field, he/she can just click the view and delete eand replace. Although when the whole thing is wrong it is not possible to delete everything at once but you have to click every view and delete by hand.
So what I need is so when the user hit backspace on an empty view, it should change focus to the one before and delete that character and so on. So all the EditTexts will be connected and work as one. I've tried with KeyListeners that listens to backspace but that only works with hardware keyboards and not soft keyboards on the phone.
I'm also happy if someone can point me in the direction of another solution better than this one.
TextWatcher:
registrationPlateEditTexts is an List of all the EditText in order.
private TextWatcher regPlateTextWatcher = 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) {
for (int i=registrationPlateEditTexts.size()-1; i>=0; i--){
if (registrationPlateEditTexts.get(i).getText().length()==1 && i!=registrationPlateEditTexts.size()-1){
registrationPlateEditTexts.get(i+1).requestFocus();
return;
}
else if (registrationPlateEditTexts.get(i).getText().length()==1){
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
#Override public void afterTextChanged(Editable s) {}
};
else if (registrationPlateEditTexts.get(i).getText().length()==1){
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
#Override public void afterTextChanged(Editable s) {}
};`
I took 4 custom edit text with property of getting selected when you press back. Here are the listeners and the CustomEditText elements
mCodeFourEt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
mConfirmBtn.performClick();
return true;
}
return false;
}
});
mCodeTwoEt.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
String text = mCodeTwoEt.getText().toString();
if (text.length() == 0) {
mCodeOneEt.requestFocus();
mCodeOneEt.selectAll();
return true;
}
}
return false;
}
});
mCodeThreeEt.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
String text = mCodeThreeEt.getText().toString();
if (text.length() == 0) {
mCodeTwoEt.requestFocus();
mCodeTwoEt.selectAll();
return true;
}
}
return false;
}
});
mCodeFourEt.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
String text = mCodeFourEt.getText().toString();
if (text.length() == 0) {
mCodeThreeEt.requestFocus();
mCodeThreeEt.selectAll();
return true;
}
}
return false;
}
});
mCodeOneEt.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) {
if (mCodeOneEt.getText().toString().length() > 0) {
mCodeTwoEt.requestFocus();
}
}
});
mCodeTwoEt.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) {
if (mCodeTwoEt.getText().toString().length() > 0) {
mCodeThreeEt.requestFocus();
}
}
});
mCodeThreeEt.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) {
if (mCodeThreeEt.getText().toString().length() > 0) {
mCodeFourEt.requestFocus();
}
}
});
mCodeFourEt.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) {
}
});
Now here is the custom edittext class
public class CustomEditText extends android.support.v7.widget.AppCompatEditText {
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context) {
super(context);
}
#Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new ZanyInputConnection(super.onCreateInputConnection(outAttrs),
true);
}
private class ZanyInputConnection extends InputConnectionWrapper {
public ZanyInputConnection(InputConnection target, boolean mutable) {
super(target, mutable);
}
#Override
public boolean sendKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
// Un-comment if you wish to cancel the backspace:
// return false;
}
return super.sendKeyEvent(event);
}
#Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
// magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
if (beforeLength == 1 && afterLength == 0) {
// backspace
return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
&& sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
}
return super.deleteSurroundingText(beforeLength, afterLength);
}
}
}
Just use this custom class in your XML and see the example above which is for 4 edit text.
I have a Profile's form with some EditText like the name, address, city and so on. And I have a Button to save this data once you press it.
So I would like to know how can I check if there have been changes before make my app connect with the database and update it.
I've thought using a TextChangedListener() with some boolean on the onTextChanged() or afterTextChanged(), but I don't know exactly how to do that.
Any suggestion? Thank you very much!
I´ve tried the code below and it works for me. I have this right now:
public class ProfileActivity extends Activity {
TextView done_btn;
EditText local_court;
boolean hasChanges = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
done_btn = (TextView) findViewById(R.id.done_profile_btn);
local_court = (EditText)findViewById(R.id.input_local_court);
done_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
if(checkChanges()) {
//saveChanges();
finish();
Toast.makeText(getApplicationContext(), "Your changes have been saved", Toast.LENGTH_SHORT).show();
}
}
});
public boolean checkChanges() {
local_court.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
hasChanges = true;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
return hasChanges;
}
}
I guess I should make the same with the other EditText.
Thank you everyone!
If your goal is just to check if something has changed at all, just check the current EditText value to what was before.
But if you want to know if the text has been touched, even reverted back to original, do as below.
EditText etext = (EditText) view.findViewById(R.id.editTxt);
etext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s,
int start, int before,
int count) {
//Set your boolean here to true
}
#Override
public void beforeTextChanged(CharSequence s,
int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
}
);
My problem is little tangled, i have to EditText boxes. Now, when i typing something on first EditText then its shows on second EditText. And as vice versa while typing on second EditText it sets on first EditText. But it gives some error on setText of any one of EditText. Below is my code,
Boolean flag,flag2;
txt_editText_1.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() != 0)
{
flag=true;
flag2=false;
if(flag==true)
{
String result=txt_editText_1.getText().toString().trim();
txt_editText_2.setText(""+result);
}
}
}
});
txt_editText_2.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() != 0)
{
flag=false;
flag2=true;
if(flag2==true)
{
String result=txt_editText_2.getText().toString().trim();
txt_editText_1.setText(""+result);
}
}
}
});
But its not working. So, can anyone give suggestion.
I found my answer. I have put touch event on EditText for maintain flag(Boolean) status.
Boolean flag,flag2;
txt_editText_1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(MotionEvent.ACTION_UP == event.getAction())
{
flag=true;
flag2=false;
return true;
}
}
});
txt_editText_2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(MotionEvent.ACTION_UP == event.getAction())
{
flag=false;
flag2=true;
return true;
}
}
});
txt_editText_1.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() != 0)
{
if(flag==true)
{
String result=txt_editText_1.getText().toString().trim();
txt_editText_2.setText(""+result);
}
}
}
});
txt_editText_2.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() != 0)
{
if(flag2==true)
{
String result=txt_editText_2.getText().toString().trim();
txt_editText_1.setText(""+result);
}
}
}
});