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.
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 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 am currently creating a popup window by this code:
View popupView = inflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.Animation);
LinearLayout linearLayout = popupView.findViewById(R.id.linearLayout1);
linearLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
popu = false;
}
});
ImageView imageView = popupView.findViewById(R.id.imageView1);
imageView.setImageResource(drawables[position]);
popupWindow.showAtLocation(imageView, Gravity.CENTER, 0, 0);
popu = true;
However, I want the popup to have multiple pages, like here:
How can I implement a multi-paged popup into my app? can I add a ViewPager into my PopupWindow or I need to switch to a Dialog instead?
Use DialogFragment instead.
https://developer.android.com/reference/android/app/DialogFragment.html
Inside of it you will need a ViewPager with your multiple pages
Add page indicators to make it look similar
You can use a library or TabLayout for the indicators:
How do you create an Android View Pager with a dots indicator?
I am trying to make something like a pop-up window, that would appear when clicked on a view in a fragment. I want this pop-up window or whatever, to not make the fragment dark, like a Dialog Fragment does.
And I also want the pop up to be positioned where the view is clicked.
Would be good if it has its own activity and layout so I can do some custom changes in it.
Can you plese show me some sample code?
The following should work perfect in accordance with your specification. Call this method from inside onClick(View v) of OnClickListener assigned to the View:
public void showPopup(View anchorView) {
View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);
PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// Example: If you have a TextView inside `popup_layout.xml`
TextView tv = (TextView) popupView.findViewById(R.id.tv);
tv.setText(....);
// Initialize more widgets from `popup_layout.xml`
....
....
// If the PopupWindow should be focusable
popupWindow.setFocusable(true);
// If you need the PopupWindow to dismiss when when touched outside
popupWindow.setBackgroundDrawable(new ColorDrawable());
int location[] = new int[2];
// Get the View's(the one that was clicked in the Fragment) location
anchorView.getLocationOnScreen(location);
// Using location, the PopupWindow will be displayed right under anchorView
popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY,
location[0], location[1] + anchorView.getHeight());
}
The comments should explain this well enough. anchorView is the v from onClick(View v).