A_Matar
A_Matar

Reputation: 2320

Alert Dialogue in Android-beginners

I am trying to create a simple alert dialogue and I am following the steps from this link However,I am getting the error: getActivity() can't be resolved After some search I understood that getActivity() can be user defined, but I am not sure what should I make it do.

Here is what I have:

public void about(View v1) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
        builder.setNeutralButton(R.string.close, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                finish();//go to the previous activity
            }
        });
        
// Set other dialog properties
        builder.setMessage(R.string.myName);

// Create the AlertDialog
        AlertDialog dialog = builder.create();}

Upvotes: 0

Views: 46

Answers (2)

// in a fragment
    new AlertDialog.Builder(getContext())
                            .setTitle("Alert title")
                            .setMessage("Alert Message")
                            .setCancelable(false)
                            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            }).show();

Upvotes: 0

Vaizadoo
Vaizadoo

Reputation: 330

getActivity() is fragments method to get Activity. If u are in activity u just need to replace this by YourActivityClassName.this

Upvotes: 2

Related Questions