I am trying to put a QR-code in an alert box. I am creating a QR code using zxing library so, the QR will first be generated as a bitmap. Here my problem is I can only set it as an icon but can't find a proper way to set as an image.
Here is what I have tried:
btn.setOnClickListener(new View.OnClickListener() {
Drawable d;
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
String text = et.getText().toString();
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.QR_CODE, 200, 200);
qr = bitMatrix.hashCode();
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
final Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
bit = bitmap;
d = new BitmapDrawable(getResources(), bit);
iv.setImageBitmap(bitmap);
builder.setTitle("hi");
builder.setIcon(d);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Yes button Clicked", Toast.LENGTH_LONG).show();
Log.i("Code2care ", "Yes button Clicked!");
dialog.dismiss();
}
});
builder.setView(iv).create().show();
//builder.show();
} catch (WriterException e) {
e.printStackTrace();
}
}
});
So I am getting a
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first"
at builder.setView(iv).create().show();
When I use a solution found in web i.e. to add a image view to the dialog.
Any help will be useful, thanks in advance.
The error log is pretty clear, the iv
view was already added to another ViewGroup
. To solve it, you should create a new ImageView
instance instead of using iv
.