I want to make something when done from typing EditText - java

what is the method I should use to type what will happen after being finished from typing in the EditText?
Please use simple statements and use an example with comments of what happens in that line and what to type in that line.
PS
I wish you got the lines above!!!

you need to add a text watcher to the text view
Example:
myEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Your query to fetch Data
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
// Your logic here
}
});

just add a texcchangedlistener with textwatcher to your edittext
https://developer.android.com/reference/android/text/TextWatcher.html
myeditext.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s)
{
//do something here!!!
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
}
});

Related

EditText On update/onChange value Add listener

I have 2 EditText in my activity,lets say editText1 ,editText2 and A Double Variable d=200
I want when user change/insert value for editText1 ,editText2 value will update in real Time as editText1*d
Also when user change/insert value for editText2 ,editText1 will be update in real time as editText2*d
I tried to use addTextChangedListener->onTextChangedbut it works fine for one Edit text,when I set this function for both editText then application crash
because its creating an infinite loop,how can I solve this problem?
update :bellow is my code
et1.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) {
Editable a = et1.getText();
Double aa = Double.parseDouble(a.toString())*100;
String aaa = aa.toString();
et2.setText(aaa);
}
#Override
public void afterTextChanged(Editable s) {
}
});
et2.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) {
Editable b=et2.getText();
Double bb=Double.parseDouble(b.toString());
String bbb=bb.toString();
et1.setText(bbb);
}
#Override
public void afterTextChanged(Editable s) {
}
});
EditText et1, et2;
TextWatcher watcher1 = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Editable a = et1.getText();
Double aa = Double.parseDouble(a.toString())*100;
String aaa = aa.toString();
et2.removeTextChangedListener(watcher2);
et2.setText(aaa);
et2.addTextChangedListener(watcher2);
}
#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
}
};
TextWatcher watcher2 = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Editable b=et2.getText();
Double bb=Double.parseDouble(b.toString());
String bbb=bb.toString();
et1.removeTextChangedListener(watcher1);
et1.setText(bbb);
et1.addTextChangedListener(watcher1);
}
#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
}
};
et1.addTextChangedListener(watcher1);
et2.addTextChangedListener(watcher2);
i think you have to create textwatcher separately then in your TextWatcher_EdOne
TextWatcher TextWatcher_EdOne = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//disable editext2 textwatcher
et2.removeTextChangedListener(TextWatcher_EdTwo);
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
///do your stuff
}
#Override
public void afterTextChanged(Editable s) {
// enable textwatcher on et2 again
et2.addTextChangedListener(TextWatcher_EdTwo);
}
}
and will do it for the other textWatcher , this way you will avoid infinite loop
You should implement a TextWatcher, It works pretty much like how it sounds. When the user changes (adds/removes text from) what is existing in the EditText, it will "Activate" this class. To test it out, you can use logs or set a breakpoint at any one of these statements.
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Your action here
edit = fromEditText.getText().toString();
}
#Override
public void afterTextChanged(Editable s) {
}
}
and in onCreate, add this to connect your EditText to the TextWatcher
fromEditText.addTextChangedListener(new MainActivity.MyTextWatcher(fromEditText));
You can add a bunch of EditText's to a single TextWatcher. All you need to do is rinse and repeat. Just change the name of the respective EditText and add it to the TextWatcher. Be aware, that if you have some EditText change the text of another EditText, it can result in an infinite loop.

Disable button upon removing textview content

public class ActivityMain extends AppCompatActivity {
Button button;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button_chatbox_send);
button.setEnabled(false);
tv= findViewById(R.id.edittext_chatbox);
tv.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {
if (charSequence.toString().equals("")) {
button.setEnabled(false);
} else {
button.setEnabled(true);
}
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
}
});
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
//Call other method with string from text view as parameter
}
}
});
}
I added a TextChangedListener to my TextView to disabled the Button, while the text view contains no string. During runtime, after I entered a string into the TextView, the Button is still enabled, even if I deleted all the text. How do I solve this problem, the method which I use during on click can not work with an empty string?
Edit
Problem is solved ty.
Update your button in onTextChanged or after textChanged
tv.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
button.setEnabled(!TextUtils.isEmpty(s.toString())); // Update button here OR
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
button.setEnabled(count>0); // Update button here
}
});
move it to afterTextChanged
#Override
public void afterTextChanged(Editable editable) {
button.setEnabled(!TextUtils.isEmpty(editable.toString()));
}
You should disable the button inside the onTextChanged method of the TextWatcher -
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
button.setEnabled(charSequence.toString().length() > 0);
}

How to add a search engine feature in an Android application?

I need to create a search engine feature for my application. It should search particular websites. Is there any way to develop this in Android.
I need to add this as a feature in my Android application.
initialize in your activity edittext for me is it address_search_view
addresses_search_view.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Addresses.this.addressesSAdapter.getFilter().filter(s);}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});

Is it possible to use [TextWatcher for button disable control] for two EditText?

I'm using this code below, to make my button disabled until first input box received some numbers.
Problem is when I press the button before second input recieve numbers, then app is crashing.
First input var is firstEdittext and second is secondEditText
firstEditText.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) {
Btn.setEnabled(!(firstEditText.getText().toString().trim().isEmpty());
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
Is it possible to use this TextWatcher for both?
Or I need to use another method for make my button disable
The error is caused by trying to parse empty text from secondEditText. In this case, you need to set the TextWatcher to both EditTexts.
You can do it by defining a named TextWatcher in the class, then set both EditTexts to use this. You also need to fix the checking condition for enabling the button.
Inside onCreate():
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) {
Btn.setEnabled(!TextUtils.isEmpty(firstEditText.getText()) && !TextUtils.isEmpty(secondEditText.getText()));
}
#Override
public void afterTextChanged(Editable s) {
}
};
firstEditText.addTextChangedListener(tw);
secondEditText.addTextChangedListener(tw);

ProgressBar with EditText

I'm making a search box. when user types down something in here, I'll show suggestions which take a bit amount of time. Considering the buffering, I'd like to show progressbar for users.
To make this feature, I made AsyncTask to show up progressbar and TextWatcher of search box to execute async task.
The problem is that simply, progressbar doesn't show up. As far as I guess, this is probably because TextWatcher doesn't update UI.
Is there any solution to help me out?
Below is the code I've made.
Any suggestions will do. Thanks.
mSearchEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
mProcessTask = new ProgressTask();
mProcessTask.execute();
"do something taking time here"
}
And below is the code for AsyncTask
private class ProgressTask extends AsyncTask <Void,Void,Void>{
#Override
protected void onPreExecute(){
super.onPreExecute();
mProgressBar.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... arg0) {
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPreExecute();
mProgressBar.setVisibility(View.GONE);
}
}
I've found the possible solution.
The code attached in the question starts working properly when I changed full screen mode to no titlebar mode.
mSearchEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
mProgressBar.setVisibility(View.GONE);
}
Why dont you use the progressbar in the textwatcher itself ...

Categories