Reputation: 651
I'm using pop-window to show some text-view while clicking the edit-text . but pop-window is not showing at particular location ,it always showing at left top corner , here is my code ,what is wrong in that
private void showPopup(Context context,final LinearLayout Parent) {
LayoutInflater layoutInflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = layoutInflater.inflate(R.layout.popup,MainActivity.parent,true);
// Creating the PopupWindow
final PopupWindow popupWindow = new PopupWindow(
layout,700,700);
popupWindow.setContentView(layout);
popupWindow.setHeight(500);
new Runnable(){
@Override
public void run() {
popupWindow.showAtLocation(layout, Gravity.CENTER,300,150);
}
};
}
Upvotes: 2
Views: 3058
Reputation: 2681
Try Following Code:
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException e)
{
//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;
}
And then use
popup.showAtLocation(parent, Gravity.TOP|Gravity.LEFT, location.left, location.bottom);
Hope it helps...
Upvotes: 2