Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

394
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!