0

I want to create a dialog like this:

required dialog click here

I need to create the dialog when user clicks on the profile image like given in link. Current I'm doing this

    final CharSequence[] items = { "Gallery", "Camera", "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {

            if (items[item].equals("Gallery")) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp1.jpg");
                mImageCaptureUri = Uri.fromFile(f);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
                startActivityForResult(intent, CAMERA_CODE);

            } else if (items[item].equals("Camera")) {

                Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, GALLERY_CODE);

            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();

I dont know how to do as the design given please help me on this.

Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23
Lol
  • 93
  • 10

1 Answers1

0

There are two ways you can achieve this.

  1. Using Custom Alert dialog.
  2. Using Intent by query intent activities.

By default, Android doesn't show the option to open camera and gallery in a single shot. But doing some tricks will help to achieve this.

Refer the below code which is used to display Camera and Gallery browse options in a single default dialog.

// Picks Camera first.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
        captureIntent, 0);
for (ResolveInfo res : listCam) {
  final String packageName = res.activityInfo.packageName;
  final Intent intent = new Intent(captureIntent);
  intent.setComponent(new ComponentName(res.activityInfo.packageName,
          res.activityInfo.name));
  intent.setPackage(packageName);
  intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
  cameraIntents.add(intent);
}

final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_PICK);

// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent,
        "Select Image from");

// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
        cameraIntents.toArray(new Parcelable[]{}));

startActivityForResult(chooserIntent, TAKE_PHOTO_CODE);

Already answered here for same type of issue. !! --> queryIntentActivityOptions not working

Hope this helps you,

Community
  • 1
  • 1
Swaminathan V
  • 4,663
  • 2
  • 22
  • 32