creatice1933
creatice1933

Reputation: 1

Toast Messages do not show up

Hey I have a problem with toast messages which does not show up. I'm creating an app which displays user‘s message into Morse code and I want toast message show up to inform which character is diplayed now. But when I put it like below, toast messages do not show up. It's probably because next function which is called are somehow killing the previous one, cause when I removed other commands and left only showStatus() the messages appeared. How can I deal with this situation?

public void dispDot()
    {
        final Parameters pM = cameraMorse.getParameters();
        pM.setFlashMode(Parameters.FLASH_MODE_TORCH);
        cameraMorse.setParameters(pM);
        MorseActivity.backgroundMorse.setBackgroundResource(R.drawable.background1on);
        waitFor(1);
        pM.setFlashMode(Parameters.FLASH_MODE_OFF);
        cameraMorse.setParameters(pM);
        MorseActivity.backgroundMorse.setBackgroundResource(R.drawable.background1a);
    }
    //function that displays the line in Morse code
    public void dispLine()
    {
        final Parameters pM = cameraMorse.getParameters();
        pM.setFlashMode(Parameters.FLASH_MODE_TORCH);
        cameraMorse.setParameters(pM);
        MorseActivity.backgroundMorse.setBackgroundResource(R.drawable.background1on);
        waitFor(3);
        pM.setFlashMode(Parameters.FLASH_MODE_OFF);
        cameraMorse.setParameters(pM);
        MorseActivity.backgroundMorse.setBackgroundResource(R.drawable.background1a);
    }

    public void showStatus(char character)
    {   
        //status1.setTextColor(Color.WHITE);
        //status1.setText("Status: Now displaying "+character);
        toast = Toast.makeText(MorseActivity.this, "Displaying "+character,Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, 30);
        toast.show();
    }

    public void morseMessageTranslator()
    {
        morseMessage = textBox.getText().toString();
        morseMessage = morseMessage.toUpperCase();
        if(morseMessage.length()>0)
        {
            char character;
            for(int a=0;a<morseMessage.length();a++)
            {
                character=morseMessage.charAt(a);
                switch (character)
                {
                case 'A': //.-
                    showStatus('A');
                    dispDot();waitFor(1);dispLine();waitFor(1);
                    break;
                case 'B': //-...
                    showStatus('B');
                    dispLine();waitFor(1);dispDot();waitFor(1);dispDot();waitFor(1);dispDot();waitFor(1);
                    break;

UPDATE: Ok it turns out that waitFor() function is the cause.

public void waitFor (final int time)
    {
        Thread waitthread = new Thread(new Runnable() { 
            // After call for background.start this run method call
            public void run() {


                    try {
                        Thread.sleep(time*500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
    waitthread.start();
    try {
        waitthread.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

But still don't know how to show toast before the wait is launched.

Upvotes: 0

Views: 1090

Answers (2)

AbdullahADhaim
AbdullahADhaim

Reputation: 65

Add .show() to the end of you toast codes

toast = Toast.makeText(MorseActivity.this, "Displaying "+character,Toast.LENGTH_LONG).show();

Upvotes: 0

Alex Curran
Alex Curran

Reputation: 8828

As your comments on this question show, the reason your Toasts aren't showing is because of your waitFor() method. As described, this performs a pointless calculation, which a) wastes precious CPU time (and therefore battery) and b) performs this on the UI thread. This means that anything that should happen on the UI thread won't happen all the time waitFor() is running, including Toasts.

You'll have to include some sort of threading here to get over this issue (the Android Developers website has a good tutorial). You'll want the dispDot, dispLine and waitFor calls to happen on a background thread. Bear in mind that if any of these three methods interact with your UI, they must do that back on the UI thread (see Communicating with the UI thread in the linked tutorial).

Previous (wrong) answer

You're creating your Toasts, but not calling show() on them! It's a very easy mistake to make. Just change the line:

toast = Toast.makeText(MorseActivity.this, "Displaying "+character,Toast.LENGTH_LONG);

to

toast = Toast.makeText(MorseActivity.this, "Displaying "+character,Toast.LENGTH_LONG).show();

Upvotes: 2

Related Questions