Search code examples
androidandroid-activitycustomdialog

How to create custom dialog common method for entire android app using java?


I am custom dialog in every activity write custom dialog code.How to write common method passing two parameters context and message string?please give me any suggestion.

final Dialog dialog = new Dialog(Activity.this,R.style.DialogTheme);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.notify_received_activity);
// set the custom dialog components - text and button
TextView text = (TextView) dialog.findViewById(R.id.txtmsg);
text.setText("Tracking Number : " + mTrackingNR);
Button dialogButton = (Button) dialog.findViewById(R.id.btncancel);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {dialog.dismiss();} });
dialog.show();

Solution

  •   /**
             * Display Dialog
             **/
            public static void showDialog(final Context context, String message) {
                Dialog dialog = new Dialog(context,R.style.DialogTheme);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.notify_received_activity);
        // set the custom dialog components - text and button
        TextView text = (TextView) dialog.findViewById(R.id.txtmsg);
        text.setText(message);
        Button dialogButton = (Button) dialog.findViewById(R.id.btncancel);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {dialog.dismiss();} });
        dialog.show();
            }
    

    and simply call anywhere as below:

    showDialog(ActivityName.this,"message");