How to set filter to AutoCompleteTextView? - java

I have some data in in arrayList allDoctorsAndSpecialities.
In my Oncreate method I have set adapter and threshold(3) for my autotxt(AutoCompleteTextView)
However,My problem is I don't want to show dropdown for strings entered in edittext like "dr.","dr. ",etc.
Following are solutions I tried
1) code:
autotxt.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
if(autotxt.getText().toString().equals("dr.")){
autotxt.dismissDropDown();
}
}
});
Problem- It does not work combination like 'Docname DocSurname'
2)AutoCompleteTextView filter problem
problem-It did not work for my problem
3)How do I use publishResults() method when extending Filters in Android?
problem-I dont understand how to set constraint for performFiltering method
Please suggest me how to proceed.
Thanks in advance

Related

how to edit 2nd editText only if the 1st is edited in recyclerView

I'm using editText in recyclerView, I want to display or edit the 2nd one only if the 1st is filled android java.
This is a quick solution of mine:
In your adapter, create a flag int currentFilled = -1;
and update this flag whenever your editText is filled
Like this:
EditText edt1 = new EditText(this);
edt1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
#Override
public void afterTextChanged(Editable s) { }
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() >0){
// this is when your edt1 is filled
// edt2.setEnabled(true); // You can enable your edt2 or whatever here
// edt2.setVisibility(View.VISIBLE);
currentFilled = itemPosition;
notifyItemChanged(itemPosition+1) // position you wan to display or edit the edt
} else {
// when edt1 is not filled
/// TODO do what ever you want
}
}
});
Then in your ViewHolder check if currentFilled == itemPosition-1
==>>
edt.setEnabled(true); // You can enable your edt2 or whatever here
// or
edt.setVisibility(View.VISIBLE)
it's may get some issues or not depending on your requirements.
Happy coding :))

Android app reading in multiple variables from one String input

I am trying to read in multiple variables (int, double, String) from one input. However, the input has to be a specific way, i.e. the user should enter either distance in miles by entering “X miles” (either in decimals or as an integer) or time by entering “Y mins” (only as an integer). Here is an example of what I coded. However, enter a double does not work. I also need to use these values in other methods.
public boolean hintWalkTracker() {
// tracks whether the input for walking/running activity is correct
String text = String.valueOf(hintEditText.getText());
String s = text.replaceAll("\\d","");
int i = Integer.parseInt(text.replaceAll("[\\D]", ""));
double d = Double.parseDouble(text.replaceAll("[\\D]", ""));
if ((activityDropDown.getSelectedItem().equals("Walking") || activityDropDown.getSelectedItem().equals("Running")) && s.equals(" miles")) {
d = d * 88.9;
return true;
} else if ((activityDropDown.getSelectedItem().equals("Walking") || activityDropDown.getSelectedItem().equals("Running")) && s.equals(" mins")) {
d = i * 4.78;
return true;
} else if ((activityDropDown.getSelectedItem().equals("Walking") || activityDropDown.getSelectedItem().equals("Running")) && ((!s.equals(" mins")) || !s.equals(" miles"))) {
// create a new AlertDialog Builder
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// set dialog's message to display
builder.setMessage(R.string.walk_missing_message);
// provide an OK button that simply dismisses the dialog
builder.setPositiveButton(R.string.OK, null);
// create AlertDialog from the AlertDialog.Builder
AlertDialog errorDialog = builder.create();
errorDialog.show(); // display the modal dialog
return false;
}
return false;
}
You just need to use a TextWatcher on your EditText in order to check what the user is writing and to react as you want when he write what you need.
Code sample :
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
hintWalkTracker();
}
#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
}
});

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.

Live Search Mechanism in Edittext in Android?

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();
}
});

Categories