Home » Android » android – Custom dialog with a RecyclerView is inflated really small on screen-Exceptionshub

android – Custom dialog with a RecyclerView is inflated really small on screen-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have created a dialog function which creates a dialog on screen where you can choose something from a RecyclerView (RView adapter code at the bottom):

fun showSelectACategoryDialog(context: Context,allCategories: LiveData<List<NotesCategory>>, owner: Fragment,chosenCategoryID: (categoryID: Long) -> Unit) {

    val dialog = Dialog(context)

    val adapter = DialogCategoriesRecyclerViewAdapter(context, chosenCategoryID)

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setCancelable(true)
    dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    dialog.setContentView(R.layout.dialog_select_category)

    dialog.dialog_select_a_category_recyclerView.adapter = adapter

    allCategories.observe(owner, Observer {
        adapter.submitList(it)
    })

// Click listeners

   dialog.show()

The dialog function is then called with:

 binding.notesCategoryTextView.setOnClickListener {
            showSelectACategoryDialog(
                context!!,
                notesEditViewModel.allCategories,
                NotesEditFragment(),
                selectedCategory(notesCategory)
            )
        }

But when the dialog is called, it looks like this:
Really small-sized dialog shows up

Here is the code of the dialog’s recycler view adapter, in case it’s relevant:


class DialogCategoriesRecyclerViewAdapter(
    var context: Context,
    var chosenCategoryID: (categoryID: Long) -> Unit
) : ListAdapter<NotesCategory, DialogCategoriesRecyclerViewAdapter.ViewHolder>(SingleCategoryDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = ViewHolder.from(parent)

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.bind(getItem(position), context)

        holder.itemView.setOnClickListener {
            chosenCategoryID(getItem(position).categoryID)
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val categoryNameTextView: TextView = itemView.findViewById(R.id.dialog_select_a_category_textView)

        fun bind(item: NotesCategory, context: Context) {
            categoryNameTextView.text = item.categoryName
        }

        companion object {
            fun from(parent: ViewGroup): ViewHolder {
                val view = LayoutInflater.from(parent.context).inflate(
                    R.layout.row_dialog_select_category_all_categories, parent, false
                )
                return ViewHolder(view)
            }
        }
    }
}

Here’s the XML layout of the dialog that does not work:

<?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"
    xmlns:tools = "http://schemas.android.com/tools"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:layout_margin = "16dp"
    android:background = "@drawable/dialog_rounded_background">

    <TextView
        android:id = "@+id/dialog_select_a_category_title"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_marginTop = "16dp"
        android:text = "@string/dialog_categories_title"
        android:textColor = "@color/Black"
        android:textSize = "25sp"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.5"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id = "@+id/create_new_category_button"
        android:layout_width = "0dp"
        android:layout_height = "wrap_content"
        android:layout_marginStart = "16dp"
        android:layout_marginTop = "16dp"
        android:layout_marginEnd = "16dp"
        android:background = "@drawable/border_square_transparent"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.5"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toBottomOf = "@+id/dialog_select_a_category_title"
        tools:ignore = "RtlSymmetry">

        <ImageView
            android:id = "@+id/_imageview"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:layout_marginStart = "16dp"
            app:layout_constraintBottom_toBottomOf = "parent"
            app:layout_constraintStart_toStartOf = "parent"
            app:layout_constraintTop_toTopOf = "parent"
            app:srcCompat = "@drawable/dialog_plus"
            tools:ignore = "ContentDescription" />

        <TextView
            android:id = "@+id/_textview"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:layout_margin = "16dp"
            android:layout_marginStart = "56dp"
            android:text = "@string/dialog_create_new_category_button"
            android:textColor = "@color/Black"
            android:textSize = "20sp"
            app:layout_constraintBottom_toBottomOf = "parent"
            app:layout_constraintEnd_toEndOf = "parent"
            app:layout_constraintStart_toEndOf = "@+id/_imageview"
            app:layout_constraintTop_toTopOf = "parent"
            app:layout_constraintVertical_bias = "0.506" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id = "@+id/dialog_select_a_category_recyclerView"
        android:layout_width = "0dp"
        android:layout_height = "0dp"
        android:layout_marginStart = "16dp"
        android:layout_marginTop = "16dp"
        android:layout_marginEnd = "16dp"
        android:layout_marginBottom = "20dp"
        android:background = "@drawable/border_square_transparent"
        app:layoutManager = "androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.5"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toBottomOf = "@+id/create_new_category_button"
        tools:listitem = "@layout/row_dialog_select_category_all_categories" />
</androidx.constraintlayout.widget.ConstraintLayout>

And here’s the XML layout of the dialog that does work:

<?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"
    xmlns:tools = "http://schemas.android.com/tools"
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:layout_margin = "16dp"
    android:background = "@drawable/dialog_rounded_background">

    <ImageButton
        android:id = "@+id/dialog_create_category_confirm"
        android:layout_width = "50dp"
        android:layout_height = "50dp"
        android:layout_marginStart = "208dp"
        android:layout_marginTop = "64dp"
        android:layout_marginEnd = "16dp"
        android:layout_marginBottom = "16dp"
        android:background = "@android:color/transparent"
        android:src = "@drawable/dialog_check"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintEnd_toEndOf = "@+id/dialog_create_category_category_name"
        app:layout_constraintStart_toEndOf = "@+id/dialog_create_category_cancel"
        app:layout_constraintTop_toBottomOf = "@+id/dialog_create_category_category_name"
        tools:ignore = "ContentDescription" />

    <ImageButton
        android:id = "@+id/dialog_create_category_cancel"
        android:layout_width = "50dp"
        android:layout_height = "50dp"
        android:layout_marginStart = "16dp"
        android:layout_marginTop = "64dp"
        android:layout_marginBottom = "16dp"
        android:background = "@android:color/transparent"
        android:src = "@drawable/dialog_cancel"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintStart_toStartOf = "@+id/dialog_create_category_category_name"
        app:layout_constraintTop_toBottomOf = "@+id/dialog_create_category_category_name"
        tools:ignore = "ContentDescription" />

    <EditText
        android:id = "@+id/dialog_create_category_category_name"
        android:layout_width = "0dp"
        android:layout_height = "wrap_content"
        android:layout_marginStart = "16dp"
        android:layout_marginTop = "32dp"
        android:layout_marginEnd = "16dp"
        android:ems = "10"
        android:hint = "@string/dialog_create_category_hint"
        android:importantForAutofill = "no"
        android:inputType = "textPersonName|textCapSentences"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.487"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toBottomOf = "@+id/dialog_create_category_title" />

    <TextView
        android:id = "@+id/dialog_create_category_title"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_marginTop = "16dp"
        android:text = "@string/dialog_create_a_category_title"
        android:textColor = "@color/Black"
        android:textSize = "30sp"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.5"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

The second dialog is called by clicking “@+id/create_new_category_button” (which is the only thing that shows on the screen) and it fills the screen width minus margin just fine

How to&Answers:

You’ve to define fixed size to dialog’s layout or just create a full width dialog.

Try this:

dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);