Live Search Mechanism in Edittext in Android? - java

I have e project where I have a Edittext from where i have to get input on every keypress or something like every character input.And Have to perform a livesearch. What kind of Listener i have to use to get input for every character input .

Do this way
editSearch.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void afterTextChanged(Editable edit) {
if (edit.length() != 0) {
// Business logic for search here
}
}
});

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.
http://developer.android.com/reference/android/widget/AutoCompleteTextView.html
Here is a good tutorial:
https://developers.google.com/places/training/autocomplete-android
http://android-er.blogspot.com.au/2010/07/example-of-autocompletetextview.html

You need to use 'TextWatcher' as I have already answered here
It will give you result as shown in below images:

Try this..
edittext.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) {
// TODO Auto-generated method stub
String textl = edittext.getText().toString().trim();
}
});

Related

Is there a way I can change the text color of an editText on encountering a word from a string array?

I am trying to implement something similar to a code editor where keywords are automatically highlighted. I am going to have a string array and I want to change the color and font of the editText string when the user types the text and it matches a string from the string array. I am using the addTextChangeListener but the text of the whole editText changes. I want just the matched word to be highlighted. Here is my code:
inputCodeEditText.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) {
if (s.toString().contains("for"))
{
inputCodeEditText.setTextColor(getResources().getColor(R.color.indigo));
}
}
});
I understand I have to use spans but the code crashes. Can anyone help me with the correct usage of spannable strings with addTextChangedListener() ?
Use addTextChangeListener API eg:
editText.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) {
// The changed text comes in "s" parameter.
// Here you can watch the changes and take an action you want...
}
#Override
public void afterTextChanged(Editable s) {
}
});

Create a method that runs when editText is changing

I'm new to Android development and I want to create a method that runs every time my editText is changed.
In swift I use this function:
func fieldChanged(textfieldChange: UITextField){
}
I can't find anywhere a similar function for Java and Android Studio, are there any suggestions?
// Globally declare a variable
private TextWatcher mTextWatcher;
// Call textChanger() method in onCreate
private void textChanger() {
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) {
if (s.length() == 6 && mEditText.getText().toString().trim().equals("some value to check"))
mEditTextOther.requestFocus();
else
mEditText.setError("enter correct");
}
#Override
public void afterTextChanged(Editable s) { }
};
mEditText.addTextChangedListener(mTextWatcher);
}
try this
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});

How can I listen on TextView and make changes to the same TextView in android

I tried the following code, but android doesn't let me do that, as I'd enter into an infinite loop.
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.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) {
//I want to format the already existing text in s in a certain way as the user is typing
mEditText.setText(s.toString() + " \n");
}
});
Please any ideas would be very helpful. Thanks
3 possible solutions :
Check for the carriage return presence in the string
Like this :
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.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)
{
//I want to format the already existing text in s in a certain way as the user is typing
if (s.toString().charAt(s.length() - 1 ) != '\n')
{
mEditText.setText(s.toString() + " \n");
}
}
});
Not the best solution, according to what's in your input string
Do it in beforeTextChanged
Like this :
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.addTextChangedListener(new TextWatcher()
{
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
//I want to format the already existing text in s in a certain way as the user is typing
mEditText.setText(s.toString() + " \n");
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void afterTextChanged(Editable s) {}
});
Cancel the listener, temporarily
Like this :
final TextWatcher tw = 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)
{
//I want to format the already existing text in s in a certain way as the user is typing
mEditText.removeTextChangedListener(tw);
mEditText.setText(s.toString() + " \n");
mEditText.addTextChangedListener(tw);
}
});
mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.addTextChangedListener(tw);
You are most likely entering an infinite loop because you are changing the text in public void afterTextChanged(Editable s) which then causes the method to be called again.
The best solution would be to change the text in one of the other methods, or on the loss of focus of the editable. See View.OnFocusChangeListener for more details on this.
Here is an example:
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
textView.setText(s.toString() + " \n");
}
}
});

Finding text change in EditText using TextWatcher

I'm trying to get what modification user has made to EditText, either insert or delete. I use TextWatcher but I don't get right result, moreover sometimes "getChar(start, end) has end before start" error.
editText = (EditText) findViewById(R.id.MyEditText);
editText.addTextChangedListener(new TextWatcher() {
#override
public void afterTextChanged(Editable s){}
#override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
showToast("text removed: " + s.subSequence(start, count));
}
#override
public void onTextChanged(CharSequence s, int start, int before, int count){
showToast("text added: " + s.subSequence(start, count));
}
}
As you can see I use beforeTextChanged to get any text that's removed by user, and onTextChanged for insertion. Please shed some light here. Thanks!
API is right here: http://developer.android.com/reference/android/text/TextWatcher.html#afterTextChanged(android.text.Editable)
EDIT:
I seem to figure it out...it's quite silly: s.subSequence(start, count)) should really be s.subSequence(start, start+count))
Just keep your functions inside the afterTextChanged and see what happens
Sample Code
seachbox.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
fillData(SEARCH_ORDER ,s.toString());
}
#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
});
I hope it will work
Try this, I'm not sure whether you want the remaining word(after insert/update) or the letter(added/removed).
public class MainActivity extends Activity implements TextWatcher {
private EditText myEditText;
private String inputText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myEditText = (EditText) findViewById(R.id.testEditText);
myEditText.addTextChangedListener(this);
}
#Override
public void afterTextChanged(Editable s) {
if (inputText.length() < s.toString().length()) {
Toast.makeText(
this,
("Text Added: " + s.toString().substring(inputText.length(),
s.toString().length())), Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(
this,
("Text Removed: " + inputText.substring(s.toString().length(),
inputText.length())), Toast.LENGTH_SHORT).show();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
inputText = s.toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}

How to stop the "force close" error

Iam giving some Values in EditText,but if i try to delete it using backspack button and to enter new value..It is getting ForceClosed.How to Rectify this Problem
My code is given below
empty_cyl_recvd = (EditText) findViewById(R.id.empty_cyl_recvd);
new_cyl_recvd = (EditText) findViewById(R.id.new_cyl_recvd);
filled_cyl_unload = (EditText) findViewById(R.id.filled_cyl_unload);`enter code here`
dmg_cyl_recvd = (EditText) findViewById(R.id.dmg_cyl_recvd);
total_dmg_cyl=(EditText)findViewById(R.id.sdff);
total_empty_cyl=(EditText)findViewById(R.id.wertyu);
total_filled_cyl=(EditText)findViewById(R.id.gfhgftg);
empty_cyl_recvd.addTextChangedListener(new TextWatcher()
{
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
int a=Integer.parseInt(empty_cyl_recvd.getText().toString()); int b=Integer.parseInt(Util.EMPTY_LIST.get(0).toString());
int c=a+b;
total_empty_cyl.setText(""+c);
}});
filled_cyl_unload.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) {
int a=Integer.parseInt(filled_cyl_unload.getText().toString());
int b=Integer.parseInt(Util.FILL_LIST.get(0).toString());
int c=a+b;
total_filled_cyl.setText(""+c);
});
dmg_cyl_recvd.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) {
int a=Integer.parseInt(dmg_cyl_recvd.getText().toString());
int b=Integer.parseInt(Util.DAMAGE_LIST.get(0).toString());
int c=a+b;
total_dmg_cyl.setText(""+c);
});``
1st of all i would suggest you to use debugger, that would help you catch such issues quickly.
in the code of onTextChange there are possibility of exceptions (NPE/NumberFormat/ArrayIndexOutOfBounds). So to put appropriate check around this code
int a,b;
if(dmg_cyl_recvd.getText() != null && !dmg_cyl_recvd.getText().trim().equals("")) // also put it in try-catch for the case the input is not numeric
try{
a=Integer.parseInt(dmg_cyl_recvd.getText().toString());
}catch(NumberFormatException ex){
//alert user about wrong input
}
if(Util.DAMAGE_LIST.size() > 0)//assuming it is a list, if not use method that gives the size of corresponding collection
b=Integer.parseInt(Util.DAMAGE_LIST.get(0).toString());
Add proper exception handling to avoid Forced Close. You are probably getting NumberFormatException on Integer.parseInt(..) when it gets empty/invalid text.

Categories