ProgressBar with EditText - java

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 ...

Related

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

I want to make something when done from typing EditText

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

Play sound when typing in EditText in android application

Hi i am making an android application.
I want to play a default sound effect when i am typing in an EditText field.
Any ideas how to do that?
Thanks
you can use textwatcher for that purpose
youredittext.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) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.yourmp3); // add mp3 file
mp.start();
}
});
Note : add yourmp3 in res->raw folder

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

Using keypress in android

I am creating an android application, a converter. Every time I am going to press any number, I want it to be displayed automatically on a textfield. I don't know how to use the keypress in android application. Is it just like using keypress in a simple java program, let's say ran on netbeans?
Um ...
public class MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(new EditText());
}
}
Your question is not very clear.. but i think you are saying that your layout contain a EditText and TextView, when user type in a number in EditText at same time the TextView should also set the same text at rumtime. if this is your requirement then you can do it as follows:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.main_activity);
TextView tv = (TextView)findViewById(R.id.textview1);
EditText et = (EditText)findViewById(R.id.edittext1);
et.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
tv.setText(s);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void afterTextChanged(Editable s)
{
}
});
}
}

Categories