Search code examples
androidandroid-intentandroid-alertdialogandroid-image

how to add the images to AlertDialog in android?


I crated the one AlertDialog that was two option one is open camera and other one is gallery

as image as shown
enter image description here

but i need to add the camera and gallery images to it like this

enter image description here

how to add the the image is this code

    final String [] items   = new String [] {"Take from camera", "Select from gallery"};    
      ArrayAdapter<String> adapter  = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
      AlertDialog.Builder builder   = new AlertDialog.Builder(this);

      builder.setTitle("Select Image");
      builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
      public void onClick( DialogInterface dialog, int item ) { //pick from camera
      if (item == 0) {
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

      mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
      "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

      intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

      try {
      intent.putExtra("return-data", true);

      startActivityForResult(intent, PICK_FROM_CAMERA);
      } catch (ActivityNotFoundException e) {
      e.printStackTrace();
      }
      } else { //pick from file
      Intent intent = new Intent();

      intent.setType("image/*");
      intent.setAction(Intent.ACTION_GET_CONTENT);

      startActivityForResult(Intent.createChooser(intent,    "Complete               action               using"), PICK_FROM_FILE);
      }
      }
      });

      final AlertDialog dialog = builder.create();



      dialog.show();

}

Solution

  • Create layout like you want then add that layout in your dialogue

    Layout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp" />
    
        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FFF" 
            android:layout_toRightOf="@+id/image" />
    
       <Button
            android:id="@+id/dialogButtonOK"
            android:layout_width="100px"
            android:layout_height="wrap_content"
            android:text=" Ok "
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:layout_below="@+id/image" />
    
    </RelativeLayout>
    

    then on activity

    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.custom);
    dialog.setTitle("Bla Bla");
    
    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText("Your Text");
    ImageView image = (ImageView) dialog.findViewById(R.id.image);
    image.setImageResource(R.drawable.ic_launcher);
    
    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
    dialogButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    
    dialog.show();