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.
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)
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.