0

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.

  • 1
    Can you remove the `setGravity` method and try again? – boxed__l Jan 12 '14 at 14:04
  • recommend you make this data driven, the dot/line pattern should be defined in a data structure and your code to show the pattern no longer needs a giant switch statement. – Greg Ennis Jan 12 '14 at 14:06
  • 1
    I don't think Toast messages are destroyed the way you are thinking. Once shown they will show even if the activity is destroyed unless you explicitly cancel the Toast. – Rohan Kandwal Jan 12 '14 at 14:07
  • First check would be to see if your code is reaching the switch case you're expecting it to. Second, I would suggest you use something in the display to show the letter (like a TextView) rather than a toast as toast messages aren't context dependant and I would assume you only want to show a letter while the user is viewing the dots and dashes. If you move away from the toast, your issue becomes moot. – Ben Pearson Jan 12 '14 at 14:11
  • @boxed_I removing setGravity didn't help. No changes. – creatice1933 Jan 12 '14 at 14:50
  • @Rohan Kandwal after rethinking it I think you're right but if so what is the source of the problem? – creatice1933 Jan 12 '14 at 14:52
  • @Ben Pearson I've tried the TextBox but the issue is simmilar. The text in the text box is showing only for the last character in sentence and after it ends displaying the dots and lines. I implemented it like this status1.setTextColor(Color.WHITE); status1.setText("Status: Now displaying "+character); – creatice1933 Jan 12 '14 at 14:55
  • From where you are calling showStatus() ? – Rohan Kandwal Jan 12 '14 at 14:59
  • @ Rohan Kandwal From the inside of switch structure you can see it on the end of the source code listed in the first post. – creatice1933 Jan 12 '14 at 15:01
  • Do any of the Toasts show up? I'm concerned since you have this in a for loop, so you might be experiencing this: http://stackoverflow.com/questions/15054732/toast-in-loop-is-not-displaying-properly – anddev84 Jan 12 '14 at 15:28
  • @anddev84 It shows up only for cases in which I call only showStatus() metohod and nothing more (no dispDot etc). For expample if user types character which is not supported by the morse code eg."$" switch goes to default statement and there is only showStatus(character) function and the Toast Message works fine for this case. – creatice1933 Jan 12 '14 at 15:34
  • @creatice1933 try using the `Toast.makeText(MorseActivity.this, "Displaying "+character,Toast.LENGTH_LONG).show();` instead of your toast. – Rohan Kandwal Jan 12 '14 at 15:56
  • @Rohan Kandwal I've tried no changes – creatice1933 Jan 12 '14 at 16:30

2 Answers2

2

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();
Alex Curran
  • 8,818
  • 6
  • 49
  • 55
  • Unfortunately it didn't help. Still the same issue – creatice1933 Jan 12 '14 at 14:49
  • 1
    Indeed, I've noticed my answer is completely wrong. What does your `waitFor()` method do? If you're blocking the main thread with it (using `Thread.sleep()`) then that will almost certainly kill the toasts – Alex Curran Jan 12 '14 at 15:31
  • waitFor() is just a for loop which does some pointless calculation to waist time :) I know that this is very primitive solution but it works for me and I'm stil a beginner:) public void waitFor (int seconds) { for(int wait=0;wait – creatice1933 Jan 12 '14 at 15:38
  • see the updated answer - it doesn't explain everything but should give you a good start. – Alex Curran Jan 12 '14 at 16:25
  • I've read your update and as I've said earlier I know that this waitFor() is primitive and GPU waisting but still I don't understand why you're saying that it's killing toast messages? – creatice1933 Jan 12 '14 at 16:34
  • 1
    It is _CPU_ wasting. That means that on the UI thread (where views - including toasts) are drawn, nothing else can happen whilst waitFor() is running. Therefore, the toasts can't be drawn because your calculation is using the CPU – Alex Curran Jan 12 '14 at 17:34
  • OK that explains a lot. So I'll try to figure out how to solve it with multithreads usage – creatice1933 Jan 12 '14 at 20:12
  • Best of luck! If you have any questions, comment and we can talk on chat at some point – Alex Curran Jan 12 '14 at 22:27
  • I tried few things and first of all I've changed the waitFor function but I guess I've swaped one problem to another :D Now I'm sleeping the UIThread public void waitFor (int seconds) { try { Thread.sleep(seconds*1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } It's working but again the toast messages are kille due to UItread sleep so this wasn't the best idea I guess. – creatice1933 Jan 13 '14 at 11:51
0

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

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