How to make custom dialog with rounded corners in android

前端 未结 16 923
半阙折子戏
半阙折子戏 2020-11-28 02:15

What I am trying to do: I am trying to make a custom dialog in android With rounded corners.

What is happening: I am able to make c

16条回答
  •  佛祖请我去吃肉
    2020-11-28 02:36

    I implemented rounded dialog using CardView in the custom layout and setting its corner radius.

    Here's my xml code.

    
    
    
    
    
        
    
    
        
    
        
    
        

    After that, i called it in Kotlin as follows :-

        val builder = AlertDialog.Builder(mContext)
        val viewGroup: ViewGroup = findViewById(android.R.id.content)
        val dialogView: View = 
        LayoutInflater.from(mContext).inflate(R.layout.subdomain_bottom_sheet, 
        viewGroup, false)
        val alertDialog: AlertDialog = builder.create()
        alertDialog.setView(dialogView,0,0,0,0)
        alertDialog.show()
        val windowParam = WindowManager.LayoutParams()
        windowParam.copyFrom(alertDialog.window!!.attributes)
        windowParam.width = AppConstant.getDisplayMetricsWidth(mContext) - 100
        windowParam.height = WindowManager.LayoutParams.WRAP_CONTENT
        windowParam.gravity = Gravity.CENTER
        alertDialog.window!!.attributes = windowParam
        
       
        alertDialog.window!!.setBackgroundDrawable
        (ColorDrawable(Color.TRANSPARENT))
    

    Where last line is very important. Missing that would cause a color(mostly white) to show behind the corners.

提交回复
热议问题