Search code examples
androiddialogcustomdialog

Close Custom Dialog from another function


I am trying to modify the layout of a dialog and then perform some function and close the alert box as my function is over.

Layout File

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:orientation="horizontal"
            android:layout_below="@+id/header"
            android:paddingTop="15dp"
            android:paddingBottom="15dp">
        <ImageView
            android:id="@+id/cam"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:paddingLeft="10dp"
            android:src="@drawable/ic_cam"
            android:layout_alignParentLeft="true"
            android:layout_weight="1"
            android:onClick="camera_listener"
            />

        <ImageView
            android:id="@+id/gal"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:paddingLeft="10dp"
            android:src="@drawable/ic_gal"
            android:layout_alignParentLeft="true"
            android:layout_weight="1"
            android:onClick="gallery_listener"
            />
        </LinearLayout>

Java File

    AlertDialog.Builder myAlertDialog;    // Variable declared as a class member

    private void startDialog() {

            LayoutInflater inflater = this.getLayoutInflater();
            final View view = inflater.inflate(R.layout.activity_fab, null);
            myAlertDialog = new AlertDialog.Builder(this);
            myAlertDialog.show();
    }

public void gallery_listener(View view) {

        pictureActionIntent = new Intent(Intent.ACTION_PICK, null);
        pictureActionIntent.setType("image/*");
        pictureActionIntent.putExtra("return-data", true);
        startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
        myAlertDialog.setOnDismissListener();
    }

    public void camera_listener(View view) {

        pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
        pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        startActivityForResult(pictureActionIntent, CAMERA_REQUEST);
    }

I'm displaying two images in dialog and defining function on their click... i want to close the DIALOG as soon as corresponding function is performed.

I tried using dismiss but it is not working..!


Solution

  • Alternate i found... is first create a alert dialog using builder then assign that to a dialog and use dismiss function with that

    Update-

    private AlertDialog dialog; // Variable with class scope
    private void startDialog() {
    
        LayoutInflater inflater = this.getLayoutInflater();
        final View view = inflater.inflate(R.layout.activity_fab, null);
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setView(view);
    
        dialog = builder.create();
        dialog.show();
        }
    
        public void gallery_listener(View view) {
    
            pictureActionIntent = new Intent(Intent.ACTION_PICK, null);
            pictureActionIntent.setType("image/*");
            pictureActionIntent.putExtra("return-data", true);
            startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
            dialog.dismiss();
        }
    

    This has worked for me... if there is any better method plz share!