I want to execute one method wheneer i enter number in EditText view.But i did not change the focus of EditText.I am stay in the same EditText when i enter a new number automatically i eant to execute one function.Now my question is how we find that event that means enter a new number in EditText
You need to associate a TextWatcher to your EditText, and then, check in onTextChanged() if the character added was a number. You could use a regular expression for that :
if(s.matches("[0-9]")){
doYourStuffMethod();
}
You can also use the Matcher class from Android SDK which is an helper for this kind of stuff.
Related
I am making a grading app, basically I have an edit field (number field) that you can enter your grades (in numbers) in, or multiple fields for each grade? But lets say I want to calculate the average grade , and if my edit fields are not in an array I would have to go through each value of the field by id and that is going to make the code too long. So I want an array that takes values from each edit field, so if I have 2 edit fields and I type 4 in the first and 6 in the second my array would be {4,6}. I am a beginner in android development but I have solid Java experience.
There's only so much you can do here since each EditText is its own object with its own value field. So to some extent, you're going to have to reference each EditText.
What you can do at least is track each EditText in a List as a member variable. that way you're only having to grab a reference to each edittext once in your activity. Then, when you need to reference the collection for averaging or whatever else, you can just iterate over the list calling .getText(). If you need to reference a specific edittext, either store a separate reference in another member variable, or look it up in the list by id or by a tag you set (see here for more info on tags).
Maybe init each editText in a HashMap with a list of numeric values and use the editText reference to add and get values to the list of numeric values like this:
final Map<EditText, List<Integer>> editTexts = HashMap<EditText, List<Integer>>();
If you init an EditText you could do something like this:
EditText editText = (EditText) findViewById(R.id.editText);
editTexts.put(editText , new ArrayList<>());
editText.setOnClickListener(new OnClickListener(){
public void onClick(View v){
editTexts.get((EditText) v).add(SOME_VALUE);
}
);
I havn't tested it but this could be a valid strategy.
I'm working on an application where need to complete date automatic/suggest while writing. Thing is user can type other text as well so how to sure when he start writing for date.
For reference: I have found similar feature on slack app.
Any suggestion?
Add TextWatcher to the EditText view. In one of it's methods for example beforeTextChanged or onTextChanged get the last entered word (or char sequence - it's up to you to decide which part of entered text you want to check). Apply regular expression to it to check if it looks like date or not. Again it's up to you to decide what looks like date and what is not. If it looks like date - provide users with UI for auto completion or autocomplete by our own. Don't forget to move cursor to the end of autocompleted text using EditText.setSelection() method.
I am using Edit Text watcher, to do some calculations.like below,
And every Edit Text group (A,B,C) is working fine independently and relatively. But relatively worked only if i come from top to bottom. I want to refresh (getText and setText) on every Edit Text onTextChange() method invoked. i can do this using custom method .
But can i do this in one go , like all edit text values of a particular XML get refresh, so each editText onTextChange will be called in TOP-DOWN approach, Because every descendent group is dependent in some context to previous group .
This is doable, you just must have an algorithm. But yes, it is possible, and not too challenging...
As a hint, EditText.onTextChanged() methods inside of the each listener will need access its other 5 EditText siblings/neighbors. (A-C rows and 1-2 columns, respectively).
All 6 onTextChangedListener implementations for the 6 Edit Texts may need to be unique in how they handle textChanged events. You will need to pass in the objects in which they are responsible for changing in their constructors.
ie
EditText et1 = (EditText)findViewById(R.id.editText1);
EditText et2 = (EditText)findViewById(R.id.editText2);
et1.setOnTextChangedListener(new TextChangedListener(et2){
//global var
EditText nextEditText;
public TextChangedListener(EditText editText){
super();
nextEditText = editText;
}
public void onTextChanged(){
this.alert();
}
public void alert(){
//modify data here and update self if necessary.
//then alert the next boxes on text changed method.
nextEditText = editText.getListener().alert();
}
});
I have to create a group of EditText s that each can contains 1 character of a pin code with auto flow between like in the following image -
when user tapping the the keyboard, the application moves to the next EditText with out any Intervention of the user.
How can it be done?
It's called PinView, here is a link for PinView.
They have made custom keyboard too. Including back button, and verification code, make changes as per your requirement. It's very good and simple tutorial to achieve this important functionality.
Make an array of edit text:
private EditText pinInput[] = new EditText[4];
Then add each EditText in array:
pinInput[0] = (EditText) findViewById(R.id.first);
...
Then implement the functionality by using onTextChanged and onFucusChanged listeners
i have ten edit text item and validation for each field , when i click submit validation is processed , and if any invalidate field throw an alert , for example i have first name need to contain minimum 6 character , if validation fails i throw an alert .
what i need is to focus the invalidate field automatically after throwing alert for entering the right values.
If there is anyway to do this.
Thanks.
use the requestFocus() method on the edittext containing the invalid value