Android: Limited number of characters in EditText - android

I want to allow user to enter only 10 characters inside the EditText. I tried two approaches.
Approach 1:
android:maxLength="10"
Approach 2:
I used InputFilter class.
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
When I am using these approaches, the cursor stops at 10 and no more characters are visible. However, it is still taking the characters I type after those 10 characters. I can see them in the suggestion area above keyboard. To explain clearly let me take an example.
Suppose I entered "abcdefghij", it works fine. Now, suppose I entered "abcdefghijklm", I can see only first 10 characters in the EditText but when press backspace it removes last character "m" instead of removing "j", the last character visible in EditText.
How can I solve this problem? I dont want to keep the extra characters in buffer also. So that when user presses backspace it should delete the 10th character.

You can use edittext.addTextChangedListener.
editTextLimited.addTextChangedListener(new TextWatcher() {
/** flag to prevent loop call of onTextChanged() */
private boolean setTextFlag = true;
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// add your code here something like this
if(count > 10){
Toast.makeText(context,"10 chars allowed",Toast.LENGTH_LONG).show();
// set the text to a string max length 10:
if (setTextFlag) {
setTextFlag = false;
editTextLimited.setText(s.subSequence(0, 10));
} else {
setTextFlag = true;
}
}
}
});

Your problem should be solved by adding this to your EditText:
android:inputType="textFilter"

Related

How to lose focus from auto complete text view and gain when focus when user tap on it in android?

I am developing an application with auto-complete text-view. My very first page contains auto-complete text-view which is working as SEARCH field. The moment my application launches, keyboard is also displaying and which hides my other contents in my first page.
I am sure that this will make user to feel bad. I need to overcome this. I need to lose focus of auto-complete text view initially and when user tap on it, keyboard should appear.
This is the listener code of my autocomplete text view.
AutoCompleteTextView jsearch;
jsearch = (AutoCompleteTextView) findViewById(R.id.asearch);
jsearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable editable) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
String newText = s.toString();
if(validNet()) {
new getJsonCategory().execute(newText);
mProgressBar.setVisibility(View.VISIBLE);
} else alertBox();
}
});
Two ways you can do.
Step 1:on launching the activity you can stop the popup of key board by the following code paste after oncreate:
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Step 2:
In code make Autocompletetextview .setFocusable(false);
Let me know is it use full or on.so that i can help you more

Move focus to next EditText in an array

I have an activity in which I get from the user credit card's serial number.
It contains four editTexts - each one for 4 digits.
I've used an array for the editTexts -
EditText[] editSerial = new EditText[4];
and I've restricted the input's length in each editText with
android:maxLength="4"
I want the focus to move to the next editText once the user have entered 4 digits in the current one.
I've seen this answer - How to automatically move to the next edit text in android:
et1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,int before, int count)
{
// TODO Auto-generated method stub
if(et1.getText().toString().length()==size) //size as per your requirement
{
et2.requestFocus();
}
}
Is there any BETTER solution than repeat this code 3 times?
Kind of. You need a TextWatcher on each one but you can extract it as a proper class so that you can pass in parameters indicating the View to focus on next.
Then it'll be like
et1.addTextChangedListener(new FocusSwitchingTextWatcher(et2));
et2.addTextChangedListener(new FocusSwitchingTextWatcher(et3));
et3.addTextChangedListener(new FocusSwitchingTextWatcher(et4));
The class:
private static class FocusSwitchingTextWatcher implements TextWatcher {
private final View nextViewToFocus;
TextWatcher(View nextViewToFocus) {
this.nextViewToFocus = nextViewToFocus;
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length > size) {
nextViewToFocus.requestFocus();
}
}
... // the other textwatcher methods
}

implement TextWatcher on multiple edittext fields

I have this application in which I create multiple EditTexts dynamically. The amount depends on user input. I am trying to allow the focus to change to the next edittext after two characters have been entered. I have this so far:
for(EditText editText : editTextList ) {
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
if(s.toString().length() == 2) {
//code to change focus to next edit text goes here
}
}
});
}
The first problem is that this could potentially create many instances of TextWatcher if the user enters a large number (average in this context would probably be 100-600 EditText fields).
The second problem is how would I go about changing focus in the afterTextChanged method because I would want to change the focus to the next EditText away from the one that is currently being represented inside the loop.
Should I not be concerned about the potential performance issues of multiple TextWatcher objects? Should I scrap this whole implementation and focus elsewhere? Any suggestions will be greatly appreciated.
As you may have a lot of EditTexts at runtime i would suggest you to use maxlength attribute of EditText in this way you will restrict user to enter only 2 characters.
like this
EditText editText = new EditText(this);
int maxLength = 2;
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
Now add next button in your keyboard like this
yourEditText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
It is the default behavior of next button to change focus to the immediate next view which takes input being in the same parent layout.

Check if an EditText was modified

My app should open a file inside an edittext to show it to the user. If user want to modify it just press inside textwiew and write it. After do this when back button is pushed, if the text was modify, the changes should be saved, else, just close the current activity and go to parent.
There's a way to see if the text was edited?
My idea is to explicitly compare file and edittext character length, but there's something better than this "rude" method?
You can use a TextWatcher:
boolean changed = false;
EditText edit = (EditText)findViewById(R.id.medittext);
edit.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
changed = true;
}
});
Just comparing the length might result in a false negative if the changes are of the same length as deletions in the text.
When the user presses back, just check if changed is true. This might result in a false positive if the user made an edit and then undid it, but it is better to have a few false positives than to lose user changes.
Nothing better actually you have to compare the text and not the length. The user could just replace a word. A TextWatcher would tell you that a user is editing but he just may change and change it back. So you really need to compare strings.

EditText TextChangeListener issue

I have an edit text meant for searching purpose. I have added
searchET.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
//intent to another page where i show my search result
}
});
The problem is:
When I give a search string for eg: "fort" i get the required result in the next page. But when I press the back button, it doesnt move to the previous page on its first click. I will have to press back button 4 times to goto the previous page. This is because my search string is of length 4 and each time a value is entered into the edittext, the textchangelistener is called. How can I solve this issue? Please reply. Thanks in advance.
Depending on what you need, you can go to the next page based on some condition. For example, start a timer, and if the afterTextChanged is called before the timer expires, reset the timer. Alternatively, you can have a button 'Search' where the user explicitly indicates that he's done typing the word.
If you can share the required behaviour, better alternatives can be suggested.
actually when you want to decide when the text ends there is no point of including an addTextChangedListener. To improve the UI you could add this button into ur edit text .refer this : edittext with view

Categories

Resources