This the code that I've written but I am getting the full string and the function is only triggered if the first character is #. I want it to check for every instance of # and whenever the # key is pressed i want to open the dialog. I tried using the onKeyDown() but couldn't make it work.
override fun afterTextChanged(editable: Editable?) {
viewModel.onDoubtTextChanged(editable.toString())
if (editable.toString() == "#") {
openUserSuggestionDialog(doubtDescription.users as ArrayList<User>,viewModel)
}
editable contains the value which is being inputted into the TextView
Add a on TextChange Listener to your editText
editable.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//get the last String entered
String lala = editable.getText().toString();
String subChar = lala.substring(lala.length() - 1);
if(subChar.equals("#"))
openUserSuggestionDialog(doubtDescription.users as ArrayList<User>,viewModel)
}
In the Android app, I have an EditText which should replace certain strings with values from app data.
for e.g. if user types $username it should get replaced with the name of whichever user is currently logged in.
The Editable parameter in afterTextChanged() method of TextWatcher applied on EditText replaces the $username with correct value but the problem is that after the $username is replaced with actual username if I press any character after that it is appended with username followed by pressed character.
e.g.
Say current logged in username is Joe
a. if the input is Hi this is #username
b. afterTextChanged() changes it to Hi this is Joe
c. Now if I press any other character(say I press g OR space) then text in EditText changes to Hi this is Joeusernameg OR Hi this is Joeusername
How do I get output as in step b?
etTemplateMessage.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) {
final String strUserNameCode = "$username";
String text = s.toString();
if(text.contains(strUserNameCode)){
int startIndex = text.indexOf(strUserNameCode);
int endIndex = startIndex + strUserNameCode.length();
Editable profileName = new SpannableStringBuilder(FileUtil.getUTF8String(settingDTO.getProfileName()));
s.replace(startIndex, endIndex, profileName);
}
}
});
on your afterTextChange method you should set text to the edit text. And String has replace(CharSequence old, CharSequence new) method you can also use it.
like this,
PublishSubject publishSubject = PublishSubject.create();
publishSubject.debounce(200, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(query -> onTextChanged(query.toString()));
void onTextChanged(String text){
final String strUserNameCode = "$username";
etTemplateMessage.setText(text.replace(strUserNameCode, FileUtil.getUTF8String(settingDTO.getProfileName())));
}
and on your aftertextChange method call publishSubject.onNext( s.toString())
Note that you can achieve this with RxJava.
I was facing the problem with the addTextChangedListener that was not fully delete the text in Firebase. I state an example, I get my name from Firebase and setText() on the myname. So on myname editText has show my name there. When I wanted to edit, I click on editText, so I can remove the word by backspace, but when I backspace too fast, the text in editText was fully removed (client side) and there are some word did not delete on Firebase(server side).
Which mean the removing text value are not consistent and accurate with the client side and server side. The ordinary text of my name is Tommy, so I backspace until the Tommy word gone, so that was cleared but in Firebase it still showing the first character 'T' of my name.
But when I was clearing all word on the editText, I input new word such as "Hello", the Firebase will store Hello.
Code:
//delcare myname
private MultiAutoCompleteTextView myname;
myname.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(!myname.getText().toString().trim().isEmpty()){
DatabaseReference nameRef = FirebaseDatabase.getInstance().getReference()
.child(Config.URL_USER);
nameRef.child(uid).child("myName").setValue(userET.getText().toString());
parentActivity.updateHeaderUserName(myname.getText().toString());
}
}
});
This loops work until there is a single charcter in the edittext, so add an else loop and inside the else loops set the value as null
I need to do form input validation on a series of EditTexts. I'm using OnFocusChangeListeners to trigger the validation after the user types into each one, but this doesn't behave as desired for the last EditText.
If I click on the "Done" button while typing into the final EditText then the InputMethod is disconnected, but technically focus is never lost on the EditText (and so validation never occurs).
What's the best solution?
Should I be monitoring when the InputMethod unbinds from each EditText rather than when focus changes? If so, how?
Why don't you use TextWatcher ?
Since you have a number of EditText boxes to be validated, I think the following shall suit you :
Your activity implements android.text.TextWatcher interface
You add TextChanged listeners to you EditText boxes
txt1.addTextChangedListener(this);
txt2.addTextChangedListener(this);
txt3.addTextChangedListener(this);
Of the overridden methods, you could use the afterTextChanged(Editable s) method as follows
#Override
public void afterTextChanged(Editable s) {
// validation code goes here
}
The Editable s doesn't really help to find which EditText box's text is being changed. But you could directly check the contents of the EditText boxes like
String txt1String = txt1.getText().toString();
// Validate txt1String
in the same method. I hope I'm clear and if I am, it helps! :)
EDIT: For a cleaner approach refer to Christopher Perry's answer below.
TextWatcher is a bit verbose for my taste, so I made something a bit easier to swallow:
public abstract class TextValidator implements TextWatcher {
private final TextView textView;
public TextValidator(TextView textView) {
this.textView = textView;
}
public abstract void validate(TextView textView, String text);
#Override
final public void afterTextChanged(Editable s) {
String text = textView.getText().toString();
validate(textView, text);
}
#Override
final public void beforeTextChanged(CharSequence s, int start, int count, int after) { /* Don't care */ }
#Override
final public void onTextChanged(CharSequence s, int start, int before, int count) { /* Don't care */ }
}
Just use it like this:
editText.addTextChangedListener(new TextValidator(editText) {
#Override public void validate(TextView textView, String text) {
/* Validation code here */
}
});
If you want nice validation popups and images when an error occurs you can use the setError method of the EditText class as I describe here
In order to reduce the verbosity of the validation logic I have authored a library for Android. It takes care of most of the day to day validations using Annotations and built-in rules. There are constraints such as #TextRule, #NumberRule, #Required, #Regex, #Email, #IpAddress, #Password, etc.,
You can add these annotations to your UI widget references and perform validations. It also allows you to perform validations asynchronously which is ideal for situations such as checking for unique username from a remote server.
There is a example on the project home page on how to use annotations. You can also read the associated blog post where I have written sample codes on how to write custom rules for validations.
Here is a simple example that depicts the usage of the library.
#Required(order = 1)
#Email(order = 2)
private EditText emailEditText;
#Password(order = 3)
#TextRule(order = 4, minLength = 6, message = "Enter at least 6 characters.")
private EditText passwordEditText;
#ConfirmPassword(order = 5)
private EditText confirmPasswordEditText;
#Checked(order = 6, message = "You must agree to the terms.")
private CheckBox iAgreeCheckBox;
The library is extendable, you can write your own rules by extending the Rule class.
Updated approach - TextInputLayout:
Google has recently launched design support library and there is one component called TextInputLayout and it supports showing an error via setErrorEnabled(boolean) and setError(CharSequence).
How to use it?
Step 1: Wrap your EditText with TextInputLayout:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layoutUserName">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hint"
android:id="#+id/editText1" />
</android.support.design.widget.TextInputLayout>
Step 2: Validate input
// validating input on a button click
public void btnValidateInputClick(View view) {
final TextInputLayout layoutUserName = (TextInputLayout) findViewById(R.id.layoutUserName);
String strUsername = layoutLastName.getEditText().getText().toString();
if(!TextUtils.isEmpty(strLastName)) {
Snackbar.make(view, strUsername, Snackbar.LENGTH_SHORT).show();
layoutUserName.setErrorEnabled(false);
} else {
layoutUserName.setError("Input required");
layoutUserName.setErrorEnabled(true);
}
}
I have created an example over my Github repository, checkout the example if you wish to!
This was nice solution from here
InputFilter filter= new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
String checkMe = String.valueOf(source.charAt(i));
Pattern pattern = Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789_]*");
Matcher matcher = pattern.matcher(checkMe);
boolean valid = matcher.matches();
if(!valid){
Log.d("", "invalid");
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});
I wrote a class that extends EditText which supports natively some validation methods and is actually very flexible.
Current, as I write, natively supported through xml attributes validation methods are:
alpha
alpha numeric
numeric
generic regexp
string emptyness
You can check it out here
Hope you enjoy it :)
I find InputFilter to be more appropriate to validate text inputs on android.
Here's a simple example:
How do I use InputFilter to limit characters in an EditText in Android?
You could add a Toast to feedback the user about your restrictions.
Also check the android:inputType tag out.
I needed to do intra-field validation and not inter-field validation to test that my values were unsigned floating point values in one case and signed floating point values in another. Here's what seems to work for me:
<EditText
android:id="#+id/x"
android:background="#android:drawable/editbox_background"
android:gravity="right"
android:inputType="numberSigned|numberDecimal"
/>
Note, you must not have any spaces inside "numberSigned|numberDecimal". For example: "numberSigned | numberDecimal" won't work. I'm not sure why.
This looks really promising and just what the doc ordered for me:
EditText Validator
public void onClickNext(View v) {
FormEditText[] allFields = { etFirstname, etLastname, etAddress, etZipcode, etCity };
boolean allValid = true;
for (FormEditText field: allFields) {
allValid = field.testValidity() && allValid;
}
if (allValid) {
// YAY
} else {
// EditText are going to appear with an exclamation mark and an explicative message.
}
}
custom validators plus these built in:
regexp: for custom regexp
numeric: for an only numeric field
alpha: for an alpha only field
alphaNumeric: guess what?
personName: checks if the entered text is a person first or last name.
personFullName: checks if the entered value is a complete full name.
email: checks that the field is a valid email
creditCard: checks that the field contains a valid credit card using Luhn Algorithm
phone: checks that the field contains a valid phone number
domainName: checks that field contains a valid domain name ( always passes the test in API Level < 8 )
ipAddress: checks that the field contains a valid ip address
webUrl: checks that the field contains a valid url ( always passes the test in API Level < 8 )
date: checks that the field is a valid date/datetime format ( if customFormat is set, checks with customFormat )
nocheck: It does not check anything except the emptyness of the field.
In main.xml file
You can put the following attrubute to validate only alphabatics character can accept in edittext.
Do this :
android:entries="abcdefghijklmnopqrstuvwxyz"
You can get desired behavior by listening when user hit "Done" button on keyboard, also checkout other tips about working with EditText in my post "Android form validation - the right way"
Sample code:
mTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
validateAndSubmit();
return true;
}
return false;
}});
for email and password validation try
if (isValidEmail(et_regemail.getText().toString())&&etpass1.getText().toString().length()>7){
if (validatePassword(etpass1.getText().toString())) {
Toast.makeText(getApplicationContext(),"Go Ahead".....
}
else{
Toast.makeText(getApplicationContext(),"InvalidPassword".....
}
}else{
Toast.makeText(getApplicationContext(),"Invalid Email".....
}
public boolean validatePassword(final String password){
Pattern pattern;
Matcher matcher;
final String PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[A-Z])(?=.*
[##$%^&+=!])(?=\\S+$).{4,}$";
pattern = Pattern.compile(PASSWORD_PATTERN);
matcher = pattern.matcher(password);
return matcher.matches();
}
public final static boolean isValidEmail(CharSequence target) {
if (target == null)
return false;
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
I have created this library for android where you can validate a material design EditText inside and EditTextLayout easily like this:
compile 'com.github.TeleClinic:SmartEditText:0.1.0'
then you can use it like this:
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="#+id/passwordSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Password"
app:setMandatoryErrorMsg="Mandatory field"
app:setPasswordField="true"
app:setRegexErrorMsg="Weak password"
app:setRegexType="MEDIUM_PASSWORD_VALIDATION" />
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="#+id/ageSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Age"
app:setMandatoryErrorMsg="Mandatory field"
app:setRegexErrorMsg="Is that really your age :D?"
app:setRegexString=".*\\d.*" />
Then you can check if it is valid like this:
ageSmartEditText.check()
For more examples and customization check the repository
https://github.com/TeleClinic/SmartEditText
I have an Activity with an AutoComplete box on it. Whenever the text changes, I want to call a web service and populate the ArrayAdapter with the new String[] returned. This part all works great, except the list in the UI isn't being refreshed when there are all new values in String[] schools. The original list populated in my onCreate always remains.
I read somewhere I needed to update it on the same thread the UI is running on, so I tried the Runnable listed in my code below. But that, as well as just updating my class variable schools does not work alongside notifyOnDataSetChange()
Where am I going wrong?
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
schools = SchoolProxy.search("Co");
autoCompleteAdapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line, schools);
autoComplete = (AutoCompleteTextView) findViewById(R.id.edit);
autoComplete.addTextChangedListener(textChecker);
autoComplete.setAdapter(autoCompleteAdapter);
}
...
....
...
final TextWatcher textChecker = 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)
{
schools = SchoolProxy.search(s.toString());
runOnUiThread(updateAdapter);
}
};
private Runnable updateAdapter = new Runnable() {
public void run() {
autoCompleteAdapter.notifyDataSetChanged();
}
};
As a less important side note, if each item in schools has a name \n city,state. Is it possible to have the city & state on a second line within the auto drop down box? Just for formatting purposes. Would look cleaner if I could.
Thank you!
Have you tried the setNotifyOnChange(true)? It is supposed to properly do what you are doing manually whenever you use methods that change the list (add(T), insert(T, int), remove(T), clear()). Perhaps you have to modify the array through these methods on the ArrayAdapter?
I'm not sure if the ArrayAdapter is actually holding the reference to schools or just copied the contents while constructing the ArrayAdapter. Maybe you can take a look at the AOSP code to see what they're doing in that constructor.
After you dynamically change adapter, just need to call:
AutoCompleteTextView.showDropdown()