Search code examples
android-studiotry-catchandroid-toast

Toast Message will not display if placed at the beggining of the try catch


I am having an issue with displaying a toast message. I want it to display when a button is initially clicked, so that the user is aware the app is working( in my case, generating a QR code). If I place the toast at the beginning of the try catch it does not display right away when the button is clicked. It displays the toast only after the QR code is generated and displayed. Furthermore, the toast is displayed(after QR code is displayed) on my Android emulator but not at all on my physical android device(I have allowed app permissions) if I place it at the beginning of the try catch. It will only display on my physical device if the toast is at the end of the try catch. This has me puzzled. I attempted to use a ProgressBar that would run on the button click and terminate after the QR code was displayed but found this difficult to implement. I thought the toast would be an easy alternative as I have used them through out my project without any issues until now. If anyone can help me understand how I can get the toast to display right away when the button is clicked instead of displaying after the QR code is generated and displayed, it would much appreciated. Alternatively, if someone could assist me with including a progress bar(circle) to run once the button is clicked and stop once the QR code is displayed that would also be very much appreciated, as I believe this would be the most ideal solution.

Please see code below:

public class Generate extends AppCompatActivity {

public final static int QRcodeWidth = 500 ;
private static final String IMAGE_DIRECTORY = "/QR_Code_Generated";
Bitmap bitmap;
private EditText etQr;
private ImageView ivGenerated;
private Button btn;
private Button clrBtn;
private Button shareBtn;
private ProgressBar progressBar;
private int progressStatus = 0;


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

    ivGenerated = (ImageView) findViewById(R.id.iv_generated);
    etQr = (EditText) findViewById(R.id.generate_input);
    btn = (Button) findViewById(R.id.button);
    clrBtn = (Button) findViewById(R.id.clearButton);
    shareBtn = (Button) findViewById(R.id.shareButton);



    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (etQr.getText().toString().trim().length() == 0) {

            } else {

                try {
                    Toast toast1 = Toast.makeText(Generate.this, "Please Wait, Your QR Code is Generating & will be Saved to your Device... ", Toast.LENGTH_SHORT);
                    toast1.show();

                    bitmap = TextToImageEncode(etQr.getText().toString());
                    ivGenerated.setImageBitmap(bitmap);
                    String path = saveImage(bitmap);  //give read write permission

                    // Toast.makeText(Generate.this, "QR Code saved to -> " + path, Toast.LENGTH_SHORT).show(); (if I include this second toast it does not display the first toast1)


                } catch (WriterException e) {
                    e.printStackTrace();
                }


            }
        }
    });

Solution

  • I dont find any issue with your code for toast pop up, you can try adding a delay of 250ms after toast.show(), maybe it . would be a work around for you.

    And for progress bar, its very simple. Create a progress bar in your .xlm file n place it at the center of your View.Layout you willing to show it & make its visibility as FALSE. You can try this,

    progressbar = = (ProgressBar) findViewById(R.id.progressbar);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progressbar.setVisibility(VISIBLE); // or try View.VISIBLE
            if (etQr.getText().toString().trim().length() == 0) {
    
            } else {
    
                try {
                    Toast toast1 = Toast.makeText(Generate.this, "Please Wait, Your QR Code is Generating & will be Saved to your Device... ", Toast.LENGTH_SHORT);
                    toast1.show();
    
                    bitmap = TextToImageEncode(etQr.getText().toString());
                    ivGenerated.setImageBitmap(bitmap);
                    String path = saveImage(bitmap);  //give read write permission
    
                    // Toast.makeText(Generate.this, "QR Code saved to -> " + path, Toast.LENGTH_SHORT).show(); (if I include this second toast it does not display the first toast1)
    
    
                } catch (WriterException e) {
                    e.printStackTrace();
                }
                finally {
                    progressbar.setVisibility(INVISIBLE); // or try View.INVISIBLE
                }
            }
        }
    });