1

I had tried so many ways but the size of the dialog never changed and I don't know but sometimes when I clicked outside of the dialog fragment, the dialog fragment doesn't close I mean in some special places and positions it closes.

here is the class:

class PopupWindowFragment : DialogFragment() {
    lateinit var mPopupWindowBinding: FragmentPopupWindowBinding
    lateinit var mPopupViewModel: PopupViewModel
    var id: Long = 0L
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mPopupWindowBinding =
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.fragment_popup_window,
                container,
                false
            )
        return mPopupWindowBinding.root
    }
}

here is the navigation:

<dialog
        android:id="@+id/navigation_dialog_fragment"
        android:name="com.example.holyquran.ui.popupWindow.PopupWindowFragment"
        tools:layout="@layout/fragment_popup_window">

EDIT Here is the layout:

<?xml version="1.0" encoding="utf-8"?>
    
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
        <data>
        <variable
            name="viewModel"
            type="com.example.holyquran.ui.popupWindow.PopupViewModel" />

        <variable
            name="userName"
            type="com.example.holyquran.data.model.UserInfo" />

        <variable
            name="loan"
            type="com.example.holyquran.data.model.Loan" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="20sp"
        tools:context=".ui.popupWindow.PopupWindowFragment">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@{userName.fullName}"
                android:textColor="@color/black"
                android:textSize="25sp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToIncrease()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginEnd="8dp"
                android:gravity="center"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginEnd="8dp"
                android:gravity="center"
                android:text="@string/submit_increase"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="left"
                android:src="@drawable/ic_baseline_add_circle_24" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToDecrease()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/submit_decrease"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_baseline_add_circle_24" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToLoan()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/submit_loan"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_baseline_add_circle_24" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/payPaymentLL"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToPayPayments()}"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/noLoanForUser"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="@string/no_loan_user"
                android:textColor="@color/gray600"
                android:textSize="14sp"
                android:visibility="invisible" />

            <TextView
                android:id="@+id/payPayment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/pay_payment"
                android:textColor="@color/black"
                android:textSize="20sp" />


            <ImageView
                android:id="@+id/img"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_baseline_add_circle_24" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1.5dp"
            android:layout_marginTop="10dp"
            android:background="@color/gray500" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToLoanList()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/payments_list"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_baseline_format_list_bulleted_24" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToUserTransactionHistory()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/transaction_history"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_baseline_format_list_bulleted_24" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1.5dp"
            android:layout_marginTop="10dp"
            android:background="@color/gray500" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.goToEditUserInfo()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/edit_user_info"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_baseline_edit_24" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/deleteUser"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:gravity="right"
            android:layoutDirection="ltr"
            android:onClick="@{() -> viewModel.deleteUser()}"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="8dp"
                android:text="@string/delete_user"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/ic_delete2" />
        </LinearLayout>
    </LinearLayout>
</layout>

I have tried this and even this but NONE of them worked.

Appreciate any Help :)

Zain
  • 37,492
  • 7
  • 60
  • 84
Amir
  • 333
  • 4
  • 16
  • 1
    Please post the `fragment_popup_window` layout – Zain Oct 22 '21 at 13:45
  • I have Edited my Question. Check it out please – Amir Oct 22 '21 at 14:27
  • Already the dialog layout width & height take the `match_parent`.. Do you want to size it programmatically? – Zain Oct 22 '21 at 14:50
  • 1
    I want to make the Dialog fragment smaller than what is it now because there isn't much space between the dialog fragment and the bottom of the phone. YES I want t to resize it programiticltly – Amir Oct 22 '21 at 15:01

1 Answers1

1

You can resize the width & height of a DialogFragment by changing the size of the dialog window;

For instance to set a 300dp width & 400dp height:

dialog!!.window!!.setLayout(
        300.toPx(requireContext()),
        400.toPx(requireContext())

But do this after the dialog layout view is created like in onViewCreated() or onStart(); not in the onCreateView():

class PopupWindowFragment : DialogFragment() {

    //..... your rest of code

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (dialog != null) dialog!!.window!!.setLayout(
            300.toPx(requireContext()),
            400.toPx(requireContext())
        )

    private fun Int.toPx(context: Context): Int =
        (this * context.resources.displayMetrics.density).toInt()

}

I want to make the Dialog fragment smaller than what is it now because there isn't much space between the dialog fragment and the bottom of the phone.

Now you need to know the current width & height of the dialog, and this requires to listen to viewTreeObserver of the dialog.

Add an id to the root ViewGroup of the dialog (LinearLayout): say dialog_root

<LinearLayout 
    android:id="@+id/dialog_root"

Then change the height & width within the viewTreeObserver` of the root layout:

class PopupWindowFragment : DialogFragment() {

    //..... your rest of code


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mPopupWindowBinding.dialogRoot.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                rootLayout.viewTreeObserver
                    .removeOnGlobalLayoutListener(this)

                // save default base layout width & height
                val height = rootLayout.height.toDp(requireContext())
                val width = rootLayout.width.toDp(requireContext())

                // change the width & height of the dialog (reduce by 50 dp)
                if (dialog != null) dialog!!.window!!.setLayout(
                    (width - 50).toPx(requireContext()),
                    (height - 50).toPx(requireContext())
                )
            }
        })

    private fun Int.toPx(context: Context): Int =
        (this * context.resources.displayMetrics.density).toInt()

    fun Int.toDp(context: Context): Int = (this / context.resources.displayMetrics.density).toInt()

}
Zain
  • 37,492
  • 7
  • 60
  • 84
  • Thanks of course if it was helpful I will mark your answer as accepted Thanks – Amir Oct 23 '21 at 12:03
  • Here is the link of the project and the dialog fragment is in ui.popupWindow.popupWindowFragment. I was wondering if you could resize it yourself because I myself get errors Thanks a million. https://github.com/AmirAndroidDeveloper/Holy-Quran – Amir Oct 23 '21 at 12:54
  • @AmirAndroidDeveloper Do you want something like [this](https://media.giphy.com/media/ZjUlsmeSs7PcMw7oPE/giphy.gif)? – Zain Oct 23 '21 at 16:23
  • your link has a problem: media.giphy.com took too long to respond. I can not open the link – Amir Oct 24 '21 at 06:56
  • @AmirAndroidDeveloper Check [here](https://drive.google.com/file/d/1kWcFW5TaDrDOarzaB9nGYKlnLdzZ1_4q/view?usp=sharing) – Zain Oct 24 '21 at 15:00
  • Yes it is what exactly I was looking for. Please pull a request to project on GitHub and then I will accept and finally I clone my project with your edits to my laptop , or u can push the project on your github and give me the link of the project. THANKS A MILLION – Amir Oct 24 '21 at 20:03
  • Or if you have created a new project please push it on your git lan and give me the link. I will merge(mix) code of your project to my project – Amir Oct 24 '21 at 20:29
  • I just copied only the dialog to a separate project to test it separately, I will pull it now – Zain Oct 24 '21 at 20:31
  • @AmirAndroidDeveloper Please replace your dialog with [this](https://pastebin.com/5tysdSSa).. – Zain Oct 24 '21 at 22:11
  • Ok just let me to try it out (test your code) – Amir Oct 25 '21 at 02:39
  • 1
    Thanks man. I have another issue that is related to android design too. I will post it as a new question and I think you know the answer. I will post the question soon so please check my profile to notice if I asked the question. again THANKS – Amir Oct 25 '21 at 13:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238508/discussion-between-amirandroiddeveloper-and-zain). – Amir Oct 25 '21 at 13:39