Reputation: 307
I've got a popupWindow that I'm using to display google maps but once it's open I can't seem to close it, I'm trying to set the back button to close it but can't call the onBackButtonClick override as I'm inside a fragment. Doesn't seem to do anything though
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_go, container, false);
mRouteBtn = (Button) rootView.findViewById(R.id.routeBtn);
mRouteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View popupView = getActivity().getLayoutInflater().inflate(R.layout.fragment_map, null);
mPopupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// If the PopupWindow should be focusable
mPopupWindow.setFocusable(true);
mPopupWindow.showAtLocation(getActivity().findViewById(R.id.routeBtn), Gravity.CENTER, 0, 0);
mPopupWindow.update(0, 0, 800, 800); //don't hardcode
Back button code part:
mPopupWindow.getContentView().setOnKeyListener( new View.OnKeyListener(){
@Override
public boolean onKey( View v, int keyCode, KeyEvent event ){
if( keyCode == KeyEvent.KEYCODE_BACK ){
mPopupWindow.dismiss();
return true;
}
return false;
}
} );
}
});
Alright sorted, after following Deepak Singh's advice I got it working although when I reopened it I had issues with crashing due to trying to recreate a view that still exists. here's my working code for future reference:
FIXED VERSION:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_go, container, false);
mRouteBtn = (Button) rootView.findViewById(R.id.routeBtn);
mRouteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mPopupView != null) {
ViewGroup parent = (ViewGroup) mPopupView.getParent();
if (parent != null)
parent.removeView(mPopupView);
}
try {
mPopupView = getActivity().getLayoutInflater().inflate(R.layout.fragment_map, null);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
mPopupWindow = new PopupWindow(mPopupView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.showAtLocation(getActivity().findViewById(R.id.routeBtn), Gravity.CENTER, 0, 0);
mPopupWindow.update(0, 0, 800, 800); //don't hardcode
mPopupView.findViewById(R.id.doneBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mPopupWindow.dismiss();
}
});
}
});
Upvotes: 1
Views: 1441
Reputation: 3255
According to this answer you need to set a background drawable to the popup window.
I tested it out and you don't even need to handle the OnKeyListener
by yourself.
This should be enough to make 'back button' work:
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setFocusable(true);
Upvotes: 0
Reputation: 154
Add a button on your pop up window layout and find the id of button using popupView.findviewbyid(id)
,then set clicklisener on this button and call mPopupWindow.dismiss()
;.
Also you can try this:
popupView.setOutsideTouchable(true);
popupView.setFocusable(true);
Upvotes: 4