title: How to put an alert dialog in another class inside a function
body: <p>Guys i've created a custom <code>date and time picker</code> in my app now i'm using that <code>picker</code> several times . So, for avoiding the redundancy of code I thought i should put that function inside a separate function in another class from where i can  call that whenever i need that but i'm not able to do so, Please help.
  The separate class is :</p>
  
  <pre><code>public class DateTimePicker {
      String str;
  
      public String returnDate(final Context ctx) {
          AlertDialog.Builder builder1 = new AlertDialog.Builder(ctx);
          final DatePicker picker1 = new DatePicker(ctx);
  
          try {
              Field f[] = picker1.getClass().getDeclaredFields();
              for (Field field : f) {
                  if (field.getName().equals("mYearPicker")|| field.getName().equals("mYearSpinner") ) {
                      field.setAccessible(true);
                      Object yearPicker = new Object();
                      yearPicker = field.get(picker1);
                      ((View) yearPicker).setVisibility(View.GONE);
                  }
              }
          } catch(Exception ex) {
              ex.printStackTrace();
          }
          picker1.setCalendarViewShown(false);
  
          builder1.setTitle("Please select date on which you would be leaving :")
              .setView(picker1)
              .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                      SimpleDateFormat parseFormat = new SimpleDateFormat("EEE dd MMM");
                      Date date1 = new Date();
                      date1.setDate(picker1.getDayOfMonth());
                      date1.setMonth(picker1.getMonth());
                      final String s = parseFormat.format(date1);
                      Log.e("DATE",s);
  
                      //Time picker
                      AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
                      final TimePicker picker = new TimePicker(ctx);
                      picker.setIs24HourView(true);
                      builder.setTitle("Please select time at which you would be leaving :")
                          .setView(picker)
                          .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int whichButton) {
                                  int hour = picker.getCurrentHour();
                                  int minute = picker.getCurrentMinute();
                                  str = s + " " + hour + ":" + minute;
                              }
                          })
                          .setNegativeButton("One way", new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int whichButton) {
                                  dialog.dismiss();
                              }
                          }).create().show();
                          //Time picker
                      }
                  }
              )
              .setNegativeButton("One way", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                      dialog.dismiss();
                  }
              })
              .create().show();
  
          return "hello "+ str ;
      }
  </code></pre>
  
  <p>and the code from where i'm using the above class is :</p>
  
  <pre><code>DateTimePicker obj = new DateTimePicker();
  Log.e("Object", "Created");
  String st = obj.returnDate(this.getActivity());
  Log.e("Function", "Called");
  System.out.println("Value returned :" + st);
  leaving.setText(st);
  </code></pre>
  
  <p>On log i'm getting "<strong>hello null</strong>" evething gets printed as soon as i called the function it is not even waiting for user input.
  please help thank you</p>