Abilash
Abilash

Reputation: 1163

Show dialog only using Context instead of Activity instance

I could show dialog if I uses an Activity instance but when I uses Context or Application Context instance Dialog is not showing.

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle(title);
            builder.setMessage(msg);

            if (null != positiveLabel) {
                builder.setPositiveButton(positiveLabel, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.cancel();
                        if (null != listener) {
                            listener.onOk();
                        }
                    }
                });
            }

            if (null != negativeLable) {
                builder.setNegativeButton(negativeLable, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.cancel();
                        if (null != listener) {
                            listener.onCancel();
                        }
                    }
                });
            }

            builder.create().show();

Can you please give me a solution to show dialog without using Activity instance

Upvotes: 3

Views: 23061

Answers (4)

Orwa Ogwari
Orwa Ogwari

Reputation: 151

To use a context, it has to be passed from the activity on which the dialog is being created from. And this is only possible when you create your own dialog instead of using the AlertDialog.Builder provided by Java. Here is a custom one and we shall use it to get the application context.

public class CustomDialogPopUp extends Dialog implements View.OnClickListener {

public Activity a;
public Context c;
public Dialog d;
//CUSTOMIZE YOUR DIALOG AS YOU LIKE
RecyclerView pointAbsorber;
ImageButton addPoint, attachPoint;
EditText addContent;

//VERY IMPORTANT CONSTRUCTOR THAT WE SHALL USE TO GET THE CONTEXT
public CustomDialogPopUp(@NonNull Context context, Activity a, Context c) {
    super(context);
    this.a = a;
    this.c = c;
}

//INITIALIZING YOUR CUSTOM DIALOG VIEWS AND WHAT NOT. IT NEEDS AN XML FILE BY THE WAY
public void initViewSnActions(){
    pointAbsorber = findViewById(R.id.pointsRV);
    addPoint = findViewById(R.id.addPoint);
    attachPoint = findViewById(R.id.attachPoint);
    addContent = findViewById(R.id.addContent);
    
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.custom_dialog);
    initViewSnActions();
}

//ON CLICK LISTENER FOR YOUR VIEWS
@Override
public void onClick(View v) {
    
}

}

To pass the application context, one has to initialize it in whichever class you want it to appear as follows. Note how the activity and the context is passed

NotesPopUp notesPopUp = new NotesPopUp(PersistentTest.this, getParent(), getApplicationContext());
notesPopUp.show();

Upvotes: 0

Rubber Duck
Rubber Duck

Reputation: 3723

For some reason [at least in android 2.1] a toast can be on the application context but not a progress dialog

MyActivity.this is an activity specific context which does not crash

MyActivity.getApplicationContext() is global and will crash progress bars and in later versions also toasts.

Upvotes: 2

Shubhayu
Shubhayu

Reputation: 13562

This is one of the MOST important things that you must always remember about Contexts. There are 2 types of contexts, Activity contexts and Application contexts. You will observe in many UI related classes, a Context is passed. This is not the Application context! In such cases you must always pass an Activity Context. Except for a Toast, no other UI component will work with Application context.

Application Context is always passed when you want some service or component which is Application related, like the Telephony Manager, Location Manager etc.

For UIs, you must always pass a context that is UI related which is the Activity.

Upvotes: 28

omermuhammed
omermuhammed

Reputation: 7385

The problem is something I faced recently too, you cant create a dialog without and activity instance. getApplicationContext() call doesn't work too. The way I did this is to make the call to a method that creates the dialog, from an activity, and pass "this" i.e. the reference to that activity as a parameter.

If you are going to reuse this code, as a reusable component or as a mechanism to create dialogs at multiple places, create a base activity class and have this method in there, and use it in sub-classed activities as needed.

Upvotes: 7

Related Questions