How to create a Custom Dialog box in android?

后端 未结 22 2829
囚心锁ツ
囚心锁ツ 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:55

    Simple first create a class

     public class ViewDialog {
    
            public void showDialog(Activity activity, String msg){
                final Dialog dialog = new Dialog(activity);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setCancelable(false);
                dialog.setContentView(R.layout.custom_dialogbox_otp);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    
                TextView text = (TextView) dialog.findViewById(R.id.txt_file_path);
                text.setText(msg);
    
                Button dialogBtn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
                dialogBtn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
    //                    Toast.makeText(getApplicationContext(),"Cancel" ,Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
    
                Button dialogBtn_okay = (Button) dialog.findViewById(R.id.btn_okay);
                dialogBtn_okay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
    //                    Toast.makeText(getApplicationContext(),"Okay" ,Toast.LENGTH_SHORT).show();
                        dialog.cancel();
                    }
                });
    
                dialog.show();
            }
        }
    

    then create a custom_dialogbox_otp

    
    
        
    
            
    
                
    
                    
    
                
    
                
    
                    
                
            
    
            
    
                
    
                    
                
    
                
    
    
                    
    
                        

    then in your drawable create beneath xml files.
    for round_layout_white_otp.xml

    
    
        
        
        
        
    

    for round_layout_otp.xml

    
    
        
        
        
        
    

    round_button

    
    
        
        
        
        
    

    Then finally use the underneath code to visual ur dialog :)

    ViewDialog alert = new ViewDialog();
            alert.showDialog(ReceivingOTPRegActivity.this, "OTP has been sent to your Mail ");
    

    your output :)

提交回复
热议问题