16

I am trying to make a simple pop up window. But every time I make one, it ends up being super small...and not the length I want it to be. This is how the pop up looks: enter image description here

Here is my layout for the pop up:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_element"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#444444"
    android:padding="10px"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Transfering data"
        android:textColor="@color/white"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Status"
        android:textColor="@color/white"/>

    <TextView android:id="@+id/server_status_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Awaiting answer..."
        android:paddingLeft="10sp"
        android:textColor="@color/white"/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:gravity="center_horizontal|bottom">

        <Button android:id="@+id/end_data_send_button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:drawablePadding="3sp"
            android:layout_centerHorizontal="true"
            android:text="Cancel" />
    </LinearLayout>
</LinearLayout>

Here is my java code:

private PopupWindow pw;
        private void bindActivity() {

            fabButton = (ImageButton) findViewById(R.id.activity_profileView_FAB);
            fabButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    initiatePopupWindow();
                }
            });


        }
     private void initiatePopupWindow() {
            try {
                //We need to get the instance of the LayoutInflater, use the context of this activity
                LayoutInflater inflater = (LayoutInflater) ProfileView.this
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                //Inflate the view from a predefined XML layout
                View layout = inflater.inflate(R.layout.popup,
                        (ViewGroup) findViewById(R.id.popup_element));
                // create a 300px width and 470px height PopupWindow
                pw = new PopupWindow(layout, 300, 470, true);
                // display the popup in the center
                pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

                TextView mResultText = (TextView) layout.findViewById(R.id.server_status_text);
                Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button);
                cancelButton.setOnClickListener(cancel_button_click_listener);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {
            public void onClick(View v) {
                pw.dismiss();
            }
        };

Cant figure out why its not working... How would I get the size I want?

Greg Viers
  • 3,423
  • 3
  • 14
  • 32
TheQ
  • 1,770
  • 6
  • 33
  • 60
  • This is sample popup. http://stackoverflow.com/questions/13341560/how-to-create-a-custom-dialog-box-in-android – Tai Nguyen Aug 17 '16 at 03:23
  • @TaiNguyen But I dont want to use to Dialog Fragment, I want to use the Popupwindow class. I just want to make sure I am able to create it using the Popupwindow class. – TheQ Aug 17 '16 at 03:32
  • This is good sample using PopupWindow http://mrbool.com/how-to-implement-popup-window-in-android/28285. You should set setContentView before showAtLocation. popupMessage.setContentView(layoutOfPopup); – Tai Nguyen Aug 17 '16 at 03:45
  • 1
    `// create a 300px width and 470px height PopupWindow` I don't see the problem here, you're getting exactly what you asked for ;-) – Andrew Sun Aug 17 '16 at 03:56
  • See [How to make a simple Android popup window](https://stackoverflow.com/a/50188704/3681880). (This answer used to be here, but I moved it since it fit that other question better.) – Suragch May 07 '18 at 07:18

2 Answers2

13

Here you can't use layout which is in your popup window xml. You have to use any View from main layout. Right now I am using FloatingButton as a View to showAtLocation.

fabButton = (ImageButton) findViewById(R.id.activity_profileView_FAB);
            fabButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    initiatePopupWindow(v);
                }
            });

 private void initiatePopupWindow(View v) {
            try {
                //We need to get the instance of the LayoutInflater, use the context of this activity
                LayoutInflater inflater = (LayoutInflater) ProfileView.this
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                //Inflate the view from a predefined XML layout
                View layout = inflater.inflate(R.layout.popup,
                        (ViewGroup) findViewById(R.id.popup_element));
                // create a 300px width and 470px height PopupWindow
                pw = new PopupWindow(layout, 300, 470, true);
                // display the popup in the center
                pw.showAtLocation(v, Gravity.CENTER, 0, 0);

                TextView mResultText = (TextView) layout.findViewById(R.id.server_status_text);
                Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button);
                cancelButton.setOnClickListener(cancel_button_click_listener);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
Naveen Kumar M
  • 6,868
  • 6
  • 55
  • 66
5

Just change

 pw = new PopupWindow(layout, 300, 470, true);

to like this

pw = new PopupWindow(layout, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT, true);

and if you need margins on any side do it in popup xml file. Also in xml use android:layout_height="wrap_content". Advantage of this is,- it will look same (if you put margin) in any device screen.

King of Masses
  • 16,881
  • 4
  • 57
  • 75
Exigente05
  • 2,068
  • 3
  • 16
  • 35