How to create a Custom Dialog box in android?

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

    Here I have created a simple Dialog, like:

    Dialog Box

    custom_dialog.xml

    
    
    
        
        
    
        
    
            

    You have to extends Dialog and implements OnClickListener

    public class CustomDialogClass extends Dialog implements
        android.view.View.OnClickListener {
    
      public Activity c;
      public Dialog d;
      public Button yes, no;
    
      public CustomDialogClass(Activity a) {
        super(a);
        // TODO Auto-generated constructor stub
        this.c = a;
      }
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog);
        yes = (Button) findViewById(R.id.btn_yes);
        no = (Button) findViewById(R.id.btn_no);
        yes.setOnClickListener(this);
        no.setOnClickListener(this);
    
      }
    
      @Override
      public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_yes:
          c.finish();
          break;
        case R.id.btn_no:
          dismiss();
          break;
        default:
          break;
        }
        dismiss();
      }
    }
    

    How to Call Dialog ?

    R.id.TXT_Exit:
    CustomDialogClass cdd=new CustomDialogClass(Values.this);
    cdd.show();  
    

    Updates

    After a long time one of my friends asked me to make a curved shape dialog with a transparent background. So, Here I have implemented it.

    enter image description here

    To Make a curved shape you need to create a separate curve_shap.XML as below,

    
    
        
    
        
    
        
    
    
    

    Now, add this curve_shap.XML in your main view Layout. In my case I have used LinearLayout

    
    ...
    
    

    How to call this ?

    CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
    cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    cdd.show();
    

    I hope that works for you.

提交回复
热议问题