Android popup doesn't fill screen size?

I am trying to create a simple popup. But every time I do this, it ends up very small ... and not the length I want. Here's what the popup looks like: enter image description here

Here is my layout for the popup:

<?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(); } }; 

I can’t understand why it doesn’t work ... How to get the size I want?

+11
android popupwindow android-popupwindow
Aug 17 '16 at 3:02
source share
3 answers

Here you cannot use the layout that is in your xml popup. You must use any view from the main layout. Right now I am using FloatingButton as View for 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(); } } 
+7
Aug 17 '16 at 4:25
source share

How to make a simple Android popup

This is a more complete example. This is an additional answer, which concerns the title of the question and not necessarily the specific details of the problem of OP. It will look as follows.

enter image description here

Make a popup layout

Add a layout file to res/layout that defines what the popup 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> 

Click and show popup

Here is the code for the main activity of our example. Whenever a button is pressed, a pop-up window is inflated and displayed above the activity. Touching anywhere on the screen rejects the popup.

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) { // get a reference to the already created main layout LinearLayout mainLayout = (LinearLayout) findViewById(R.id.activity_main_layout); // 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 popupWindow.showAtLocation(mainLayout, 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; } }); } } 

For reference, the main layout is also located here.

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.popupwindow.MainActivity"> <Button android:text="Show popup window" android:onClick="onButtonShowPopupWindowClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp"/> </LinearLayout> 

Further study

It also helped to learn how to create a popup:

+25
Jul 13 '17 at 10:14
source share

Just change

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

to like it

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

and if you need fields on either side, do it in a popup xml file. Also in xml use android: layout_height = "wrap_content". The advantage of this is that it will look the same (if you place the margin) on any screen of the device.

+5
Aug 17 '16 at 6:00
source share



All Articles