androidkotlinandroid-fragmentsdialogcustomdialog

Visibilty gone layout of custom dialog from another function


I have a custom dialog in kotlin fragment which have 2 button and a progress bar in fuction submitSpk()

class RincianPembelianFragment : androidx.fragment.app.Fragment(), BlockingStep {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val v = inflater.inflate(R.layout.fragment_spk_rincian_bayar, container, false)

    //initialize your UI

    return v
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    activity?.supportFragmentManager?.popBackStack(null, androidx.fragment.app.FragmentManager.POP_BACK_STACK_INCLUSIVE)
    setContext(activity!!.applicationContext) 
}
fun submitSpks (){
        val inflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val builder = AlertDialog.Builder(context!!)

        val dialogView = inflater.inflate(R.layout.activity_signature, null)
        builder.setView(dialogView)
        builder.setTitle("Tanda Tangan Pembeli")
        //builder.show()
        builder.setCancelable(false)
        dialogView.clear.setOnClickListener {
            dialogView.signature_view.clearCanvas()
        }

        dialogView.save.setOnClickListener {
            submitTtd() // or some fuction else
            dialogView.save.visibility = View.INVISIBLE
            dialogView.progressBar.visibility = View.VISIBLE
        }

        builder.setNegativeButton(""){ dialog: DialogInterface?, which: Int ->

        }

        builder.show()

}

fun submitTtd(){
    // here there will be a crud transaction and visible/invisible button save
}
}

my custom dialog is look like this layout

i want to visibility gone the button save dialogView.save.visibility = View.VISIBLE from another fuction e.g saveTtd() how can i do this? i have tried this but my dialog can't show.. anyone can help me? Thanks before


Solution

  • Storing the dialogView in a global variable should do the job.
    Also, call the build() method on the AlertDialog.Builder instance before showing the dialog:

    class RincianPembelianFragment : androidx.fragment.app.Fragment(), BlockingStep {
    
        private lateinit var dialogView: View
    
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            val v = inflater.inflate(R.layout.fragment_spk_rincian_bayar, container, false)    
            //initialize your dialog view
            dialogView = inflater.inflate(R.layout.activity_signature, null)
            return v
        }
        
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            activity?.supportFragmentManager?.popBackStack(null, androidx.fragment.app.FragmentManager.POP_BACK_STACK_INCLUSIVE)
            setContext(activity!!.applicationContext) 
        }
    
        fun submitSpks() {
            // Using requireContext() is suggested
            val builder = AlertDialog.Builder(requireContext())
        
            builder.setView(dialogView)
            builder.setTitle("Tanda Tangan Pembeli")
            builder.setCancelable(false)
            dialogView.clear.setOnClickListener {
                dialogView.signature_view.clearCanvas()
            }
        
            dialogView.save.setOnClickListener {
                submitTtd() // or some fuction else
            }
        
            builder.setNegativeButton(""){ dialog: DialogInterface?, which: Int ->
        
            }
        
            // Build the dialog builder before showing it 
            val dialog = builder.build()
            dialog.show()
        }
        
        fun submitTtd(){
            // here there will be a crud transaction and visible/invisible button save
            dialogView.save.visibility = View.INVISIBLE
            dialogView.progressBar.visibility = View.VISIBLE
        }
    }