Regis
Regis

Reputation: 176

Calling an AlertDialog in another class

I want to create a java class that holds all of the alerts for my application. I want to make the methods static so they are easier to call. Here is my code.

Alert Class

public class Alerts {

    // Player has not selected a team
    public static void noPlayerTeam(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(view);
        builder.setMessage("Select your team.");
        builder.setCancelable(true);

        builder.setNeutralButton(
                "Okay",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        AlertDialog a = builder.create();
        a.show();
    }
}

Acvitety code

Alerts.noPlayerTeam(view);

The documentation shows AlertDialog.Builder(Context context)

I'm struggling with the getting the context of the activity to the Alert class. The error with the code above is Error:(13, 63) error: incompatible types: View cannot be converted to Context.

I know the code works because I can execute it functionally in the same script as the activity.

Upvotes: 0

Views: 2318

Answers (3)

Krishna
Krishna

Reputation: 1

Common class for Alert

public class Alert {
    // Alert with Message and ok
    public static void showAlertDialog(Context context,String title ,String msg){

        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }});
        builder.create().show();
    }
    // Alert with Message and ok Click Event
    public static void showAlertDialog(Context context, String title ,String msg, DialogInterface.OnClickListener listener){

        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton("OK", (DialogInterface.OnClickListener) listener);
        builder.setNegativeButton("Cancel",(DialogInterface.OnClickListener) listener);
        builder.create().show();
    }
}

In Main Activity Class

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Alert.showAlertDialog(this,"Testing","Demooo");

        Alert.showAlertDialog(this,"Testing","Demoooooo", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which==DialogInterface.BUTTON_POSITIVE) {
                    //Your Code
                }else
                    {
                    //Your Code
                }

            }});
    }
}

Upvotes: 0

Vishal Yadav
Vishal Yadav

Reputation: 1024

Try this

  • put this in Constant

public static void alertDialogShow(Context context, String message)
    {
        final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setMessage(message);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which) 
            {
                alertDialog.dismiss();
          } 
        }); 
        alertDialog.show();
    }

  • call in your Activity

Constant.alertDialogShow(YourActivity.this,"Your Error Message");

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Use view.getContext() instead of view :

    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());

Need to pass valid Context(UI Context) to Builder constructor to get AlertDialog instance.

Upvotes: 3

Related Questions