Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1046373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:08:12+00:00 2026-05-16T16:08:12+00:00

I want to show a Toast right after the user clicks on a CheckBoxPreference

  • 0

I want to show a Toast right after the user clicks on a CheckBoxPreference in my PreferenceActivity.

myCheckBox.setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Toast.makeText(Prefs.this,
                        "test",
                        Toast.LENGTH_SHORT).show();
                doSomething();
                return false;
            }
        });

I also tried to put the Toast into the doSomething() method, but it’s always shown after the whole method is processed. I tried getBaseContext() instead of Prefs.this, but it didn’t help.
Any idea why the Toast doesn’t show up at once and how to make it do so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:08:13+00:00Added an answer on May 16, 2026 at 4:08 pm

    This is happening because the onPreferenceClick listener is running in the UI thread. This thread is also the same as the one that handles displaying the Toast. Toast#show only pushes a message onto the message queue that will then run code to make the Toast display. That queue won’t be processed until after your onPreferenceClick handler is completely finished.

    You can try:

    myCheckBox.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    Toast.makeText(Prefs.this,
                            "test",
                            Toast.LENGTH_SHORT).show();
    
                    Prefs.this.runOnUiThread(new Runnable() {
                        @Override
                            public void run() {
                                doSomething();
                            }
                        });
                    return false;
                }
        });
    

    This will cause the Toast to post to the message queue then your doSomething will also be posted to the queue after the toast. The downside to this is that there could be UI messages that will be handled before doSomething is called. Also, if doSomething is long running it will monopolize your UI thread and could cause a possible ANR force close. You may want to think about running doSomething in an AsyncTask if it takes more than 150ms or so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't want to show any toast, notification and any dialog, want to send
I want show an information page after success db opartions. And after the info
I want show annotation view (above one of my pin) after load map. For
I have a Preferences Activity in my code. I want to show a toast
I have an Activity where I want to show a ProgressDialog. I override onCreateDialog
I'm connecting to bluetooth in background and I want to show alert or toast
/* In onclick function */ Toast.makeText(this,you are right!,Toast.LENGTH_SHORT).show(); timer(); compare(); /* onclick function ends
I want to show an image as a toast instead of plain text message.
hii I want show a pop up Dialog box when i click on to
I'm using Drupal 7 and Views module in my site. And want show number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.