Reputation: 2643
I've inflated a PopupWindow using the method .showAsDropDown()
however I'm not sure why It's not allowing me to shift it right or left. It works perfectly fine when shifting up and down.
public class TestWindow extends PopupWindow {
private final Context context;
public TestWindow (Context context) {
super(context);
this.context = context;
setupView();
}
private void setupView() {
View view = LayoutInflater.from(context)
.inflate(R.layout.popup_window_wallet_options, null);
ButterKnife.bind(this, view);
setOutsideTouchable(true);
setFocusable(true);
setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.bgr_menu_clear_wallet));
setElevation(SpacingUtils.convertIntToDP(context, 4));
setContentView(view);
}
}
PopupWindow popupWindow = new TestWindow(context);
popupWindow.showAsDropDown(anchorButton, 50, -30);
Shifting the menu up by 30 works perfectly fine, but also I'm trying to shift it towards the left and it's not working. What am I doing incorrectly?
Note:
I've already tried it with 50
and -50
so I'm at lost why it's not moving horizontally
My R.layout.popup_window_wallet_options
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/bgr_menu_clear_wallet">
<TextView
android:id="@+id/tv_wallet_qr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?attr/selectableItemBackground"
android:padding="6dp"
android:text="Wallet QR"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_clear_wallet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?attr/selectableItemBackground"
android:padding="6dp"
android:text="Clear wallet"
android:textColor="@color/black" />
</LinearLayout>
Upvotes: 2
Views: 1538
Reputation: 40878
Disclaimer: This is not a direct fix to
showAsDropDown()
, but it could be a workaround withshowAtLocation()
.
By using the window decorView as the anchorView, and accumulating the actual anchorView location to x & y shift values.
int[] anchorView = new int[2];
anchorButton.getLocationInWindow(anchorView); // anchor button location in the window
popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.NO_GRAVITY,
anchorView[0] + 50,
anchorView[1] + anchorButton.getHeight() -30);
UPDATE:
The anchorButton is an ImageView. Can you post your code + xml for it
Nothing special than yours. This is the popup window layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test clear"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The activity just has a button as the anchorView:
And calling it:
Button button = findViewById(R.id.button);
PopupWindow popupWindow = new TestWindow(MainActivity.this);
popupWindow.showAsDropDown(button, 50, -30);
Upvotes: 2