Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android sdk prompt toast message in account settings

I'm using an AbstractAccountAuthenticator and I want to single account for my app. So when the user is opting to add a new account for this app I want to prompt a message. I saw other apps use a toast for the message, but for some reasons mine doesn't get displayed.

I display the message like this:

public Bundle addAccount() {
    if (accounts.size() > 0) {
        Toast.makeText(context, R.string.MSG_ONLY_ONE_ACCOUNT_IS_SUPPORTED, Toast.LENGTH_LONG).show();
        return null;
    }
}

Any ideas why? I'm checking the accounts number in the addAccount() method from AbstractAccountAuthenticator.

like image 967
Alex Avatar asked Nov 21 '13 19:11

Alex


People also ask

How do I stop toast messages on android?

You can use toast. cancel() befor showing next toast. cancelling won't reduce the toast time.

How do I put text in toast?

Create a new instance of Toast using makeText() method. Use getView() method to get the view of the Toast. Open MainActivity. java file and add function to show toast message.

What is toaster message?

Toast messages help to deliver simple feedback to the user. They most often follow an action performed by a user and provide information regarding the success or failure of that action. Toast messages are informative, have a lifespan of just a few seconds and take up a very small portion of the screen.


1 Answers

I've been looking around for the same. The following answers have helped me: 1, 2.

Using your code example:

private final Handler handler = new Handler();

public Bundle addAccount(...) {
    if (accounts.size() > 0) {
        final Bundle bundle = new Bundle();
        final String message = 
                  mContext.getString(R.string.MSG_ONLY_ONE_ACCOUNT_IS_SUPPORTED);
        bundle.putInt(AccountManager.KEY_ERROR_CODE, 1);
        bundle.putString(AccountManager.KEY_ERROR_MESSAGE, message);

        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            }
        });

        return bundle;
    }
}
like image 132
Bruno Flávio Avatar answered Oct 06 '22 18:10

Bruno Flávio