Android - In-line autocomplete with EditText - android

I want to get an in-line autocomplete with an EditText, not a result list but the best suggestion directly in EditText.
Something like this : In-line auto-complete (near the bottom of the page).
Is it possible in Android ?
Thank you.

I have no eclipse now so I will try to give you some hints.
To create a custom autocomplete I would do something like this.
First
In the view layout add the EditText and an OutputText (this with visibility=hidden)
Second
In the activity create a TextWatcher and implement the method afterTextChanged.
Inside this method call a service with the input text and then update the content of the outputText.
Something like:
afterTextChanged(Editable s){
// you know your input is an EditText
final EditText input= (EditText) s;
// TODO make this call async
String suggestedText= someService.getSuggestion(input.getString());
outputText.setText(suggestedText);
outputText.setVisibility(View.VISIBLE);
// to avoid infinite loops
if(suggestedText!=null && !"".equals(suggestedText) && !suggestedText.equals(input.getString())
{
// add a onclick control to update the input
outputText.setOnClickListener(new View.OnClickListener(){
editText.setText(suggestedText);
});
}
}
Third
Implement the suggestion service.

Android has an AutoCompleteTextView which should do the job you want.
According to the official Android docs
"AutoCompleteTextView is an editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key.
The list of suggestions is obtained from a data adapter and appears only after a given number of characters defined by the threshold."
For an example code snippet refer to AutoCompleteTextView

For autocomplete you must another type of EditText called AutocompleteTextView or MultiAutocompleteTextView. Here you can find simple example for that option.
P.S. if you want to create your own type of List Filtration, your Adapter class must implement Filterable interface

Related

Update text watcher based on other events

In one of my screen i am using text watcher to check value against a range and for invalid range color of text is being changed.
Now there is one more requirement.
Along with EditText i need to use CheckBox to set completely different range.
The problem i am facing is that changing the checkbox value requires to call afterTextChanged in which i have put all validations for both set of ranges.
So basically my requirement is to update textWatcher anyhow so that afterTextChanged get called after i change value of checkbox.
I get a strange feeling that i am forgetting something very simple here,if so please let me know.
Thanks in advance.
Perhaps move your code from the callback in the textWatcher and put it into another function ie
private void checkRange() {
// do checks here
String color = text.getEditableText().toString();
}
and call this method from your textWatcher and from the listener on the checkbox.

Editable textview on click

I have a screen with textviews now i want to make this editable on click of that
i tried one solution using edittext making it as transparent background but initially it will show cursor and the click is not recognizing properly,if i set focusbaleintouchmode to false in xml it is not getting focus.but some how the click is not working properly as expected.first is this correct approach?
expected result is textview should be there once user clicks on it it should be editable once user clicks outside it it should be not editable. any sample code will helps me a lot.sorry for my english
Thanks in advance
finally i got one solution using below code
in xml edit text i gave foucasbletouchmode to false which makes click works properly after that with in onclick
et.setFocusable(true);
et.setEnabled(true);
et.setFocusableInTouchMode(true);
et.requestFocus();
to lose focus
et.setFocusable(false);
et.setClickable(true);
et.clearFocus();
You can use the below code :
private makeEditable(boolean isEditable,EditText et){
if(isEditable){
et.setBackgroundDrawable("Give the textbox background here");//You can store it in some variable and use it over here while making non editable.
et.setFocusable(true);
et.setEnabled(true);
et.setClickable(true);
et.setFocusableInTouchMode(true);
et.setKeyListener("Set edit text key listener here"); //You can store it in some variable and use it over here while making non editable.
}else{
et.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
et.setFocusable(false);
et.setClickable(false);
et.setFocusableInTouchMode(false);
et.setEnabled(false);
et.setKeyListener(null);
}
}

How to know, from addTextChangedListener, how the text has changed

I have a screen with an EditText and a ListView. As you type into the EditText, an AsyncTask is launched (any existing AsyncTask is cancelled and tossed) to query locally for some data to display in the ListView. If the EditText is empty of text, all data is queried for and displayed. Essentially, the EditText is a filter of the data on the screen. This is easy to do. In the addTextChangedListener, simply cancel your previous AsyncTask, launch a new one, and pass to it s.toString().
When the user hits the backspace to erase some text in the EditText, I want the default behavior (where all data populates the list). This works with my current implementation of addTextChangedListener.
However, I also have an X button on the right side of my EditText. This is intended to clear out the EditText (EditText.setText("");). This is a special case. In this case, I do not want the query to happen. The problem is, addTextChangedListener does not seem intelligent enough to know this. It gets triggered because the text has changed and all data is queried for.
How do I prevent this?
Use a boolean ignoreNextTextChangeKThxBye data member, setting/clearing that boolean as appropriate and using an if() statement to avoid doing the query.

Android - Forms

on my app a have a form which contains 2 fields and a save button.
What do i need at the end of my onClick to return the cursor back to the first field.
i have this to clear them both
txtData.setText("");
txtData2.setText("");
but the cursor always stays on the bottom field
regards
Have you tried progamatically calling requestFocus on the view you want to have focus after the user clicks on your save button? For instance, you could in your onClick method do something like:
public void onClick(View){
//do other stuff like saving and clearing the fields
//then request the focus be switched back to the first text field.
txtData.requestFocus();
}

Android: How to enable my button back if EditText is not empty?

I have 2 EditTexts; 01 and 02. My button will be disabled once the activity is started and when these two EditText contain text, the button has to be enabled again. However my button is always disabled and can't enable it using button.setEnabled(true);.
Can anyone help me with this?
summit.setEnabled(false);
buttonEnable();
public void buttonEnable(){
if (feedback.length()>0 && email.length()>0){
summit.setEnabled(true);
}else{
summit.setEnabled(false);
}
}
You're correct about needing a TextWatcher. The afterTextChanged(Editable) method is the one you're interested in for something like this. Call your buttonEnable() method from it, and add the TextWatcher to any applicable text fields. (Looks like feedback and email from your sample.)
One easy way can also be to set onKeyListener to your editText(), then if there is something in editText(), set button enable if nothing disable it.

Categories

Resources