Android Custom Alert dialog

Process One: Extending AlertDialog Class..

//in style.xml


<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColor">@android:color/black</item>
<item name="android:textColorPrimary">@color/colorPrimary</item>
<item name="dialogCornerRadius">18dp</item>
<item name="android:background">@color/bg_color</item>
<item name="background">@color/bg_color</item>
</style>
//create a class by extending AlertDialog
class AddItemDialog(
    context: Context,
    private val listener: OnAddItemListener
) : AlertDialog(context,R.style.AlertDialogStyle) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.layout_add_new)

        setTitle("Add New Item")


        submit_btn.setOnClickListener(View.OnClickListener {
            val title=title.text.toString().trim()
            val price=price.text.toString().trim()
            if(title.isEmpty() || price.isEmpty()){
                Toast.makeText(context, "fill up the form", Toast.LENGTH_SHORT).show()
                return@OnClickListener
            }
            val item =ShoppingItemTable(title,price.toDouble())
            listener.onAddItem(item)
            dismiss()
        })
    }
}


interface OnAddItemListener {
    fun onAddItem(item: ShoppingItemTable)
}


Leave a Comment

Your email address will not be published. Required fields are marked *

top