How to put an image in an AlertDialog? Android
Create one sample.xml
and add ImageView
in that XML.
sample.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/dialog_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
Java Code :
AlertDialog.Builder alertadd = new AlertDialog.Builder(MessageDemo.this);
LayoutInflater factory = LayoutInflater.from(MessageDemo.this);
final View view = factory.inflate(R.layout.sample, null);
alertadd.setView(view);
alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}
});
alertadd.show();
how to add the images to AlertDialog in android?
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();
Show AlertDialog with ImageView without any padding
I ended up figuring out a way to solve this issue. Because manually changing the height of the ImageView removes the extra padding, I ended up finding the dimensions of the original image, and apply them to the ImageView once the ImageView dimensions can be calculated. Here is the final result:
This is the final working code:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog dialog = builder.create();
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
dialog.setView(dialogLayout);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface d) {
ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.whygoprodialogimage);
float imageWidthInPX = (float)image.getWidth();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Math.round(imageWidthInPX),
Math.round(imageWidthInPX * (float)icon.getHeight() / (float)icon.getWidth()));
image.setLayoutParams(layoutParams);
}
});
And the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/goProDialogImage"
android:layout_width="wrap_content"
android:layout_height="350dp"
android:src="@drawable/whygoprodialogimage"/>
</LinearLayout>
How can I set image URI as icon in AlertDialog box?
If you need to display an image from a url in an ImageView
, I recommend you to use an image loading and caching libraries. Some well-known ones are Glide and Picasso.
Android - Alert Dialog with changeable image
You can get a reference of ImageView
by calling findViewById
on your view
and then call setImageResource
on it :
LayoutInflater factory = LayoutInflater.from(wheel.this);
final View view = factory.inflate(R.layout.alert, null);
// change the ImageView image source
final ImageView dialogImageView = (ImageView) view.findViewById(R.id.dialog_imageview);
dialogImageView.setImageResource(R.drawable.your_image);
alertadd.setView(view);
alertadd.setTitle("Alert Dialog Title");
alertadd.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}
});
alertadd.show();
Related Topics
What Is the Equivalent of Actionbar in Earlier Sdk Versions
Two Textviews Side by Side, Only One to Ellipsize
Map View Draw Directions Using Google Directions API - Decoding Polylines
Proguard Missing Type Parameter
How to Retrieve the Data from Asynctasks Doinbackground()
How to Get Current Buildtype in Android Gradle Configuration
How to Retrieve Data from Cursor Class
How Do Task Managers Kill Apps
Eclipse Installation Error: Install_Failed_Uid_Changed
Unsigned APK Can Not Be Installed
Adt Blank Activity Created with Fragment Activity
Could Not Access the Package Manager. Is the System Running While Installing Android Application
How to Pop Fragment Off Backstack
Generating Google Map Release API Key
Read Command Output Inside Su Process