Specify addTextChangedListener in XML - android

Is there any property for EditText I can specify TextChangedListener? I know it has 3 methods, so I want just specify value of property like below
<EditText
android:afterTextChanged="myAfterHandler"
However I am getting compilation errors. I can add on click listener in same way so I am wondering what is wrong on text changed?
I know I can obtain view at certain time in Java code and call add listener, but this solution looks really ugly.

Android's Data Binding Library now supports generating xml attributes for setters that exist in the view's class.
If you have data binding enabled, all you need to do is use the app namespace (don't forget to declare the namespace), so something like app:addTextChangedListener="#{someobject.textwatcherfield}"

For use of the TextWatcher..
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) {
// TODO Auto-generated method stub
}
});

There is no such property of TextChangedListener that you can specify in xml file.
You have to add textchangedListener to your edittext in Java class as described in another answer to this post.
You can just give onclick property to your views in xml file.
You can ask if you have any further queries :)

Related

Android search list invisible by default? Can it be done?

Hi guys I have a question:
In short, I have a listview, with a searchbar on top of it, I am also able to start my other activities after filtering the results etc; basically everything works fine and sweet.
My question is this:
When launching the main activity the listview is visible (obviously enough). Is there a way to make the listview invisible and only after typing in the searchbar to make the results of the listview become visible? Something like an in app search thing; or am I just imagining things?
I hope that I am not too confusing with what I wrote above.
I am looking forward to your replies and I thank you in advance.
Go this way:
set your listview visibility to gone or invisible from your xml file:
android:visibility="gone"
Now apply textwatcher on searchbar edittext
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
//put your search logic here and populate listview.
//after populating listview set its visibility to visible
listView.setVisibility(View.VISIBLE);
//and set listview visibility to GONE again when user erase all text from search edittext
if(s.length() == 0){
listView.setVisibility(View.GONE);
} else {
listView.setVisibility(View.VISIBLE);
}
}
#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
}
});

Android Listview Searching to the specific position

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.

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.

How to overwrite value in edittext

I have an edittext in my app, which accepts only numbers. It is set to 0 by default. But when i enters some umbers, i want to overwrite the initial 0 in the left. Now i am having some problem with edittext. For example, when i inserts a 1, i want it to how 1, not 01. my code is like,
value = (EditText)findViewById(R.id.value);
value.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
if(arg0.length()==0)
{
value.setText("0");
int i = value.getText().length();
value.setSelection(i);
}
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
How can i achieve it? or is it possible?
You can set android:hint in your xml file.
For example,
<EditText
android:id="#+id/value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="0"/>
There are lots of options to accomplish what you're looking for. Besides the ones already mentioned, I'd say the EditText's attribute android:selectAllOnFocus="true" might be worth looking at. It's convenient for a default value, and at the same quite user friendly as changing the value does not require any 'backspacing'.
You can change string to empty in your beforeTextChanged Function Call and let the onTextChanged as it is.
Instead of using value.setText("0");
use value.setHint("0");
Use setHint(int) method to display 0 initially. So that when the user types something it is automatically cleared and you can get the text as desired.

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