EditText in Google Android - android

I want to create the EditText which allow Numeric Values, Comma and Delete and other values are ignore.
So How I can achieve this by programming code ?
Thanks.

I achieved same thing using follwing code, hope you will also find help from it.
editText.addTextChangedListener(controller);
#Override
public void afterTextChanged(Editable s) {
if(s.trim.length() > 0)
{
int start = s.toString().length() - 1;
int a = (int) s.charAt(start);
if (!(a >= 48 && a <= 57 || a == 44))
s = s.delete(start, start + 1);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

To restrict what characters can be typed into an android EditText, you must set them in the android:digits XML attribute. See http://developer.android.com/reference/android/widget/TextView.html#attr_android:digits . Make sure to also validate user input before putting it into storage.

just simple ,
you want to create a text-field like that you can enter the new string must have some grammar that you want to allow.
i have some email validation grammar that can help you.
string = "/alphabetics/number/special-character" ;
and another is
user-entered-string = " ";
just compare user-entered-string's lexems and you defined grammar.

Related

show TextView text depending on an answer

I am very new to this sort of thing and am trying to teach myself but have gotten stuck here. I am writing an app which will calculate the size battery required. The calculator side of things i have done and it is working however i want to add a feature that when the answer is displayed, it will tell you what part number you need for the battery required. I have tried to work this out myself and failed miserably so i am hoping someone may be able to point me in the right direction
The answer from the battery calculator shows in ahresult. what i want is battreq to show the part number required for the correct size battery. so for example, if the calculator showed that a 2.43Ah battery was required, battreq would show "A 3.2ah battery is required" as that is the next size available.
ahresult = (TextView) findViewById(R.id.ahresult);
battreq.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) {
Integer.parseInt(ahresult.getText().toString());
if (ahresult.equals(>= "0.00" && <= "1.20")){
battreq.setText("A 1.2Ah battery is required");
}
else if (ahresult.equals (>= "1.21 && <= 2.10")){
battreq.setText("A 2.1Ah battery is required");
}
else if (ahresult.equals (>= "2.11 && <= 3.20")){
battreq.setText("A 3.2Ah battery is required");
}
// so on and so on with different battery sizes
}
Here is my (poor) effort at having a go but i am getting illegal start of expression.
Could someone guide me in the right direction please?
EDIT: Still seem to be having issues with this despite the help here. Below is the updated code. Have i got anything wrong here?
ahresult.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
double result = Double.parseDouble(ahresult.getText().toString());
if (ahresult.equals(result >= 0.001 && result <=1.200 )) {
battreq.setText("1.2Ah req");
}
else if (ahresult.equals(result >= 1.201 && result <=2.100 )) {
battreq.setText("2.1Ah req");
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Firstly, you should add addTextChangedListener to ahresult instead of battreq.
Secondly, you have to parse the changed text s to Double then compare within your desired range:
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Double result = Double.parseDouble(s.toString());
if (ahresult.equals(result >= 0.00 && result <= 1.20)){
battreq.setText("A 1.2Ah battery is required");
}
else if (ahresult.equals (result>= 1.21 && result <= 2.10)){
battreq.setText("A 2.1Ah battery is required");
}
else if (ahresult.equals (result>= 2.11 && result <= 3.20)){
battreq.setText("A 3.2Ah battery is required");
}
// so on and so on with different battery sizes
}
You need change this
battreq.addTextChangedListener(new ...
With this
ahresult.addTextChangedListener(new ...
You need listen the ahresult's value and change the battery needed.
In the "onTextChanged" the "CharSequence s" param is the ahresult's text

create tag comment system in android same as facebook

create tag comment system in android same as facebook.
In comment section if we insert # and type then show list of friend. and choose one friend. I want this type of comment system same as facebook.
You can set a TextWatcher which will get triggered whenever text changes in your EditText. Then, you can use a Regex after an # is found to see if there is any #name followed by a space. If there is, you can make another UI element pop up which shows a ListView of friends which match the particular Regex. Here is an example which I came up with:
// Declare listening as a member variable
commentInput.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// Listen until space
String text = charSequence.toString();
if (text.contains("#")) {
listening = true;
int index = text.split("#").length;
String textToSearchFor = text.split("#")[index];
// Use textToSearchFor to search for friends,
// and if you get results then set a UI element
// to appear: listView.setVisibility(View.VISIBLE);
// on click of ListView element, stop listening
} else if (text.substring(0, text.length() - 1).equals(" ")) {
// If the latest character is a space, then stop listening
listening = false;
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});

Android TextWatcher behavior

Although code is far from being complete, for the begining I tried to detect via TextWatcher
when character is pressed in EditText that doesn't belong to hex values and not to permit it
to be displayed, but to inform user of error entry.
The excerpt from code follows, where arrayOfChars consists of 16 permitted hex chars and edt2
is EditText var. What I tried is to enter following chars: "aeyd", so it is aim to inform of
"y" as an error and not to display it.
edt2.addTextChangedListener(new TextWatcher(){
private boolean errorDetected= false;
private int oldbefore;
#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 st;
if (errorDetected){ //set in code bellow
errorDetected= false;
return;
}
st= s.toString();
if (before > 0){
if (before<= s.length()){st= s.subSequence(before, s.length()).toString();}
}
boolean velid= true;
for (char c: st.toCharArray()){
if (new String(arrayOfChars).indexOf(Character.toUpperCase(c))==-1){
edt2.setError("Wrong char - " + c);
errorDetected= true;
oldbefore= before;
velid= false;
break;
}
}
if (velid) {edt2.setError(null);}
}
#Override
public void afterTextChanged(Editable s) {
if (errorDetected){s.delete(oldbefore, s.length());}
}
});
When "y" is entered everything behave as expected- only "ae" displayed and error info also.
However when "d" is entered and breakpoint settled at the beginning of onTextChanged, I see
"s" parameter to be "aeyd"- so "y" is still preserved somehow.
Any help where I go wrong ?

Form Validation Android

I was wondering what is the best way to validate form ?
I did try the following:
EditText fname = (EditText)findViewById(R.id.first_name);
String fname_text = fname.getText().toString;
if(fname_text.equalsIgnoreCase(""))
{
fname.setError("Field is required");
}
and also:
fname.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)
{
Pattern mPattern = Pattern.compile("[A-Za-z]{2,20}$");
Matcher matcher = mPattern.matcher(s.toString());
if(!matcher.matches()) // on Success
{
fname.setError("Please make sure you input a valid first name");
}
}
});
The thing that I am confused in is that ... whenever the page loads for the first time, the error message is shown, but when I go inside the EditText and type some content, and if I erase the content, the error message does not persist. So how do I keep this validation persistent ??? Because the way that the program is shaping up, it looks like it won't validate anything very nicely. And you guys know some good links for regex in android with complete example, please do recommend.
And also, how will me putting the Pattern and Matcher methods in onTextChanged or beforeTextChanged affect the output ?
Instead of running the check right after
EditText fname = (EditText)findViewById(R.id.first_name);
String fname_text = fname.getText().toString;
Use an OnFocusChangeListener and run it whenever the onFocusChange() method is called. Ideally, you'd run it only when the View loses focus. Something like:
EditText fname = (EditText)findViewById(R.id.first_name);
String fname_text = fname.getText().toString;
fname.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View arg0, boolean arg1) {
if(!(v.isFocused())) {
//Run your validation
}
}
});
This way, you only run the Validation when the user is done typing, instead of everytime the user changes something.

afterTextChanged deleting issue

I tried to set this up so that when I enter something into the EditText field, it will change the text color and set a variable to the value of the numbers entered. However, when I delete the characters from the field, it triggers an error that closes the app. I think I may either need to find an if statement that doesn't rely on length, or maybe use some of the other methods (onTextChanged, beforeTextChanged... I don't really know how to use either of those correctly though)
public void afterTextChanged(Editable arg0) {
if(arg0.length()>0){
ageTag.setTextColor(Color.GREEN);
ageEntered=true;
ageInYears=Integer.parseInt(enterAge.getText().toString());
}
}
Implement the android.text.TextWatcher interface
Bind a listener:
enterAge.addTextChangedListener(this);
I had a similar problem. I had to append "-" after 5th char, 11th etc. When I wanted to delete the "-" it was ignoring the delete key, so what I did is the following.
Created two variables: beforeCharCounter and charCounter and in onTextChanged I have set their values on before and count.
activationKeyEditText.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) {
beforeCharCounter = before;
charCounter = count;
System.out.println(count);
}
#Override
public void afterTextChanged(Editable s) {
if ((s.length() == 5 && beforeCharCounter <= 5 && charCounter == 5) ||
(s.length() == 11 && beforeCharCounter <= 11 && charCounter == 11)
|| (s.length() == 17 && beforeCharCounter <= 17 && charCounter == 17)) {
s.append('-');
}
}
});
if the value of the before is bigger then the length then for sure it is delete and this will solve the delete issue. The count is used for when the user has entered 10 chars and he press on the 5th one. If we don't keep the exact count dashes will be appended at the end like crazy.
Hope this will help you solve the issue.

Categories

Resources