tags:

views:

108

answers:

1

Hi,

I want to toast text entered into a texbox using an asynchronous event eg. press button. It compiles without error but nothing happens when the button is pressed. From what I have been able to gather on the forum, my context is wrong. Can someone help me? Below is the code:

mSendButton = (Button) findViewById(R.id.button_send); mSendButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { TextView view = (TextView) findViewById(R.id.edit_text_out); String message = view.getText().toString();

       if(message=="bla")
         {
           Toast.makeText(getApplicationContext(), "Bla was entered",Toast.LENGTH_LONG).show();             
         }                   

       else
         {
           Toast.makeText(getApplicationContext(), "Bye ", Toast.LENGTH_SHORT).show();
         } 
       }
    } 
A: 

Are you sure that you have no stack trace ?

Your code seems right, at least if it's in an Activity.

Suggestions :

  • try with getBaseContext() instead of getApplicationContext()
  • look for stack traces
  • try with Toast.makeText(MyActivityClass.this,"Bye ", Toast.LENGTH_SHORT).show();
dystroy
Or, better yet, just use the `Activity` as the `Context`, rather than trying `getBaseContext()` (not recommended) or `getApplicationContext()` (*really* not recommended).
CommonsWare
Thanks, dystroy. I found the mistake higher up in the code. The call to the function had been masked out. So it was never reaching the "if" statement in the first place.
Juan