jpgerb
jpgerb

Reputation: 1120

Toast not displaying onButtonClick

I have a toast that should display anytime my "submit" button is pressed:

Here is my code:

@SuppressLint("ShowToast")
    public void ButtonOnClick(View view){
        SharedPreferences sharedPref= getSharedPreferences("chaosautoreply", 0);
        SharedPreferences.Editor editor= sharedPref.edit();
        TextView tvMessage = (TextView) findViewById(R.id.editMessage);
        String message = tvMessage.getText().toString();
        editor.putString("message", message).commit();
        Toast.makeText(getApplicationContext(), "Updated Successfully", 10);
    }

Here is my layout:

    <Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editMessage"
    android:layout_centerHorizontal="true"
    android:clickable="true"
    android:onClick="ButtonOnClick"
    android:text="Submit" />

There are no errors or logcats to display. The submit appears to function, but it doesn't Toast.

Upvotes: 0

Views: 122

Answers (4)

Hamad
Hamad

Reputation: 5152

its because, You have to call the show method on Toast to make it display something like:

Toast.makeText(getApplicationContext(), "Updated Successfully", 10).show();

Upvotes: 4

Nathan Walters
Nathan Walters

Reputation: 4136

You need to call show() on the Toast after making it, otherwise it will never be displayed. I'm curious as to why you suppressed the warnings for this; if you had simply listened to Eclipse, you would have seen the error and been able to correct it yourself.

Upvotes: 2

Sayed Jalil Hassan
Sayed Jalil Hassan

Reputation: 2595

You have to call the show method on Toast to make it display something

    Toast.makeText(getApplicationContext(), "Updated Successfully", 10).show();

Upvotes: 4

Nitin Misra
Nitin Misra

Reputation: 4522

Toast.makeText(getApplicationContext(), "Updated Successfully", 10).show();

Upvotes: 5

Related Questions