Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Threads and Services Threads in Android Threads and Runnables

Trainer Workout
Trainer Workout
22,341 Points

Toast does not show after Button click

Any ideas why the Toast does not show on Button click ?

(...)
public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();
    private Button downloadButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        downloadButton = (Button) findViewById(R.id.download_button);

        downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = getString(R.string.downloading);
                Log.d(TAG, message);
                // Toast does not show after Button click
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
                downloadSong();
            } // onClick()
        }); // OnClickListener()
    } // onCreate()

    private void downloadSong() {
        (...)
    } // downloadSong()
} // MainActivity

2 Answers

Trainer Workout
Trainer Workout
22,341 Points

The problem is explained in the video by Ben. To solve this, the downloadSong() method has to be called in a separate thread than the UI thread. In order to this, we need to create a Runnable and start a Thread using that runnable.

downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = getString(R.string.downloading);
                Log.d(TAG, message);
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();

                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        downloadSong();
                    } //run()
                }; // Runnable()

                Thread thread = new Thread(runnable);
                thread.setName("DownloadThread");
                thread.start();
            } // onClick()
        }); // OnClickListener()
Anastasiia Karpenko
Anastasiia Karpenko
2,413 Points

Also do not forget to include android:onClick="true" to the Button view in xml file. After I included it , the toast became visible after I clicked the button