How to set the layout and size of PopupWindow

I am writing my first android program and am having difficulty placing PopupWindow and size, as I assume. Since at the moment the popup will be activated after clicking the button in the menu bar. After clicking, I was hoping that the pop-up display would be centralized, however, currently clicking on the result displays the image below (cannot send the image due to reputation 10):

https://www.box.com/s/7d4qk8tqlvhiog7576mc

Java Popup Method and Listener:

public void showPopup(View add){ PopupWindow popup = new PopupWindow(this); setContentView(R.layout.add); popup.setBackgroundDrawable(null); popup.showAtLocation(add, Gravity.CENTER,0,0); popup.setFocusable(true); popup.setOutsideTouchable(true); View hideAdd = findViewById(R.id.add_task); hideAdd.setVisibility(View.INVISIBLE); } public boolean onOptionsItemSelected(MenuItem menuItem){ switch(menuItem.getItemId()){ case R.id.add_task: View addTaskView = findViewById(R.id.add_task); showPopup(addTaskView); return true; default: return false; } } 

}

Add Xml Layout:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/title_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/task_title" android:textSize="25sp" android:textColor="@android:color/holo_blue_bright"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/holo_blue_bright"/> <EditText android:id="@+id/task_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/task_prompt"> </EditText> </LinearLayout> 

Any help would be greatly appreciated.

+4
source share
3 answers

There is something called a custom dialog. . For instance.:

  • You can create a layout and set it in a dialog box.

      Dialog dialog=new Dialog(); dialog.setContentView(the layout u designed here); 
  • Convert activity to dialogue .. check out my answer to this post ..

.. Thus, in dialogs there are endless possibilities.

+1
source

Try changing all "android: layout_width =" match_parent "" in your xml to "android: layout_width =" wrap_content ""

+1
source

I think this is because you set the appearance of the view to be invisible. try commenting on this and run it again.

0
source

Source: https://habr.com/ru/post/1485158/


All Articles