patternjavaMinor
Live character count for EditText
Viewed 0 times
edittextcharacterliveforcount
Problem
Could you review it and said if it's written good and/or is better way to do that?
My algorithm is:
Code of that algorithm is:
My algorithm is:
Input roomNumber
If length of roomNumber is 8 set goToRoom state Enabled and color 0xFFFFFFFF
Else set goToRoom state Disabled and color 0xBBFFFFFF
Code of that algorithm is:
public class Join_room_screen extends Activity {
EditText numberRoom;
Button goToRoom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.joinroom);
numberRoom = (EditText) findViewById(R.id.roomNumber);
goToRoom = (Button) findViewById(R.id.goToRoom);
TextWatcher watcher = new LocalTextWatcher();
goToRoom.addTextChangedListener(watcher);
updateButtonState();
}
void updateButtonState() {
boolean enabled = checkEditText(numberRoom);
if (enabled) {
goToRoom.setBackgroundColor(0xFFFFFFFF);
goToRoom.setEnabled(enabled);
} else {
goToRoom.setBackgroundColor(0xBBFFFFFF);
goToRoom.setEnabled(false);
}
}
private boolean checkEditText(EditText edit) {
return ((edit.getText().toString()).length() == 8 );
}
private class LocalTextWatcher implements TextWatcher {
public void afterTextChanged(Editable s) {
updateButtonState();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
}
Solution
Here is my list of improvements:
-
Encapsulate all the logic in a custom textwatcher. This gives you the benefit to make the EnableViewTextWatcher reusable in other parts of your project (if you extract it from the Activity).
-
Programming against TextView vs EditText and View vs Button will make the code more flexible.
-
Limit visibility by using private keyword (class members, parameters and local variables)
-
Make class members, params and local variables final is possible
-
Follow the general Android coding guidelines regarding naming.
-
Use meaningful names when naming class, class members, params, and local variables
-
Use annotations in your code to improve code quality
-
Use constants instead of Magic Numbers.
-
Encapsulate all the logic in a custom textwatcher. This gives you the benefit to make the EnableViewTextWatcher reusable in other parts of your project (if you extract it from the Activity).
-
Programming against TextView vs EditText and View vs Button will make the code more flexible.
-
Limit visibility by using private keyword (class members, parameters and local variables)
-
Make class members, params and local variables final is possible
-
Follow the general Android coding guidelines regarding naming.
-
Use meaningful names when naming class, class members, params, and local variables
-
Use annotations in your code to improve code quality
-
Use constants instead of Magic Numbers.
public class RoomSelectActivity extends Activity {
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_room);
final TextView roomNumberText = (TextView) findViewById(R.id.tv_room_number);
final View enabledView = findViewById(R.id.btn_go_to_room);
final TextWatcher watcher = new EnableViewTextWatcher(roomNumberText, enabledView);
roomNumberText.addTextChangedListener(watcher);
}
private static class EnableViewTextWatcher implements TextWatcher {
private static final int LENGTH_TO_ENABLE_VIEW = 8;
private static final int ENABLED_TEXT_COLOR = 0xFFFFFFFF;
private static final int DISABLED_TEXT_COLOR = 0xBBFFFFFF;
private final TextView mWatchedTextView;
private final View mEnableView;
public EnableViewTextWatcher(@NonNull final TextView textViewToWatch, @NonNull final View viewToEnable) {
mWatchedTextView = textViewToWatch;
mEnableView = viewToEnable;
updateEnabledViewState();
}
public void afterTextChanged(@NonNull final Editable s) {
updateEnabledViewState();
}
public void beforeTextChanged(@NonNull final CharSequence s, final int start, final int count, final int after) {
}
public void onTextChanged(@NonNull final CharSequence s, final int start, final int before, final int count) {
}
private boolean checkTextView(@NonNull final TextView textView) {
return ((textView.getText().toString()).length() == LENGTH_TO_ENABLE_VIEW);
}
private void updateEnabledViewState() {
final boolean enabled = checkTextView(mWatchedTextView);
mEnableView.setBackgroundColor(enabled ? ENABLED_TEXT_COLOR : DISABLED_TEXT_COLOR);
mEnableView.setEnabled(enabled);
}
}
}Code Snippets
public class RoomSelectActivity extends Activity {
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_room);
final TextView roomNumberText = (TextView) findViewById(R.id.tv_room_number);
final View enabledView = findViewById(R.id.btn_go_to_room);
final TextWatcher watcher = new EnableViewTextWatcher(roomNumberText, enabledView);
roomNumberText.addTextChangedListener(watcher);
}
private static class EnableViewTextWatcher implements TextWatcher {
private static final int LENGTH_TO_ENABLE_VIEW = 8;
private static final int ENABLED_TEXT_COLOR = 0xFFFFFFFF;
private static final int DISABLED_TEXT_COLOR = 0xBBFFFFFF;
private final TextView mWatchedTextView;
private final View mEnableView;
public EnableViewTextWatcher(@NonNull final TextView textViewToWatch, @NonNull final View viewToEnable) {
mWatchedTextView = textViewToWatch;
mEnableView = viewToEnable;
updateEnabledViewState();
}
public void afterTextChanged(@NonNull final Editable s) {
updateEnabledViewState();
}
public void beforeTextChanged(@NonNull final CharSequence s, final int start, final int count, final int after) {
}
public void onTextChanged(@NonNull final CharSequence s, final int start, final int before, final int count) {
}
private boolean checkTextView(@NonNull final TextView textView) {
return ((textView.getText().toString()).length() == LENGTH_TO_ENABLE_VIEW);
}
private void updateEnabledViewState() {
final boolean enabled = checkTextView(mWatchedTextView);
mEnableView.setBackgroundColor(enabled ? ENABLED_TEXT_COLOR : DISABLED_TEXT_COLOR);
mEnableView.setEnabled(enabled);
}
}
}Context
StackExchange Code Review Q#74930, answer score: 9
Revisions (0)
No revisions yet.