I am making a program in android using EditText and ListView. I want to search the ListView item using EditText above. After populate data to ListView, when user type text in EditText, the ListView will scroll to the position start with that text. Example: I have item: [apple, application, book, boy, car, cat, cash.....] when I type b in EditText then ListView will scroll to book. I want to use the listview.setSelection(position), but I don't know how can I get the position from my EditText search. I use the code below, it work well, but it seem slow when we search in EditText. How can I do this and run smoothly?
Thanks in advances.
I use following code:
YOUR_EDITTEXT.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
//LOGIC MAY DIFFER ACCORDING TO YOUR REQUIREMENT..
int POSITION = 0;
for(int i =0;i<list.size();i++) {
if(list.get(i).startsWith(s.toString()))
{
POSITION = i;
break;
}
}
listview.smoothScrollToPosition(POSITION);
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
});
As you have 20,000 list entries, what you currently do is bound to be slow, as you do up to 20,000 startsWith() operations, everytime the user changes the text.
So what can you do:
Simplest solution:
Do NOT select an entry while the user types. Let him finish and click a search button and only then do what you currently do for every text change.
Database solution:
If you have your entries in a database, make a query to get the position. This is much much faster than searching in code. If you don't, can you put it into a database? Is it worth the effect or would the 'Simplest Solution' be good enough?
Try to be smart:
You can try to collect information while the user types, and use that for your advantage. Say the user begins to type Hello. When H is entered you find the first item that starts with H, say at position 1000. Now when the user types e, you don't have to start from the beginning but can start at position 1000 and so on. Of course you also have to handle cases when the user deletes a character or pastes another text and so on, so there is quite some programming logic involved here.
Related
How to make a custom Data Picker like Date Picker in Android so that I can get data (int data) by custom input or by using the buttons (- & +) so that it become easy for user to enter larger value !!
Has anyone any idea how to implement this?
please provide example, code snippet if possible.
I want to make like this
left - Button
middle - EditText
right - Button
I want Like this image--
This is the example of Date Picker in Android:-
then try this link
try on text changed listener as described in the link to update the variable whenever user changes the edit text, then variable will hold the changed value and so you can use your method to change values of that variable
Design a layout as you want with two buttons and one edittext in middle
Then give one button addition funtionality and other substraction functionality
Follow little code snippet, provided below
addButton.setOnClickListener(new OnClickListener(){
public void onClick(){
int valueInEditText = Integer.parseInt(etReference.getText().toString());
etReference.setText(String.valueOf(valueInEditText + 1));
}
});
Similaryly, do it for substration button.
Note: Do validation if you don't want negative values.
You might want to use the NumberPicker which will allow you to select any value.
It's not the same look but it might work for you:
https://android--examples.blogspot.com/2015/05/how-to-use-numberpicker-in-android.html
After reading through various posts, examples, articles and Android documentation I found solution to my own question, It can be achieved by two ways:
First by using NumberPicker
Secondly by adding TextChangedListener to the EditText
Here is the code how I achieved it :
int noOfCups = 2;
EditText editTextNoOfCups;
editTextNoOfCups = (EditText) findViewById(R.id.edit_text_no_of_cups);
editTextNoOfCups.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) {
if(s.length() != 0) {
Log.e("pss", String.valueOf(s));
noOfCups = Integer.parseInt(String.valueOf(s));
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Here is the link to official Android documentation :
TextWatcher | Android Developers
now i am making a program in android using edittext and listview. I want to search the listview item using edittext above. After populate data to listview, when user type text in edittext, the listview will scroll to the position start with that text. Example: i have item: apple, application, book, boy, car, cat, cash..... when i type b in edittext then listview will scroll to book. I use the listview.setSelection(position), but because the amount of my data is over 30,000 , so when i use the following code it is slow to find data.
Are there any solutions or any other method to do this?
Here is my code:
YOUR_EDITTEXT.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
//LOGIC MAY DIFFER ACCORDING TO YOUR REQUIREMENT..
int POSITION = 0;
for(int i =0;i<list.size();i++) {
if(list.get(i).startsWith(s.toString()))
{
POSITION = i;
break;
}
}
listview.smoothScrollToPosition(POSITION);
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
});
You could take a different approach and load your list data with an AutoCompleteTextView. This would provide your required functionality and it's designed to handle large data sets.
Example:
https://developers.google.com/places/training/autocomplete-android
Is your list sorted? If it is, you can use binary sort on the datasource.
Hi I am learning Android programming and have run into an issue that I couldn't get a clear answer to through researching.
I have a TextView which serves as a label for my EditText. I have a method which checks if the EditText is an empty String. If the string is empty I want to be able to get a reference to the TextView that corresponds to that EditText in order to make a toast saying something like "please enter a value for ".
I've looked into getLabelFor/setLabelFor but is there a way to do this in the layout XML?
What is best practice for this type of functionality.
You're describing a functionally that is build in to EditText. There is a special field you can define in xml called hint, which is the recommended way to label an EditText rather than a nearby TextView. Additionally, EditText has a method called setError() (link). If the user attempts to hit a submit button, for example, you can check to see if the EditText is empty and if so, call setError().
I wonder if the following is the thing that you need
TextWatcher inputTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().equals("")) {
textView.setText("please enter a value for ..");
} else {
textView.setText("<the textedit is not empty>");
}
}
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(inputTextWatcher);
I'm using Android's AutoCompleteTextView with a CursorAdapter to add autocomplete to an app. In the view's onItemClickListener() (i.e. when the user touches one of the autocompleted drop down items) I retrieve the text and place it in the EditText so that the user can modify it if they need to.
However, when I call setText() on the TextView the autocomplete behavior is triggered and the dropdown shows again. I'd like to only show the dropdown if the user types new text with the keyboard. Is there a way to do this?
You can use the dismissDropDown() method of the AutoCompleteTextView object. Take a look at the documentation.
When we click on item suggested in AutoCompleteTextView.onTextChanged() is performed before onItemClick
So, to avoid this try below code..
autocompletetextview.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (autocompletetextview.isPerformingCompletion()) {
// An item has been selected from the list. Ignore.
} else {
// Perform your task here... Like calling web service, Reading data from SQLite database, etc...
}
}
#Override
public void afterTextChanged(final Editable editable) {
}
});
If you wish to dissmis AutoCompleteTextView's dropdown you should use its post(Runnable r) method. It works for me :)
Here is an example:
mAutoCompleteTextView.post(new Runnable() {
public void run() {
mAutoCompleteTextView.dismissDropDown();
}
}
Answering my own question after a couple hours of hacking at this: It turns out you should implement your own OnItemClickListener and instead rely on the existing click listener to populate the TextView. I had originally implemented the onItemClickListener because it was using the results of Cursor.toString() to populate the text view. To change the output String, you should implement convertToString(Cursor) in your CursorAdapter. The CharSequence that gets returned will be populated in the text view.
Doing this will also prevent the dropdown from showing up again (since setText() triggers the completion behavior but the default onItemClickListener does not).
Different approach.
I agreed dismissDropDown() works but in my case, it wasn't working as expected. So, I used:
autoCompleteTextView.setDropDownHeight(0);
And if you want to show the dropdown list again, you an use
autoCompleteTextView.setDropDownHeight(intValue);
I want to cause the focus of one edit text box to move to another on editting (meaning you can only type on letter before it automatically moves on to the next edit text).
It's the "on edit" that I can't get my head around. Can anyone help me out with a simple example? Theres a lot I need to implement it into, so just a basic understanding should set the ball rolling ^_^
I do not really recommend this. With soft keyboards and multiple languages, what exactly is "one letter"? After all, a soft keyboard might enter in an entire word, like it or not.
CommonsWare makes an excellent point: you can't prevent the user from adding more characters to the EditText box, however you can listen to what's changed and act on that. Here's how to:
EditText editbox = (EditText) findViewById(R.id.MyEditBoxName);
editbox.addTextChangedListener(new TextWatcher()
{
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void afterTextChanged(Editable s)
{
// Test s for length, request focus for the next edit.
// editbox2.requestFocus();
}
});
Be careful not to get yourself into an infinite loop changing the editbox, any changes you make will cause these methods to be called again recursively.