How to create a Custom Dialog box in android?

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

    This is an example dialog, create with xml.

    enter image description here

    the next code xml is just an example, the design or view is implemented here:

    
    
    
    
    
    
    

    this lines of code are resources of drawable:

    android:src="@drawable/dialog_cross"
    android:background="@drawable/btn_flat_red_selector"
    

    you could do a class extends Dialog, also something like this:

    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.dialog);
    
            TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
            text.setText(msg);
    
            Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
            dialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
    
            dialog.show();
    
        }
    }
    

    finally the form of call, on your Activity for example:

    ViewDialog alert = new ViewDialog();
    alert.showDialog(getActivity(), "Error de conexión al servidor");
    

    I hope its work for you.

提交回复
热议问题