Search code examples
androidkotlinandroid-alertdialoglag

EditText widget lags alert dialog popup speed


Creating an empty alert dialog box at compile time and activating it at run time results in no lag when the box pops up, but as soon as I add an EditText widget (Plain Text or Multiline Text) to the xml resource layout file for the alert dialog, it lags the dialog popup speed by 2 to 3 seconds.

I tried all sorts of changes to fix this but no luck. For example, I've used all the different layout containers including contraintlayout and changed various attributes of the EditTexts but with no luck. This problem persists regardless of whether I create the dialog within a dialog fragment or in a regular fragment. I'm using the android jetpack framework with a navigation graph and kotlin. What can I do to fix this issue? Thanks.

Layout file (fragment_new_category_dialog.xml)

    <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:id="@+id/dialog_new_category_constraint_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.main.NewCategoryDialogFragment">

      <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

Fragment Code

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.Spinner
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment

import com.example.pomoplay.R
import kotlinx.android.synthetic.main.main_activity.*

class NewCategoryDialogFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        super.onCreateDialog(savedInstanceState)

        val view =
            requireActivity().layoutInflater.inflate(R.layout.fragment_new_category_dialog, null)

        return activity?.let { it ->

            // Use the Builder class for convenient dialog construction
            val builder = AlertDialog.Builder(it)
            builder.setTitle("Testing")
                .setPositiveButton(
                    "ok"
                ) { _, id ->

                }
                .setNegativeButton("cancel") { _, id ->

                }
                .setView(view)

            builder.create()

        } ?: throw IllegalStateException("Activity cannot be null")
    }

}

Solution

  • I found a solution to my own question. It seems like if I build a signed release apk of the app in Android Studio and then install that on my phone or emulator, the dialog pop up speed lag goes away. So the lag has something to do with running a debug version of the app on my phone. So, it shouldn't lag when a user installs my app from Google play.