user1278696
user1278696

Reputation: 41

How to continuously check for text change event in android ?

How can I make my app keep checking for this? Meaning by keep checking if there is text in there

String str1, str2;

str1 = word.getText().toString();
str2 = answer.getText().toString();

if(!(str1.equals("")) && !(str2.equals("")))
{
  teach.setEnabled(true);
}
else
{
  teach.setEnabled(false);
}

Here is my java code where would i put the fixed code that makes it check and every thing?? Please help!

public class TeachmeDialog extends Activity implements OnClickListener {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.teachme);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    Button teach = (Button)findViewById(R.id.btn_teach_send);
    teach.setOnClickListener(this);


}



@Override
public void onClick(View v) {
    switch(v.getId())
    {
        case R.id.btn_teach_send:
        {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://monaiz.net/get.php");

            String responseStr = "";

            try {
                TextView word = (TextView)findViewById(R.id.tv_teach_request);
                TextView answer = (TextView)findViewById(R.id.tv_teach_response);

                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
                nameValuePairs.add(new BasicNameValuePair("word", word.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("answer", answer.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("action", "teach"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity( );

                responseStr = EntityUtils.toString( entity );

            } catch (Exception e) {
                // TODO Auto-generated catch block
            }

            if( responseStr.equals("ok") )
            {
                Toast.makeText(getApplicationContext(), "Poco just learned a new word!", Toast.LENGTH_LONG).show();
                try {
                    this.finish();
                } catch (Throwable e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

}

Upvotes: 1

Views: 4030

Answers (5)

Manju
Manju

Reputation: 742

In ur question str1 = word.getText().toString(); str2 = answer.getText().toString();

means either word or answer is edittext or textview write TextChanged listeners for that.

Upvotes: 0

Rajkiran
Rajkiran

Reputation: 16191

Use TextWatcher instead. Your "word" seems to be an EditText. So you can do something like this-

word.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) {
                  teach.setEnabled(true);
            }
    });

Upvotes: 2

josephus
josephus

Reputation: 8304

I'm assuming you want to do something when the EditText's text is modified. If so, just use addTextChangedListener

to use this:

        word.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // do whatever you need to do HERE.
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });

Upvotes: 0

npinti
npinti

Reputation: 52185

You could do something like so:

String str1, str2;

while (true) 
{
    str1 = word.getText().toString();
    str2 = answer.getText().toString();

    if(!(str1.equals("")) && !(str2.equals("")))
    {
       teach.setEnabled(true);
       break;
    }
    else
    {
       teach.setEnabled(false);
    }
}

This will create a loop which will keep on going checking some condition. If there strings are not empty, it will stop through the use of the break keyword. That being said, I do not recommend such an approach since it will most likely affect your UI. What I would recommend you do is to attach Focus Lost events to the text fields and make the check when the focus is lost. This will allow you to run the check only when needed.

Upvotes: 2

Chandra Sekhar
Chandra Sekhar

Reputation: 19500

use a while loop with boolean true in the condition, write the logic inside it, where you want to keep checking use continue keyword, where you want to stop the checking use break keyword.

Upvotes: 0

Related Questions