7

I am trying to set Popup window similar to the old Facebook comment section.

Rounded corner dialog box but I am facing a problem with size of dialog box and showatlocation of the Dialog box.

When I try this code on different mobile:

        val display = windowManager.defaultDisplay
        val size = Point()
        display.getSize(size)
        val popupWindow = PopupWindow(customView, size.x-30, size.y-300, true)
        popupWindow.showAtLocation(linearLayout1, Gravity.CENTER, -3, 100)
        popupWindow.setAnimationStyle(R.style.PopupAnimation)

Xml File :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_bg"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10px">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/popup_rcv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <EditText
            android:id="@+id/new_comment_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Please enter Comment" />

        <Button
            android:id="@+id/comment_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit" />
    </LinearLayout>
</LinearLayout>

//Animation from Source

Output On Oneplus 6T: One plus

In one plus my animation also disable when I change .showAtLocation to other numbers

Output on Lenovo K8 Note:

K8 Note

In K8 note Location of Popup change if I change to another value.

Thank you in Advance.

Ashish
  • 6,791
  • 3
  • 26
  • 48
  • Why not just use dialog instead it will give your desired affect unless you want the animation.This is way simpler way to prompt user for feedback. This way you don't have to worry about where it will show up plus you can add custom styles to the dialog https://stackoverflow.com/questions/12351330/what-is-the-difference-between-popupwindow-and-dialog – skryshtafovych Feb 04 '19 at 20:13

4 Answers4

5

Looks like these 2 phones have different screen resolutions. So you have to use DP instead of pixels. Check this post

Popup size. Firstly you have to convert dialog size to pixels. These are random values and they are hardcoded, but you can take them from resources or harcode too.

val popUpWidthDp = 200
val popUpHeightDp = 100

val popUpWidthPx = convertDpToPx(popUpWidthDp)
val popUpHeightPx = convertDpToPx(popUpHeightDp)

val popupWindow = PopupWindow(customView, popUpWidthPx, popUpHeightPx, true)

Popup position. Firstly you need to convert dp to px and then you can calculate popup position related to screen size.

val popUpLeftSideMarginDp = 50
val popUpTopMarginDp = 100

val popUpXPoint = convertDpToPx(popUpLeftSideMarginDp)
val popUpYPoint = convertDpToPx(popUpTopMarginDp)

popupWindow.showAtLocation(linearLayout1, Gravity.CENTER, popUpXPoint, popUpYPoint)

Check out this answer to understand how to convert dp into pixels and vise versa.

If popup should have size and position related to screen size then you have to change these values:

  • popUpHeightPx, popUpWidthPx - popup size

  • popUpXPoint, popUpYPoint - popup position

Let me know if you need detailed explanation.

Community
  • 1
  • 1
Yamko
  • 535
  • 1
  • 3
  • 13
  • where i need to change pixel to dp means i am not using pixels anywhere not even in my xml file so can you provide more information. Sorry i am new to android designing – Ashish Jan 24 '19 at 10:23
  • there was animation of slider in popup but it removed after i put those values -3 and 100 – Ashish Jan 24 '19 at 10:27
  • Updated the answer – Yamko Jan 24 '19 at 11:41
  • its not working it says java.lang.Float cannot be cast to java.lang.Double – Ashish Jan 30 '19 at 11:14
  • I thought you are using Kotlin. Where exactly do you have exception? You need to cast Float to Double explicitly. – Yamko Jan 31 '19 at 13:03
1

Create PopupWindow as:

popupWindow= new PopupWindow(
                        customView,
                        LayoutParams.MATCH_PARENT,
                        LayoutParams.MATCH_PARENT
                );

And set its location as:

popupWindow.showAtLocation(linearLayout1, Gravity.CENTER, 0, 0)

And add customView top margin 10dp or 5dp in xml

Zumbarlal Saindane
  • 1,199
  • 11
  • 24
0

Try with below solution : its in Java but you can convert it in Kotlin

PopupWindow popupWin = new PopupWindow(mContext);

// Measure layout here, then we can get measureHeight.
layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupWin .setWidth(layout.getMeasuredWidth()); 
popupWin .setHeight(layout.getMeasuredHeight()); //you can pass height as you want.
popupWin .setContentView(layout);
popupWin .showAsDropDown(anchorView,0 ,0, Gravity.NO_GRAVITY);

Here anchorView is a view in where we want to open PopupWindow in as dropdown.

PopupWindow

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

I think you would like to convert the pixels into dp, rather then dp into pixels, in order to fit different resolutions of screens. More information here.

This is how to convert pixels to dp.

Daniel Reyhanian
  • 579
  • 4
  • 26