Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

401
Visualizações
Custom dialog not showing, just faded black background

Ok I've been trying to write a separate class for custom dialog with static function here is the code

class CustomDialog {
    companion object {
        fun create(context: Context, content: String) {
            context as Activity
            val inflater = context.layoutInflater
            val view = inflater.inflate(R.layout.dialog_info, null)
            val infoDialog = AlertDialog.Builder(context).create()
            view.dialog_content.text = content
            view.dialog_okButton.setOnClickListener {
                println("CLICKED")
                infoDialog.dismiss()
            }
            infoDialog.setContentView(view)

            infoDialog.show()
        }
    }
}

and the layout which I'm inflating is this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    android:background="@drawable/dialog_background"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="20dp">

    <TextView
        android:id="@+id/dialog_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/iranyekanregular"
        android:text="Some text"
        android:textColor="@color/white" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="10dp"
        android:background="@color/white" />

    <Button
        android:id="@+id/dialog_okButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/iranyekanregular"
        android:gravity="center"
        android:clickable="true"
        android:focusable="true"
        android:text="OK" />
</LinearLayout>

A simple textview a separator line and a button to dismiss the dialog.

but whenever I trigger the button to show the dialog it just shows the faded black screen and not the dialog itself. If I use the setView(view) instead of setContentView it pretty much shows it self but as it is only the middle(content) part of the default dialog I see the background of the default dialog and still the dismiss button won't work. any help will be much appreciated. btw I've searched a lot with no luck.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You forgot to add the inflated view to the dialog.

class CustomDialog {
companion object {
    fun create(context: Context, content: String) {
        context as Activity
        val inflater = context.layoutInflater
        val view = inflater.inflate(R.layout.dialog_info, null)
        val infoDialogBuilder = AlertDialog.Builder(context)
        infoDialogBuilder.setView(view)
        val infoDialog = infoDialogBuilder.create()
        view.dialog_content.text = content
        view.dialog_okButton.setOnClickListener {
            println("CLICKED")
            infoDialog.dismiss()
        }
        infoDialog.setContentView(view)

        infoDialog.show()
    }
}

That is the missing line:

infoDialogBuilder.setView(view)
over 4 years ago · Santiago Trujillo Relatório

0

The issue is arising due to the fact that you are not giving it any style.

Use  infoDialog.setView(view)

And while creating dialogbox give a material theme style

val infoDialog = AlertDialog.Builder(ContextThemeWrapper(context,android.R.style.ThemeOverlay_Material_Dialog)).create()

and in the end

infoDialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
infoDialog.show()

Rest of your code is fine.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda