1

I am making an app with the following layout:Main App Layout In the app I use a Take Photo button to call the camera intent from my phone to take a picture. The picture gets displayed into the image view(outlined in red in the layout).

I also use the Save Button to save the picture to the gallery via intent. I also use a Signature button to capture the user's signature.Signature has its own layout. The layout consists of the following: Signature LayoutHowever, Lets say I open the app and hit the Save Button when there is no image currently displayed. My Save button currently still works and pulls up the gallery despite there being no picture. The same thing happens with the save button in my signature layout. If there is no signature present currently, the save button still saves.

How would I code it to where I can check if there is currently a picture already shown or a signature already shown, and if not, save buttons in my Signature and Main App Layout are disabled. I know for disabling a button, the syntax is: myButton.setEnabled(false);

I have the following code for the save button in the main app layout:

 //this save button is for the gallery app after you take a photo
 saveButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //launch the gallery app intent
            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setType("image/*");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            Toast.makeText(DriverActivity.this, "Image Saved to Gallery", Toast.LENGTH_SHORT).show();
            /*if there is currently no image, disable save button and display a toast message
            Toast.makeText(DriverActivity.this, "There's no image currently shown.", Toast.LENGTH_SHORT).show();*/

        }
    });
    // restoring storage image path from saved instance state
    // otherwise the path will be null on device rotation
    restoreFromBundle(savedInstanceState);

Then I have this code for the signature:

//this is for signature
    signatureButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            /*// Use an intent to launch an email app.
            // Send the order summary in the email body.
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("mailto:")); // only email apps should handle this
            intent.putExtra(Intent.EXTRA_SUBJECT,
                    getString(R.string.order_summary_email_subject));
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }*/
            Intent intent = new Intent(DriverActivity.this, SignatureActivity.class);
            startActivity(intent);
            Toast.makeText(DriverActivity.this, "Now Loading Signature Sign", Toast.LENGTH_LONG).show();
        }
    });

This piece of code is from my SignatureActivity.java file(Note, the two code samples above are from a different Activity.java file(i.e.: DriverActivity.java):

//capture signature
    btnSave.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            view.setDrawingCacheEnabled(true);
            mSignature.save(view,StoredPath);
            Intent intent2 = new Intent(getBaseContext(), DriverActivity.class);
            startActivity(intent2);
            finish();
            Toast.makeText(getApplicationContext(), "Successfully Saved", Toast.LENGTH_SHORT).show();

        }
    });
Boken
  • 4,825
  • 10
  • 32
  • 42
Amma
  • 17
  • 1
  • 9

2 Answers2

2

in the layout that holds ImageView, you can use the "tag" attribute for the ImageView of the photo; if the tag is "false" then there is no image associated to the ImageView, and you can then disable the save button; if it's "true" then there is one, and you can enable the save button.

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="false" />

Behavior

ImageView image = findViewById(R.id.image);

if ((Boolean) image.getTag()) { // No image
    // Disable save button
    mBtnSave.setEnabled(false);
} else {
    mBtnSave.setEnabled(true);
}

Whenever you take the photo, you should change the tag of the ImageView to "true"

image.setTag("true");

Edit:

As you use a different activity for signature, you've also to use startActivityForResult() which will start SignatureActivity and waits for results when it's done; you can follow the above mentioned procedure of setting signature image tag to be either "true" if you received a successful signature, or "false" if not. Here you can find how to use startActivityForResult()

Zain
  • 37,492
  • 7
  • 60
  • 84
1

You can do this by following steps given below as I done

  • Make an Boolean variable and initialized its value as false
  • When you got an image from Intent sets its value true
  • Now you can make an check on Button click as if value is false then do not save the picture or data and if value is true then save the image or your data.

It worked for me.

Adeel Iftikhar
  • 772
  • 7
  • 30