Reputation: 2717
I'm trouble trying to add an image to an AlertDialog. This image is not always the same, it's different everytime.
What i'm trying to do, is adding an image to the AlertDialog (if it's possible as an imageView, and not as icon of the dialog) knowing his name. The trouble is that i have thousand images.
1) So, when calling builder.setIcon(int iconid) i dunno how to pass it the iconid instead of the name of the picture. Considering i'm using different image every time.
2) Any advice on how to put the image as a imageView (considering that there's a layout associated with this Dialog)?
UPDATE
I have a String s = scanQR.substring(start, 21);
that is the result of the scan with a QR-CODE reader.
This string contain the name of the image i want to put in an AlertDialog. Now, here is the code for the Dialog:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_single_work, null))
//HERE IS THE ICON (BETTER WILL BE AN IMAGEVIEW, BUT DUNNO HOW TO DO IT)
.setIcon()
.setPositiveButton("POS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("NEG", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
}
the method .setIcon requires or an int (iconid) or a drawable object. But all i have is just the name (String s) of the image. How can i solve it?
Upvotes: 0
Views: 4249
Reputation: 17037
builder.setIcon();
is actually setting an icon for AlertDialog
which is placed to left of dialog's title. As I understand you want to add an image to your dialog, not icon. To achieve this you should add a custom view to your dialog :
View mView = View.inflate(this, R.layout. dialog_single_work, null);
ImageView mImage = (ImageView) mView.findViewById(R.id.image);
// use this ImageView to set your image
builder.setView(mView);
and add an ImageView
in it's layout where you can set your image. Is that the thing which you are trying to achieve?
Edit: Do something like :
public void showDialog(int result){
switch(result){
case 0:
mImageView.setImageResource(R.drawable.my_icon);
break;
case 1:
mImageView.setImageResource(R.drawable.my_second_icon);
break;
// and so on... or do it with string or whatever is your response.
}
}
Edit2: You can get image from resources using String like this:
String mDrawableName = "myappicon";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
Upvotes: 1