How to adjust popup menu when pop outside container - android

I am using a custom popup menu that pops inside a RecycleView container.
When the view touched is the on at the bottom, the popup menu hides the controls bellow the RecycleView.
I want to adjust the popup position in this case, bringing the popup up the exact distance needed so the menu keeps inside the container.
This should be easy, but i am having difficulties getting the coordinates and applying the calculations from inside the Adapter that deals with clicks on items.
Here is what i managed so far:
void show(FragmentActivity activity, View touchedView, DataItem item) {
LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.popup_menu, null);
PopupWindow popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
bind(popupWindow, popupView, item);
//draws the menu perfectly bellow the touched element,but doesn't take in account the parent view area
popupWindow.showAsDropDown(touchView);
}

This is what I did:
private void showPopupMenu(final View view, final int position) {
// inflate menu
final PopupMenu popup = new PopupMenu(view.getContext(), view);
popup.setGravity(Gravity.END);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
final EditText editText = (EditText) promptView.findViewById(R.id.input_text);
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
And here is the outcome:

Found out. 2 Things were on the way and are explained in comments.
void show(FragmentActivity activity, View touchView, IDataItem dataItem) {
LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.popup_menu, null);
PopupWindow popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
bind(popupWindow, popupView, dataItem);
//1º problem: View item is inside ViewHolder.Item, so container is 2 levels up and touched item one level up.
View container = (View) touchView.getParent().getParent();
View item = (View) touchView.getParent();
int containerHeight = container.getHeight();
//2º problem: to get size before the popup view is displayed: ref:https://stackoverflow.com/a/15862282/2700303
popupView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int yOff=0;
//if popup view will draw pass the bottom, get the amount needed to adjust the y coordinate
if (ContainerHeight < item.getHeight() + item.getTop() + popupView.getMeasuredHeight())
yOff = ContainerHeight- (item.getTop() + item.getHeight() + popupView.getMeasuredHeight());
popupWindow.showAsDropDown(touchView,0,yOff);
}

Related

when i set popup outside touchable to false edit text does not showing keyboard in pop up window

I have a PopupWindow on my activity, the popupWindow has Edit text. The thing is when i set popup Window outside touchable to false Edit text does not opening keyboard in pop up window in android and i am dismissing the popup by giving cancel button inside popup. Please help thanks in advance.
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = inflater.inflate(R.layout.repeat, null);
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = false;
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
final LinearLayout until = popupView.findViewById(R.id.Until);
Button pop_done = popupView.findViewById(R.id.pop_done);
Button pop_cancel = popupView.findViewById(R.id.pop_cancel);
dropdown = popupView.findViewById(R.id.spinner);
final EditText repetition = popupView.findViewById(R.id.repeat_times);
Try this
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = inflater.inflate(R.layout.repeat, null);
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
final PopupWindow popupWindow = new PopupWindow(popupView, width, height);
popupWindow.setFocusable(true);
popupWindow.update();
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
popupWindow.setOutsideTouchable(false);
final LinearLayout until = popupView.findViewById(R.id.Until);
Button pop_done = popupView.findViewById(R.id.pop_done);
Button pop_cancel = popupView.findViewById(R.id.pop_cancel);
Spinner dropdown = popupView.findViewById(R.id.spinner);
final EditText repetition = popupView.findViewById(R.id.repeat_times);

How to show popupWindow as drop down onClick of a Recyclerview row element?

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.

Add View into RecyclerView

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.

TapTargetView is placed behind popup menu

I created TapTargetView (or TapTargetSequence) in all of my activities and it displaying well, but for inner views of popup menu, it hides behind my popup menu window! exactly between under activity and popup menu. how can I bring it to top, please? Is parameter of TapTargetSequence ((Activity) context) correct? "context" or "this" occurs error !
public class Popup_Menu implements OnClickListener {
PopupWindow popup; View layout; Context context;
WindowManager wm;
void showPopup(final Activity context) {
this.context=context;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) ((Activity) context).findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = layoutInflater.inflate(R.layout.popup_layout_menu, viewGroup);
// Creating the PopupWindow
popup = new PopupWindow(context);
popup.setContentView(layout);
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
popup.showAtLocation(layout, Gravity.BOTTOM, 30 ,0 );
tapTarget_menu();
} /////////////////// close of showPopup() !
// TapTaget_menu
public void tapTarget_menu() {
new TapTargetSequence((Activity) context)
.targets(
TapTarget.forView(layout.findViewById(R.id.chkb_menu_remem), "remember", "last lesson")
// first target
.targetCircleColor(R.color.sabz_seyedi)
.outerCircleColor(R.color.sabzabi_kmrng)
.dimColor(R.color.sabz_seyedi)
.titleTextSize(22)
.descriptionTextSize(16)
.textColor(R.color.white)
.drawShadow(true)
.transparentTarget(true)
.cancelable(false)
.targetRadius(60),
// second target
TapTarget.forView(layout.findViewById(R.id.ll_call_menu), "call", "connecting friends!")
.targetCircleColor(R.color.sabz_seyedi)
.outerCircleColor(R.color.sabzabi_tireh)
.dimColor(R.color.sabz_seyedi)
.titleTextSize(22)
.descriptionTextSize(16)
.textColor(R.color.white)
.drawShadow(true)
.transparentTarget(true)
.cancelable(false)
.targetRadius(60)
).start();
}
}
I found my solution ! I have changed this code after popup.showAtLocation(), like following and it can display tapTargetView on top of PopupWindow.
VIVA ME !!!
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
layoutParams.packageName = context.getPackageName();
layoutParams.format = PixelFormat.TRANSLUCENT;
layoutParams.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
then, out of showPopup() method:
public void tapTarget_menu1(){
TapTarget target = TapTarget.forView(layout.findViewById(R.id.ll_call_menu), "remember", "last lesson");
.targetCircleColor(R.color.sabz_seyedi)
.outerCircleColor(R.color.sabzabi_tireh)
.titleTextSize(24)
.descriptionTextSize(18)
.textColor(R.color.black)
.drawShadow(true)
.cancelable(false)
.transparentTarget(true)
.targetRadius(60);
content = (ViewGroup) layout.findViewById(android.R.id.content);
final TapTargetView tapTarget_menu1 = new TapTargetView(context, wm, content, target, new TapTargetView.Listener(){
#Override
public void onTargetClick(TapTargetView view) {
tapTarget_menu2();
super.onTargetClick(view);
}
});
((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).addView(tapTarget_menu1, layoutParams);
}

Pop up window to display some stuff in a fragment

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).

Categories

Resources