Android popup positioning is correct

I am stuck in positioning popup windows. I show my popup when a button is clicked. I want it to fit in the available space. Also, if my button is in the center, it should be below the button. Below is my code for this thing. Please let me know where I am going wrong. Thanks.

mBtnPopUp.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ListView mListView = (ListView) mPopUpView.findViewById(R.id.pop_up_list_view); mListView.setAdapter(mPopupListAdapter); Drawable drawable = getResources().getDrawable(android.R.drawable.alert_light_frame); mPopupWindow.setBackgroundDrawable(drawable); // mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); showLikeQuickAction(0, 0); mPopupWindow.showAsDropDown(mBtnPopUp); } }); mPopupWindow.setOutsideTouchable(true); public void showLikeQuickAction(int xOffset, int yOffset) { int[] location = new int[2]; mBtnPopUp.getLocationOnScreen(location); Rect anchorRect = new Rect(location[0], location[1], location[0] + mBtnPopUp.getWidth(), location[1] + mBtnPopUp.getHeight()); mBtnPopUp.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); int rootWidth = mBtnPopUp.getMeasuredWidth(); int rootHeight = mBtnPopUp.getMeasuredHeight(); @SuppressWarnings("deprecation") int screenWidth = mWindowManager.getDefaultDisplay().getWidth(); int xPos = screenWidth - rootWidth + xOffset; int yPos = anchorRect.top - rootHeight + yOffset; if(rootWidth > anchorRect.right - anchorRect.left) { // right xPos = anchorRect.right - rootWidth; } else { // left xPos = anchorRect.left + 15; } if(xPos + rootWidth > screenWidth) xPos = screenWidth - rootWidth - 20; // display on bottom if(rootHeight > anchorRect.top) { yPos = anchorRect.bottom + yOffset; // mPopupWindow.setAnimationStyle(R.style.Animations_GrowFromTop); } mPopupWindow.showAtLocation(mBtnPopUp, Gravity.NO_GRAVITY, xPos, yPos); } 

Thanks..

+4
source share
4 answers
 public void downloadBtnSelected(View anchor) { final ListPopupWindow lpw = new ListPopupWindow(this); String[] data = { ".png", ".pdf", ".jpg", ".jpeg" }; PopupAdapter pa = new PopupAdapter(data, this); lpw.setAdapter(pa); //setting up an anchor view lpw.setAnchorView(anchor); //Setting measure specifications. I'v used this mesure specs to display my //ListView as wide as my anchor view is lpw.setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT); lpw.setWidth(anchor.getRight() - anchor.getLeft()); // Background is needed. You can use your own drawable or make a 9patch. // I'v used a custom btn drawable. looks nice. lpw.setBackgroundDrawable(this.getResources().getDrawable(android.R.drawable.btn_default)); // Offset between anchor view and popupWindow lpw.setVerticalOffset(3); lpw.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Our action..... lpw.dismiss(); } }); lpw.show(); } 

and a button with onClickListener to call this method:

 Button btn = new Button(this); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { downloadBtnSelected(v); } }); 

this code allows you to show a popup below btn the width of the popup will be the same as button 1. if you want to fill the width of the screen - set the width = LinearLayout.MATCH_PARENT popup menu will be displayed under the snap view, if space and outside, if not places below

+1
source

I know his quiet being late, but I hope this helps someone.

 LayoutInflater layoutInflater = getLayoutInflater(); int popupWidth = 500;//ViewGroup.LayoutParams.WRAP_CONTENT; int popupHeight = ViewGroup.LayoutParams.WRAP_CONTENT; popupView = layoutInflater.inflate(R.layout.main, null); PopupWindow attachmentPopup = new PopupWindow(this); attachmentPopup.setFocusable(true); attachmentPopup.setWidth(popupWidth); attachmentPopup.setHeight(popupHeight); attachmentPopup.setContentView(popupView); attachmentPopup.setBackgroundDrawable(new BitmapDrawable()); attachmentPopup.showAsDropDown(v, -5, 0); 

It will place your popup according to the position of your button.

+1
source

For a custom popup, create one XML file named

cust_toast_layout.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="45dp" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" android:textSize="25sp" android:textStyle="bold" android:gravity="center" android:paddingTop="200dp"/> </LinearLayout> 

Then in the java file where you want this popup to be added below the code

 LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.cust_toast_layout, (ViewGroup)findViewById(R.id.toast_layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); //text.setText("Excellent !!!"); text.setBackgroundResource(R.drawable.right_ans); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(layout); toast.show(); 

In buttonclick, you can call one method and include this code in this method. You can make changes according to your requirements by editing in an XML file.

0
source

this code will show a perfect popup in the center of your particular view

 // click event of specific view @Override public void onClick(View v) { super.onClick(v); if (v == lin_filterActionbar) { PopupWindow popupwindow_obj; // create object popupwindow_obj = dialog_AllergiesSelect(RR_HomeActivity.this,lin_filterActionbar); popupwindow_obj.showAsDropDown(lin_filterActionbar); } } private PopupWindow dialog_AllergiesSelect(Context context,LinearLayout lin) { final PopupWindow dialog_AllergiesSelect = new PopupWindow(this); View v = View.inflate(context, R.layout.dialog_filter_actionbar, null); dialog_AllergiesSelect.setContentView(v); dialog_AllergiesSelect.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); dialog_AllergiesSelect.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); dialog_AllergiesSelect.setFocusable(true); // TextView lis_testSelection = (TextView) v.findViewById(R.id.dialogfilter_txt_filter); // LinearLayout.LayoutParams lin_laLayoutParams = new LinearLayout.LayoutParams((int) (sunApplication.getDeviceWidth() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_spinner_width)), (int) (sunApplication.getDeviceHeight() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_height))); // lis_testSelection.setLayoutParams(lin_laLayoutParams); // Some offset to align the popup a bit to the right, and a bit down, relative to button position. int OFFSET_X = 0; int OFFSET_Y = 0; // Clear the default translucent background dialog_AllergiesSelect.setBackgroundDrawable(new BitmapDrawable()); // Displaying the popup at the specified location, + offsets. //dialog_AllergiesSelect.showAtLocation(v, Gravity.C, px + OFFSET_X, py + OFFSET_Y); Rect location = locateView(lin); dialog_AllergiesSelect.showAtLocation(v, Gravity.TOP|Gravity.CENTER, 0, location.bottom); // Getting a reference to Close button, and close the popup when clicked. return dialog_AllergiesSelect; } public static Rect locateView(View v) { int[] loc_int = new int[2]; if (v == null) return null; try { v.getLocationOnScreen(loc_int); } catch (NullPointerException npe) { //Happens when the view doesn't exist on screen anymore. return null; } Rect location = new Rect(); location.left = loc_int[0]; location.top = loc_int[1]; location.right = location.left + v.getWidth(); location.bottom = location.top + v.getHeight(); return location; } 
0
source

All Articles