1

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

}

Amit
  • 13,134
  • 17
  • 77
  • 148
user1278696
  • 41
  • 1
  • 4

5 Answers5

2

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.

npinti
  • 51,780
  • 5
  • 72
  • 96
2

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);
            }
    });
Rajkiran
  • 15,845
  • 24
  • 74
  • 114
0

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.

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • @amit may I know the reason behind the hang. – Chandra Sekhar Mar 20 '12 at 07:02
  • While (true) {} will be running till the break condition is reached... However 1. the OP wants to check whenever the text changes (or focus lost event occurs, efficient than the text change event), in that case the listener will be more appropriate as suggested in other answers.. 2. the UI therad will hang till the loop finishes ... – Amit Mar 20 '12 at 07:10
0

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) {
            }
        });
josephus
  • 8,284
  • 1
  • 37
  • 57
0

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.

Manju
  • 720
  • 9
  • 23