0

In VB to use an alert box you write:

Response=Msgbox("Click YES to continue, NO to Abort",vbyesno)
If response=vbyes...do something

In Java we have to write yards more code to do this. What I wanted to write was a utility function that could be called from anywhere and return a response. I wrote:

 protected static boolean Response=false;
public  boolean CreateAlert(String AlertMessage){   
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

alertbox.setMessage(AlertMessage);
      alertbox.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
        Response=true; 
      } 
  }); 
     alertbox.setNegativeButton("NO" , new DialogInterface.OnClickListener(){
   public void onClick(DialogInterface arg0, int arg1) { 
       Response=false;
   }       
 });
  alertbox.show(); 
  return Response;

However, I could not move it to a different class because AlertDialogBuilder needs a this which presumably means I need to pass a context variable from another class. A Message Box is an almost essential component of interactive forms, and all I want is to add 1 line of code for each time I open a Message Box...there must be a way!!

Nigel
  • 1

1 Answers1

1

I could not move it to a different class because AlertDialogBuilder needs a this which presumably means I need to pass a context variable from another class.

Yes. Moreover, your code doesn't work -- Response is null, because show() is not a blocking call.

A Message Box is an almost essential component of interactive forms

You are welcome to your opinion. Modal dialogs, particularly confirmation dialogs like you are proposing, are considered bad form in modern UI development, particularly on mobile devices. See Why are modal dialog boxes evil?, for example.

all I want is to add 1 line of code for each time I open a Message Box...there must be a way!!

No, because that would imply a blocking show() call. Android, like most modern UI frameworks, is event-driven, and in the case of Android and some other frameworks, the UI is intrinsically single-threaded.

BTW, you may wish to consider adopting Java programming styles (e.g., methods, variables, and data members starting with a lowercase letter).

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491