[Android] add image in alertdialog

Source: Internet
Author: User

2011-08-28

My first android app ---- happy first version is coming soon

This week, the computation has done something and added icons and pic for the result page.

To sum up

Problem: add an image for AlertDialog. The number of images is displayed based on the number of result objects.

Check AlertDialog sdk and find that a better way is to create FrameLayout, add imageview to it, and set it to the dialog attribute.

However, how to create FrameLayout does take some twists and turns out, And framelayout cannot be added to layout of the same activity.

The specified child all ready has a parent error may occur.

You need to create a new propalterdialog. xml to define it separately and place layout, and then call LayoutInflater to change the layout to the current layout display.

The ImageView label can be defined in FrameLayout xml.

Use framelayout context to dynamically generate an image

However, the image in framelayout overlaps. When LinearLayout is used, android: orientation = "horizontal" is set in the xml file, and the image is placed in one row.

The following are all the codes used.

LayoutInflater factory = LayoutInflater.from(DemoActivity.this);
LinearLayout fl = (LinearLayout) factory.inflate(R.layout.propalterdialog,null);
int size = selectionList.size();
for(Selection sel:selectionList){
ImageView imageView = new ImageView(fl.getContext());
imageView.setLayoutParams(new FrameLayout.LayoutParams(190/size, 190/size, 100));
int image_id = DemoActivity.this.getResources().getIdentifier(sel.getPicture(), "drawable",
"com.netqin.android.lab");
imageView.setImageResource(image_id);
fl.addView(imageView);
}

new AlertDialog.Builder(DemoActivity.this).setTitle("result")
.setIcon(R.drawable.result_icon)
.setView(fl)
.setMessage(result).setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();

}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.