To create a simple working PopupWindow, we need to do the following:
popup_example.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="10dip" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="Test Pop-Up" /> </LinearLayout>
Java code
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false),100,100, true); pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
My requirement is that I need a
<TEXTVIEW android:layout_height="wrap_content" android:layout_width="fill_parent" />
and a
<BUTTON android:id="@+id/end_data_send_button" android:text="Cancel"/>
in my popup_example.xml
. How can I handle these two components in my Java code?
Use setWidth(int) and setHeight(int) . Set the layout type for this window. Display the content view in a popup window anchored to the bottom-left corner of the anchor view. Displays the content view in a popup window anchored to the corner of another view.
It is created inside the res/menu directory.
This is a fuller example. It is a supplemental answer that deals with creating a popup window in general and not necessarily the specific details of the OP's problem. (The OP asks for a cancel button, but this is not necessary because the user can click anywhere on the screen to cancel it.) It will look like the following image.
Add a layout file to res/layout
that defines what the popup window will look like.
popup_window.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#62def8"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="30dp" android:textSize="22sp" android:text="This is a popup window."/> </RelativeLayout>
Here is the code for the main activity of our example. Whenever the button is clicked, the popup window is inflated and shown over the activity. Touching anywhere on the screen dismisses the popup window.
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onButtonShowPopupWindowClick(View view) { // inflate the layout of the popup window LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View popupView = inflater.inflate(R.layout.popup_window, null); // create the popup window int width = LinearLayout.LayoutParams.WRAP_CONTENT; int height = LinearLayout.LayoutParams.WRAP_CONTENT; boolean focusable = true; // lets taps outside the popup also dismiss it final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable); // show the popup window // which view you pass in doesn't matter, it is only used for the window tolken popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0); // dismiss the popup window when touched popupView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { popupWindow.dismiss(); return true; } }); } }
That's it. You're finished.
Check out how gravity values effect PopupWindow.
You can also add a shadow.
These were also helpful in learning how to make a popup window:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With