Narasimha
Narasimha

Reputation: 3766

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();

Upvotes: 0

Views: 3757

Answers (3)

Dante
Dante

Reputation: 133

How is specified in the android documentation, you can pass arguments to a dialog in the following way...

public static class MyDialogFragment extends DialogFragment {
    int mNum;   

   /**
    * Create a new instance of MyDialogFragment, providing its arguments
    */
   static MyDialogFragment newInstance(int num, /*other parameters*/) {
      MyDialogFragment f = new MyDialogFragment();

      Bundle args = new Bundle();
      args.putInt("num", num);
    //put all parameters on the Bundle
      f.setArguments(args);

      return f;
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      mNum = getArguments().getInt("num");
    //get the arguments
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //create the view

       return v;
  }
}

Upvotes: 0

Pararth
Pararth

Reputation: 8134

Here is what you want to do:

    public static void showAlert(Context context, String message)
        {
            final Dialog dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.custom_dialog);
            TextView messageTextView = (TextView) dialog.findViewById(R.id.customDialogMessageTextView);
            messageTextView.setText(message);
            Button okButton = (Button) dialog.findViewById(R.id.customDialogOkButton);
            okButton.setOnClickListener(new OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                        {
                            dialog.dismiss();
                        }
                });
            dialog.show();
        }  

The tricky part is if you want the action dialog.dismiss() every time the user clicks an ok button.

Where you need a custom implementation depending on the dialog, you will have to write code for that dialog separately.

Can call this method by passing the context from where ever you are calling.

Can be declared in a normal java class, need not be an activity. eg. Utility.showAlert(activityContext, "your message or variable");

Upvotes: 0

jyomin
jyomin

Reputation: 1967

  /**
         * 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");

Upvotes: 1

Related Questions