I have to show a popupwindow. If i show it in click event of the ImageView, Then i call dismiss it hides. But when i show it in Touch event of ImageView, then dismiss event calls, but the popup not hides. What are the reasons for this error?. How can i solve it?
The code i used to show popup window is
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; //320
CabotMessageHandler.printConsole("width of screen"+width);
//show Popup
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView=inflater.inflate(R.layout.gallerytoppopup, null, false);
pw = new PopupWindow(
popupView,
width,
30,
true);
// The code below assumes that the root container has an id called 'main'
pw.setAnimationStyle(R.anim.popupanimation);
pw.showAtLocation(this.findViewById(R.id.webview), Gravity.TOP, 0, 30);
Finally i find out the problem. The touch event calls twice, and two popups created. That was the reason to not hide the Popup when dismiss called.(One popup hides and other remains there).
Related
My popup window should be auto dismissed when click outside. I already read this topic, and have set background drawable to the window. Here is my code:
protected int showPopupWindow(final int popupWidth) {
hideCalendarCellPopupWindow();
final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = inflater.inflate(R.layout.popup_calendar_cell, null);
mCalendarCellPopupWindow = new PopupWindow(popupView, popupWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
mCalendarCellPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
mCalendarCellPopupWindow.setOutsideTouchable(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mCalendarCellPopupWindow.setElevation(...);
}
mCalendarCellPopupWindow.showAtLocation(....);
}
private hideCalendarCellPopupWindow() {
if (mCalendarCellPopupWindow != null) {
mCalendarCellPopupWindow.dismiss();
mCalendarCellPopupWindow = null;
}
}
A problem have appeared on Android 10, as you can touch outside of the phone screen and slide the finger inside the screen, meanwhile "recent apps" could be shown with such gesture.
So my issue is when I slide the finger from the bottom to the top a little bit and than go back to the bottom - popup window is not dismissed, moreover it cannot be dissmissed no more, as its property isShowing() returns false. I have tried to call popupWindow.dismiss() method inside onPause() but it is not called as well in such situation.
This screencast could explain the issue more precies: https://youtu.be/w2cQMvFMYkk
What could be the workaround?
If you want to dismiss your popup outside, you may set as following:
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setOutsideTouchable(true);
popupWindow.update();
I have tried using a code that is working fine except when I click on the last or end rows of the recyclerview, the popupWindow opens offscreen. Is there any way to show the popupWindow upSide of the view for end rows of the recyclerview.
Here is the code I am using:
public void showPopup(View v, int _pos) {
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_row_appointment_option, null);
popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
//TODO do sth here on dismiss
}
});
popupWindow.showAsDropDown(v, v.getScrollX(), v.getScrollY());
tv_view_profile = (TextView) popupView.findViewById(R.id.tv_view_profile);
tv_view_profile.setOnClickListener(this);
}
I would suggest you to go for PopupMenu which accepts AnchorView -> with the use of that we can get the exact position where we want to show popup -> you can pass AnchroView as View (The three dot imageview)
Here is the great example of it :
https://www.javatpoint.com/android-popup-menu-example
And also please use Appcompat version to get Material touch in menu.
Ok with custom layout you can go with your regular flow with minor changes --
popup.showAtLocation(anchorView, Gravity.BOTTOM, 0,
anchorView.getBottom() - 60);
this is just an example for bottom gravity. You can set gravity and margin according to your code.
anchorView = the view where you want to show this popup. Try this and let me know if you have any query.
I want to show a PopUpWindow under a LinearLayout after closing soft keyboard when clicking a button.
This is what I looking for
When user click the button,the keyboard hide,then the PopUpWindow show under the LinearLayout(area of the keyboard initially). Like this picture below
I try this code:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//hide the keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
LinearLayout bottomBar = (LinearLayout)findViewById(R.id.bottomBar);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.popup_window, null);
// create the popup window
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
popupWindow.showAtLocation(bottomBar, Gravity.BOTTOM, 0, 0);
}
}
But end up the PopUpWindow show like this:
The LinearLayout which contain the button,going to the bottom of screen when the keyboard is hide,the PopUpWindow show on top of the LinearLayout,so it become invisible.Like the image below:
So how can I get the desire behavior like the 1st picture?PopUpWindow show under the LinearLayout after the keyboard is hide.
Why are you trying to do this with popup window. I would suggest you to keep it simple layout. This layout will contain both the views (the layout with button and the popup layout under it). Just add this layout at the bottom of your screen and set the visibility of popup layout visibility VISIBLE/GONE whenever required. You can add the animation while showing or hiding the popup layout with help of this:
Show/Hide View using Slide Up and Down Animation.
I tried to add view into RecyclerView when click to Button in a ViewHolder.
When click to Button in ViewHolder3, a View (view add more) will add and appear like image above.
ViewAddMore will pin there and RecyclerView can scroll normal.
I tried but don't found any solution for my issue;
Have any suggest for my issue?
Please use PopupWindow to show this 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;
}
});
https://developer.android.com/reference/android/widget/PopupWindow.html
How to make a simple android popup window?
Hope this will help you.
I have a popup window that gets called from a listener in my fragment. Before it was called from a button press and it displayed immediately. However, after moving the method that creates it to a custom listener, it no longer pop ups instantly. But rather only after a click event or scroll event occurs anywhere on the screen. I'm guessing the screen needs to be refreshed somehow to show the popup? Here's my creating code:
/***POP UP WINDOW CODE***/
PopupWindow popupMessage;
PopupWindow pw;
#SuppressLint("NewApi")
private void popupInit(){
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Get screen dimensions to set the window size
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
final View layout = inflater.inflate(R.layout.add_interview, null, false);
// create a 300px width and 470px height PopupWindow
pw = new PopupWindow(layout, (int) (width*(0.8)), (int) (height*(0.8)), true);
// display the popup in the center
layout.post(new Runnable() {
public void run() {
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
pw.update(0, 0, pw.getWidth(), pw.getHeight());
}
});
}
If you don't want to delay the popup just do the following:
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
pw.update(0, 0, pw.getWidth(), pw.getHeight());