2/24/2014 9:02:43 PM

Create a PopupWindow with buttons and a text message. The background will be faded black background.

1. Create a layout file defining the popup. this is not the layout of the activity that will call the popup //layout_popup <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_popup" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#7d000000" android:gravity="center_vertical|center_horizontal" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="200dp" android:background="#000000" android:gravity="center_vertical|center_horizontal" android:orientation="vertical" > <TextView android:id="@+id/layout_popup_txtMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal" > <Button android:id="@+id/layout_popup_butOne" style="@style/main_Buttons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginRight="5dp" android:layout_weight="1" android:text="Button One" /> <Button android:id="@+id/layout_popup_butTwo" style="@style/main_Buttons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginLeft="5dp" android:layout_weight="1" android:text="Button Two" /> </LinearLayout> </LinearLayout> </LinearLayout> 2. Inflate the view private PopupWindow POPUP_WINDOW_SCORE = null; private void ShowPopup(string message) { DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics(); int width = displayMetrics.widthPixels; int height = displayMetrics.heightPixels; // Inflate the popup_layout.xml LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = layoutInflater.inflate(R.layout.layout_popup, null); // Creating the PopupWindow POPUP_WINDOW_SCORE = new PopupWindow(this); POPUP_WINDOW_SCORE.setContentView(layout); POPUP_WINDOW_SCORE.setWidth(width); POPUP_WINDOW_SCORE.setHeight(height); POPUP_WINDOW_SCORE.setFocusable(true); // prevent clickable background POPUP_WINDOW_SCORE.setBackgroundDrawable(null); POPUP_WINDOW_SCORE.showAtLocation(layout, Gravity.CENTER, 1, 1); TextView txtMessage = (TextView) layout.findViewById(R.id.layout_popup_txtMessage); txtMessage.setText(message); // Getting a reference to button one and do something Button butOne = (Button) layout.findViewById(R.id.layout_popup_butOne); butOne.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //Do Something //Close Window POPUP_WINDOW_SCORE.dismiss(); } }); // Getting a reference to button two and do something Button butTwo = (Button) layout.findViewById(R.id.layout_popup_butTwo); butTwo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //Do Something //Close Window POPUP_WINDOW_SCORE.dismiss(); } }); }