• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps alert.setPositiveButton arguments confusing

At first i'm a beginner (completely) to an android development, so please forgive me my noob questions.
I have a problem with understanding this sample code below. I can't manage out what is happening in a alert.setPositiveButton arguments section. DialogInterface.OnClickListener() is an interface so what a "new" word in a front of it stands for? Please try to explain this to me step by step.

Code:
  public void aalert() {
        AlertDialog.Builder alert = new AlertDialog.Builder(DisplayMessageActivity.this);
        
        alert.setTitle("Are u ok?");
        alert.setMessage("?");
        alert.setPositiveButton("Yes",
         new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
           Toast.makeText(DisplayMessageActivity.this, "Success", Toast.LENGTH_SHORT).show();
          }
         });

        alert.show();
    }
 
You are correct in assuming that you cannot instantiate an interface alone, however if you provide the implementation during the instantiation, it's perfectly acceptable. This is because providing the implementation during instantiation is effectively creating an anonymous inner class.
 
Back
Top Bottom