Skip to content

Commit

Permalink
Prevent popup from getting off the screen
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavpandey committed Jul 21, 2022
1 parent 69b09e0 commit 1446b6c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void setActionOnClickListener(@Nullable View.OnClickListener onClickListe
@Override
protected int getMaxWidth() {
return (int) getAnchor().getContext().getResources()
.getDimension(R.dimen.ads_popup_max_width_large);
.getDimension(R.dimen.ads_popup_max_width);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.pranavpandey.android.dynamic.util.DynamicSdkUtils;
import com.pranavpandey.android.dynamic.util.DynamicUnitUtils;
import com.pranavpandey.android.dynamic.util.DynamicViewUtils;
import com.pranavpandey.android.dynamic.util.DynamicWindowUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -225,6 +226,16 @@ protected int getMaxWidth() {
.getDimension(R.dimen.ads_popup_max_width);
}

/**
* The offset to adjust the size of the window if enough space is not available.
*
* @return The offset to adjust the size of the window if enough space is not available.
*/
protected int getSizeOffset() {
return (int) getAnchor().getContext().getResources()
.getDimension(R.dimen.ads_popup_offset);
}

/**
* This method will be called just before showing this popup.
*
Expand Down Expand Up @@ -318,7 +329,7 @@ public void run() {
Dynamic.setVisibility(content, View.GONE);
}

mPopupWindow = new PopupWindow(view, getMaxWidth(),
mPopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, true);
PopupWindowCompat.setWindowLayoutType(mPopupWindow,
WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
Expand All @@ -327,6 +338,11 @@ public void run() {
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mPopupWindow.setAnimationStyle(R.style.Animation_AppCompat_DropDownUp);

if (getMaxWidth() + getSizeOffset()
< DynamicWindowUtils.getAppUsableScreenSize(getAnchor().getContext()).x) {
mPopupWindow.setWidth(getMaxWidth());
}

if (getAnchor().getRootView() != null) {
try {
TransitionManager.endTransitions((ViewGroup) getAnchor().getRootView());
Expand All @@ -343,7 +359,7 @@ public void run() {

// Check for RTL language.
if (DynamicViewUtils.isLayoutRtl(getAnchor())) {
viewCenterX = viewCenterX + getAnchor().getWidth() - getMaxWidth();
viewCenterX = viewCenterX + getAnchor().getWidth() - mPopupWindow.getWidth();
OFFSET_X = -OFFSET_X;
}

Expand Down
1 change: 1 addition & 0 deletions dynamic-support/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
<!-- Popup -->
<dimen name="ads_popup_max_width">300dp</dimen>
<dimen name="ads_popup_max_width_large">340dp</dimen>
<dimen name="ads_popup_offset">@dimen/ads_margin_large</dimen>
<dimen name="ads_popup_selector_icon_size">24dp</dimen>

<!-- Bottom Sheet -->
Expand Down

0 comments on commit 1446b6c

Please sign in to comment.