0

I am working on a program in which I would like to click on a button and have a popup window appear in the middle of the screen displaying a random image. Then upon clicking the popup window it closes the popup.

my question is this possible? if so how?

bribrem
  • 37
  • 1
  • 5

2 Answers2

1

yes you cant. for that you would need to create a Custom Alert dialog which inflates a layout with an imageview in it and call it on clicking the desired object. also you will need to set an onlick listener by adding a close button to the AlertDialog to close it.

Community
  • 1
  • 1
Anurag Ramdasan
  • 4,189
  • 4
  • 31
  • 53
0

Yes, you could use something like this:

Dialog confirmDeleteDialog = new Dialog(this);
confirmDeleteDialog.setContentView(R.layout.custom_message_dialog);
//This allows dialog to be closed with back button
confirmDeleteDialog.setCancelable(true);
//DisplayMetrics will get the screen dimensions and allow you to 
//declare a center point
DiaplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenHeight = displaymetrics.heightPixels; //Divide by 2 to get mid
int screenWidth = displaymetrics.widthPixels; //Divide by 2 to get mid

Button closeButton = (Button) confirmDeleteDialog.findViewById(R.id.close_button);
closeButton.setOnClickListener(this);
confirmDeleteDialog.show();

custom_message_dialog.xml - could look something like this:

<?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">

<Button
android:id="@+id/close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"/>

</RelativeLayout>

The ClickListener

public void onClick(View v) {
if(v == closeButton){
confirmDeleteDialog.dismiss();
}}
DeaMon1
  • 572
  • 5
  • 10