How to create a Custom Dialog box in android?

后端 未结 22 2773
囚心锁ツ
囚心锁ツ 2020-11-21 07:06

I want to create a custom dialog box like below

\"enter

I have tried the foll

22条回答
  •  盖世英雄少女心
    2020-11-21 07:54

    Create Custom Alert Dialog

    cumstomDialog.xml

    
    
    
    
    
    
    
    
    
        

    Show your custom dialog on your activity:

      public void showDialog(String title, String des, int icon) {
    
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.custom_dialog);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    
        Button cancelBTN = dialog.findViewById(R.id.cancelBTN);
        Button acceptBTN = dialog.findViewById(R.id.acceptBtn);
        TextView tvTitle = dialog.findViewById(R.id.title);
        TextView tvDescription = dialog.findViewById(R.id.description);
        ImageView ivIcon = dialog.findViewById(R.id.icon);
    
        tvTitle.setText(title);
        tvDescription.setText(des);
        ivIcon.setImageResource(icon);
    
        cancelBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    
        acceptBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
            }
        });
    
        dialog.show();
    }
    

    Call like this:

    showDialog("Title", "Message", R.drawable.warning);

提交回复
热议问题