Search code examples
androidxmlkotlinsizedialogfragment

DialogFragment's width and height is wrap content, but I wrote match parent in attributes


My DialogFragment at the moment is using in code match_parent sizes for width and height, but in emulator and in real device he is wrap_content.Used the databinding, kotlin

I have addcardialog.kt:

package kz.jy.android.ui.custom_dialog_add_car

import android.content.res.Resources
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import kz.jy.android.R
import kz.jy.android.databinding.DialogAddCarBinding
import kz.jy.android.model.Car

class AddCarDialog : DialogFragment() {
    private lateinit var carBodyTypeList: ArrayList<String>
    private lateinit var binding: DialogAddCarBinding
    lateinit var carList: ArrayList<Car>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        carBodyTypeList = ArrayList()
        carList = ArrayList()
        carBodyTypeList.add("Кроссовер")
        carBodyTypeList.add("Седан")

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = DataBindingUtil.inflate(inflater, R.layout.dialog_add_car, container, false)

//        val r : Resources = activity!!.resources
//        dialog!!.window!!.setLayout(r.getDimension(R.dimen.))

        binding.btnAddCarToList.setOnClickListener {
            if (binding.etCarModel.equals("") || binding.etCarNumberAddCar.equals("")
                || !binding.spinnerCarBodyType.isSelected) {
                Toast.makeText(context!!, "Заполните все ячейки.", Toast.LENGTH_SHORT).show()
            } else {
                val car = Car(
                    binding.etCarModel.text.toString(),
                    binding.spinnerCarBodyType.selectedItem.toString(),
                    binding.etCarNumberAddCar.text.toString()
                )
                carList.add(car)
            }
        }
        binding.root.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        return binding.root
    }
}

and dialog_add_car.xml:

<layout>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:background="@drawable/bg_white_10dp_radius"
        android:orientation="vertical"
        android:padding="8dp">

        <TextView
            style="@style/BasicTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/new_car"
            android:textColor="@color/text_color_blue" />

        <TextView
            style="@style/BasicTextView.SmallText.EditTextTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_marginTop="@dimen/default_margin_between_view_medium"
            android:text="@string/new_car"
            android:textColor="@color/text_color_on_white" />

        <EditText
            android:id="@+id/etCarModel"
            style="@style/BasicEditText"
            android:layout_width="match_parent"
            android:layout_height="@dimen/edit_text_default_height"
            android:layout_marginTop="@dimen/default_margin_between_view_small" />

        <TextView
            style="@style/BasicTextView.SmallText.EditTextTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_marginTop="@dimen/default_margin_between_view_medium"
            android:text="@string/car_body_type"
            android:textColor="@color/text_color_on_white" />

        <Spinner
            android:id="@+id/spinnerCarBodyType"
            style="@style/BasicEditText"
            android:layout_width="match_parent"
            android:layout_height="@dimen/edit_text_default_height"
            android:layout_marginTop="@dimen/default_margin_between_view_small" />

        <TextView
            style="@style/BasicTextView.SmallText.EditTextTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_marginTop="@dimen/default_margin_between_view_medium"
            android:text="@string/car_number"
            android:textColor="@color/text_color_on_white" />

        <EditText
            android:id="@+id/etCarNumberAddCar"
            style="@style/BasicEditText"
            android:layout_width="match_parent"
            android:layout_height="@dimen/edit_text_default_height"
            android:layout_marginTop="@dimen/default_margin_between_view_small" />

        <TextView
            android:id="@+id/btnAddCarToList"
            style="@style/BasicTextView.SmallText"
            android:layout_width="match_parent"
            android:layout_height="@dimen/button_default_height"
            android:layout_marginTop="@dimen/default_margin_between_view"
            android:background="@drawable/bg_blue_10dp_radius"
            android:gravity="center"
            android:text="@string/add_car_button" />
    </LinearLayout>
</layout>

and in activity I call the dialog fragment with this code:

val addCarDialog = AddCarDialog()
addCarDialog.show(supportFragmentManager.beginTransaction(),"tag")

I guess, my problem is in dialog class


Solution

  • Try below solution.

    • First of all create your own style in your style.xml file

        <style name="AppTheme.Dialog.MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowMinWidthMajor">100%</item>
            <item name="android:windowMinWidthMinor">100%</item>
        </style>
      
    • Then set above theme in your dialog fragment onCreate method

        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.AppTheme_Dialog_Custom)