When I run the above, I get the progress bar to keep on adding 25 with every text
This is because you're using the following code (please read the comment):
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int length = s.toString().trim().length();
// if string not empty, keep adding the progress with 25
if (length > 0) {
registrationProgress.setProgress(registrationProgress.getProgress() + 25);
}
// subtract 25 each time string empty.
else {
registrationProgress.setProgress(registrationProgress.getProgress() - 25);
}
}
you can see that you're always adding 25 if there is a non empty string or subtracting 25 if no non empty string found.
To solve the problem, you need to keep tracking for each text change in first name, last name, e-mail, password. You can use HashMap combined with Enum. Something like this:
public YourActivity extends AppCompatActivity {
...
// this is the list of entry
private enum Entry {
FIRST_NAME, LAST_NAME, EMAIL, PASSWORD
}
// keep tracking the entry where Boolean is the value if entry already inserted
private HashMap<Entry, Boolean> mMapEntries = new HashMap<>();
private void checkAndValidateEntries() {
// initialize the entry as not inserted yet
mMapEntries.put(FIRST_NAME, false);
mMapEntries.put(LAST_NAME, false);
mMapEntries.put(EMAIL, false);
mMapEntries.put(PASSWORD, false);
// then handle the entry view
// here the example only for firstname
firstName.addTextChangedListener(new TextWatcher() {
...
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int length = s.toString().trim().length();
if (length > 0) {
boolean isInserted = mMapEntries.get(FIRST_NAME);
// do changes if entry not yet inserted before.
if(isInserted == false) {
// progress percentage per entry
int percentage = 100 / mMapEntries.size();
// add the progress
int progress = registrationProgress.getProgress() + percentage;
registrationProgress.setProgress(progress);
// we now have inserted the value, so mark it.
mMapEntries.put(FIRST_NAME, true);
}
} else {
boolean isInserted = mMapEntries.get(FIRST_NAME);
// Do changes if entry already inserted before.
if(isInserted == true) {
int percentage = 100 / mMapEntries.size();
// subtract the progress.
int progress = registrationProgress.getProgress() - percentage;
registrationProgress.setProgress(progress);
// entry is removed, mark as not inserted yet
mMapEntries.put(FIRST_NAME, false);
}
}
}
...
});
// handle the last name, e-mail, password
...
}
}
The above code will only increase the progress value if an entry progress isn't added to the progress before and vice versa.