Search code examples
javaandroidandroid-buttononactivityresult

How to make a floating button disappear android?


I have a simple floating button that open the camera and then display the image in the Main Activity.

I want to remove that floating button right after the user retrieve the image result so It should be onActivityResult right after scannedImageView.setImageBitmap(bitmap)

I tried using btnFloat.setVisibility(View.GONE); but since it's a floating button I do not know how to call it on my method.

MainActivity

ImageView scannedImageView;

public void init() {
FloatingActionButton fab = findViewById(R.id.fab_capture);
}

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
     if (resultCode == Activity.RESULT_OK && data != null) {
         if (requestCode == OPEN_THING) {
             Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
             Bitmap bitmap = null;
             try {
                 bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                 getContentResolver().delete(uri, null, null);
                 scannedImageView.setImageBitmap(bitmap);
               //btnFloat.setVisibility(View.GONE);
                 FileOutputStream outputStream = null;
                 File sdCard = new File(getExternalFilesDir(null).getAbsolutePath());
                 File directory = new File (sdCard.getAbsolutePath() +"/Scan Documents");
                 directory.mkdir();

                 String filename = String.format("d.jpg", System.currentTimeMillis());
                 File outFile = new File(directory, filename);

                 Toast.makeText(this, "Image Saved Successfully", Toast.LENGTH_SHORT).show();

                 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                 intent.setData(Uri.fromFile(outFile));
                 sendBroadcast(intent);

                 try{
                     outputStream = new FileOutputStream(outFile);
                     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                     outputStream.flush();
                     outputStream.close();

                 }catch (FileNotFoundException e)
                 {
                     e.printStackTrace();
                 }
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
 }

Solution

  • make FloatingActionButton a Global member variable then you can access it in onActivityResult

    public class MyActivity extends Activity {
     ...
    FloatingActionButton fab;
    
      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fab = findViewById(R.id.fab_capture);
    }
    
    
    
     protected void onActivityResult(int requestCode, int resultCode,
             Intent data) {
      ...
        fab.hide();
       ...
     }
    }