I'm new to android & I'm trying to write an application for a project.
I need to check whether the user has entered 7 numbers followed by one alphabet in edittext.
Example: 0000000x
How should I do that? TIA! :)
Probably the best approach would be to use a TextWatcher passed into the addTextChangedListener() method of the EditText. Here is an example use:
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable e) {
String textFromEditView = e.toString();
validateText(textFromEditView);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//nothing needed here...
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//nothing needed here...
}
});
I will leave the implementation of the validateText(String) method as an exercise for the reader, but I imagine it should be easy enough. I would either use:
A simple Regular Expression.
Or since this case is easy enough, checking that the length of the string is 8, and reviewing each character. There is a simple utility class to inspect the characteristics of characters. Character.isDigit(char) and Character.isLetter(char)
OnKeyListener listens to every key stroke in the view. you can use that to check whether the user has entered what he is supposed.
eg : if the no of char entered is 7 then
check if it follows the reqd expression format.
There is a Class called Pattern in Android in that you can give Regular Expression to match your Requirements try this follwoing code i think it may work
Pattern p = Pattern.compile( "{7}" );
Matcher m = p.matcher(String.valueOf(edittext));
This will be true only if 7 characters are there in the Text box and then you can use some menthods like "Character.isDigit(char) and Character.isLetter(char)"
Related
I am trying to generate a regular expression in Android which can satisfy following conditions:
Edit text can accept:
Alphabet only
Combination of Alphabet and number
Combination of Alphabet and special character
Should not accept:
a. Only Number
b. Only Special character
I tried alot but still, i didn't get any meaningful link. Please try to save my day.
I tried with regular expression (?!^\d+$)^.+$") which only valid alphanumric requirement. I am looking for such regular expression which fullfil my requirement.
It's really simple bro... just clear EditText if the text inside it doesn't have an alphabet .. as you have said ...
Should not accept a. Only Number b. Only Special character
TextWatcher mTextWatcher = 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) {
boolean atleastOneAlpha = s.toString().matches(".*[a-zA-Z]+.*");
if (!atleastOneAlpha) {
editText.setText("");
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
mTargetEditText.addTextChangedListener(mTextWatcher);
I have a registration screen with large number of registration fields, and when user click register button, I pass field values to presenter. In presenter I validate these values and create an object. The problem is a large number of arguments in register() method. I think that I should avoid this situation, but I have no idea how to do it.
Maybe you could explore the Builder pattern.
It allows to keep the code clean when you need to pass a big number of arguments.
It's also very useful when you don't know the exact number of arguments that will be passed, because some of them might not be mandatory.
In practice, you would have something like
MyObject myObject
void register() {
myObject = MyObject.Builder(<mandatory arguments>)
.argument1(<argument 1>)
.argument2(<argument 2>)
...
.create();
if (myObject == null) fail();
else dosomething();
}
One way I have done this previously is to use a TextWatcher on each field that has to be completed:
myEditText.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) {
presenter.myEditTextChanged(s.toString());
}
});
Then have the corresponding methods in the presenter update your entity. This way when the user finally clicks register all the details will already be waiting in your presenter.
It also has the advantage that you can do validation as the user progresses - ie the register button isn't enabled until all fields are valid.
If you are using ButterKnife, RxBinding or DataBinding the code is more succinct as well.
I have an AutoCompleteTextView in my layout and I want to do an API call which takes the first character entered as a parameter. I do this API call in an AsyncTask to which I pass the first character as a parameter.
What listener should I use on AutoCompleteTextView so that AsyncTask call happens right after the first character is inputted ?
Try this link
http://makovkastar.github.io/blog/2014/04/12/android-autocompletetextview-with-suggestions-from-a-web-service/
I achieved the above functionality using shared article.
Fixed this issue using addTextChangedListener.
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().trim().length() == 1) {
isFileReceived = false;
mAirport = new AsyncTaskAirport(s.toString());
mAirport.execute((Void) null);
}
I'm trying to implement emojis in a way that I automatically replace things like ":)" with their emoji equivalent. And I'm not really sure how to do that, I found a table of emoji UTF codes, but I'm not sure on how I'm supposed to programmatically put them into a EditText :/
inputField=(EditText)temp.findViewById(R.id.inputField);
inputField.addTextChangedListener(new TextWatcher() {
boolean justChanged=false;
#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(justChanged) {
justChanged=false;
return;
}
Log.d(s.toString(), s.toString());
if(s.toString().contains(":)")) {
justChanged=true;
inputField.setText(s.toString().replace(":)", "UTF CODE HERE?"));
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
The simplest way is to achieve this is to encode your source code as UTF-8, and paste your desired emoji into the code. You will then need to pass -encoding UTF-8 to javac. The emoji will then be converted to its Unicode point on compilation.
E.g.
inputField.setText(s.toString().replace(":)", "😁"));
Alternatively, you can use UTF-16 Unicode point literals within Java strings, using the \uXXXX notation. From a suitable Unicode reference site, such as, http://www.fileformat.info/info/unicode/block/emoticons/list.htm, you can get the UTF-16 type encoding or the complete Java escape sequence.
E.g.
inputField.setText(s.toString().replace(":)", "\uD83D\uDE00"));
I'm programming on Android and I want an easy way to do the things below without having to have a bunch of overrides and ugly code:
These are all examples:
When editText1 is changed I want a TextView to be updated with copy of what the user typed
Same thing for editText2 up to editText10.
When editText11 is changed I want to multiply the number in it by 10 and put it In some TextView.
When editText12 becomes 0 I want some LinearLayouts to hide
Basically I want to be able to easily set up a listener and modify what kind of method the listener will trigger, without having a bunch of anonymous inner classes and other nasty stuff. Having many derivatives that each do their own predefined thing is OK, but I want to avoid repeating code and make it utilize polymorphism.
I tried really hard using interfaces, abstract methods, and other similar techniques but it just made my head go crazy.
What you probably want is a TextWatcher
How to use the TextWatcher class in Android?
Here's an example:
http://www.learn-android-easily.com/2013/06/using-textwatcher-in-android.html
You can pass TextView objects into it and monitor the text text as it is input by the user. However, even this is probably pretty complex based on the requirements you are describing and the general unpredictable behavior of users in general.
Why don't you try something like this for a custom TextWatcher with an event-listener callback you can use for after the text has changed:
public class CustomTextWatcher implements TextWatcher {
public interface TextChangedEventListener{
public void afterTextChanged(String newText);
}
private TextChangedEventListener eventListener;
public CustomTextWatcher(TextChangedEventListener eventListener){
this.eventListener = eventListener;
}
#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) {
eventListener.afterTextChanged(s.toString());
}
}
Now for the Implementation...
EditText editText = (EditText)findViewById(R.id.my_et);
editText.addTextChangedListener(new CustomTextWatcher(new CustomTextWatcher.TextChangedEventListener() {
public void afterTextChanged(String newText) {
// set the text of the next EditText, which will trigger the next TextWatcher in the chain
}
}));
You're still going to have to deal with an anonymous inner class, but there's not really a way around that if you want customization for each EditText. At least this will cut down on the number of inner classes from 3 to 1 and clean up the code you have to look at a bit.