Home > Net >  Android custom dialog not displaying title and message
Android custom dialog not displaying title and message

Time:10-04

I have a custom dialog and both the title and the message are not being displayed:

import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.sibela.horn.R

class CarYearDialog : AppCompatDialogFragment() {

    private val args: CarYearDialogArgs by navArgs()
    private val car by lazy { args.car }

    private val rootView by lazy { View.inflate(activity, R.layout.dialog_car_year, null) }
    private val yearInput by lazy { rootView.findViewById<TextView>(R.id.year_input) }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val builder = setupDialog()
        val dialog = builder.create()
        dialog.setCanceledOnTouchOutside(false)
        return dialog
    }

    private fun setupDialog(): AlertDialog.Builder {
        return AlertDialog.Builder(requireActivity(), R.style.AlertDialogTheme)
            .setTitle("Ano")
            .setMessage("Escolha o ano do veículo")
            .setView(rootView)
            .setNegativeButton("Cancelar") { _, _ -> }
            .setPositiveButton("Ok") { _, _ ->
                val year = yearInput.text.toString().toInt()
                val directions =
                    CarYearDialogDirections.actionCarYearDialogToHornToContactsFragment(car, year)
                findNavController().navigate(directions)
            }
    }
}

enter image description here

CodePudding user response:

You can use the MaterialAlertDialogBuilder.

import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.sibela.horn.R

class CarYearDialog : AppCompatDialogFragment() {

    private val args: CarYearDialogArgs by navArgs()
    private val car by lazy { args.car }

    private val rootView by lazy { View.inflate(activity, R.layout.dialog_car_year, null) }
    private val yearInput by lazy { rootView.findViewById<TextView>(R.id.yearinput) }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val builder = setupDialog()
        val dialog = builder.create()
        dialog.setCanceledOnTouchOutside(false)
        return dialog
    }

    private fun setupDialog(): AlertDialog.Builder {
        return MaterialAlertDialogBuilder(this.requireContext())
            .setTitle("Ano")
            .setMessage("Escolha o ano do veículo")
            .setView(rootView)
            .setNegativeButton("Cancelar") { ,  -> }
            .setPositiveButton("Ok") { , _ ->
                val year = yearInput.text.toString().toInt()
                val directions =
                    CarYearDialogDirections.actionCarYearDialogToHornToContactsFragment(car, year)
                findNavController().navigate(directions)
            }
    }
}
  • Related