SOLID - The Interface Segregation Principle - android

I am wondering how to implement Interface Segregation Principle from SOLID for the TextWatcher.
More specific: How to remove the not needed functions:
beforeTextChanged(),
afterTextChanged()
as I only need:
onTextChanged()
passwordinput.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) {
presenter.validateCredentials(emailinput.getText().toString(), passwordinput.getText().toString());
}
#Override
public void afterTextChanged(Editable s) {
}
});

Create a class NewClass that inherits from MyTextWatcher
and
passwordinput.addTextChangedListener(new New Class() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
presenter.validateCredentials(emailinput.getText().toString(), passwordinput.getText().toString());
}
});

You may create your own TextWatcher class by implementing TextWatcher and leave the default implementations empty. Then in your anonymous implementation only override onTextChanged().
class MyTextWatcher implements 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) {
}
}
Usage:
passwordinput.addTextChangedListener(new MyTextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
presenter.validateCredentials(emailinput.getText().toString(), passwordinput.getText().toString());
}
});

Related

Disable button if edit text field is empty. Can't get it to working

Currently I am learning to code and came across one problem. I need my upload button to be disabled untill Edit Text field is somekind of symbol than default hint
Full code
private TextWatcher EmptyEdit = 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().trim().length()==0)
{
mUpload.setEnabled(false);
} else {
mUpload.setEnabled(true);
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
Add your code in afterTextChanged() instead of onTextChanged().
private TextWatcher EmptyEdit = 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().trim().length()==0)
{
mUpload.setEnabled(false);
} else {
mUpload.setEnabled(true);
}
}
};
Do your code inside afterTextChanged().
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
mUpload.setEnabled(!TextUtils.isEmpty(s.toString().trim()))
}
// don't forget to set listner
editText.addTextChangedListener(EmptyEdit);
yourEditText.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) {
//Disable your button in here
}
});

Automatically passing focus betwen EditText

Since my question asking about separating characters that are typed in an EditText into groups or blocks didn't receive much attention, I've come here with another question on an alternative way to accomplish what I need.
So, my idea is to have four EditText that have 4 char length. Now what I want is to change the focus as the user type, from an EdutText to another when the limit is reached.
Example: Let's say we have EditText A, B, C, D; the first to gain focus is EditText A, then when the chars length at A reach 4, the focus passes to EditText B and so on and stops At D.
I'm trying something like the code bellow, but the focus is not requested when the length reaches 4 chars.
inputFieldA.requestFocus();
inputFieldA.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
inputFieldB.requestFocus();
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputFieldB.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
inputFieldC.requestFocus();
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputFieldC.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
inputFieldD.requestFocus();
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
inputFieldD.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().trim().length() == 4){
//send all to Main EditText (A + B + C + D)
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
So I'm here asking if there is a way to do that.
Write your conditions in afterTextChanged method and make changes as follows :
inputFieldA.requestFocus();
inputFieldA.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(inputFieldA.getText().toString().trim().length() == 4){
inputFieldB.requestFocus();
}
}
});
inputFieldB.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(inputFieldB.getText().toString().trim().length() == 4){
inputFieldC.requestFocus();
}
}
});
inputFieldC.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(inputFieldC.getText().toString().trim().length() == 4){
inputFieldD.requestFocus();
}
}
});
inputFieldD.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(inputFieldD.getText().toString().trim().length() == 4){
//send all to Main EditText (A + B + C + D)
}
}
});
if(inputFieldA.toString().trim().length() == 4){
inputFieldB.requestFocus();
}
replace this lines in onTextChange() methode.
instead of adding validation on the basis of CharSeq length add validation on the basis of edittext text like this:
replace
if(s.toString().trim().length() == 4)
with
if (currEdittext.getText().toString().length() >= 4)
Note: update your condition as well.
currEdittext will be either inputFieldA, inputFieldB, inputFieldC depending on your requirement

How to implement on one method of TextWatcher in android?

I have an app in which i have lots of edit text but in some edit text i only want to implement only one method of TextWatcher. How do i do that kindly guide.
code:-
private TextWatcher m_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) {
isFieldsEmpty();
}
#Override
public void afterTextChanged(Editable s) {
m_mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = m_mainLayout.getRootView().getHeight() - m_mainLayout.getHeight();
if (!(heightDiff > RewardUtil.dpToPx(mContext, 200))) { // if more than 200 dp, it's probably a keyboard...
m_otpEditText.clearFocus();
}
}
});
}
};
m_otpEditText.addTextChangedListener(m_textWatcher);
Create metod lke this.
public static class MyTextWatcher implements TextWatcher {
private EditText mEditText;
public MyTextWatcher(EditText editText) {
mEditText = editText;
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
oldText = mEditText.toString();
}
....
}
And use like this...
m_otpEditText.addTextChangedListener(new MyTextWatcher(mFirstEditText));
It will help you.
Thanks.
First of all, you can create a class as follows :
public class CustomTextWatcher implements 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) {
}
}
Thereafter, just override the method/s you need. Suppose you need only onTextChanged method, then you can do as follows :
m_otpEditText.addTextChangedListener(new CustomTextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
TextWatcher class gives you flexibility to override methods whichever you need. If you really need only one method then override only one, but you have to always implement all methods .

EditText - Cursor coming to start for every letter when clear text

I set TextWatcher to EditText like below. But when I try to clear text, cursor is coming to start after clearing every letter.
class MyInputWatcher implements 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) {
et.removeTextChangedListener(watcher2);
et.setText(s.toString().replaceAll("[^[:alpha:]]", ""));
et.addTextChangedListener(watcher2);
}
#Override
public void afterTextChanged(Editable s) {
}
}
Do it like this (UPDATED):
class MyInputWatcher implements 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 temp = s.toString().replaceAll("[^a-zA-Z]", "");
if (s.toString().length() != temp.length()) {
et.setText(temp);
et.setSelection(temp.length());
}
}
#Override
public void afterTextChanged(Editable s) {
}
}
Please try like this
editText.setSelection(editText.getText().toString().length());
Subhash Kumar, you can use method:
et.setSelection(position)
for displaying cursor in need position
Set position to your cursor on afterTextChanged() method like this.
class MyInputWatcher implements 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) {
et.removeTextChangedListener(watcher2);
et.setText(s.toString().replaceAll("[^[:alpha:]]", ""));
et.addTextChangedListener(watcher2);
}
#Override
public void afterTextChanged(Editable s) {
et.setSelection(et.getText().toString().length())
}
}
Every time you clear a character it calls onTextChanged() method, as your implementation it get the edittext text and back set to it, so the cursor comes to the starting of the text. Clear et.setText(s.toString().replaceAll("[^[:alpha:]]", "")); and it will be fixed. Or use this et.setSelection(et.getText().toString().length+1);

How to define Textchanged event for edit text?

how to define textchanged event for EditText??
Thanks,
Balu.
You need this:
inputView.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) {
}
#Override
public void afterTextChanged(Editable s) {
}
});

Categories

Resources