What makes a dialog box visible?

I have a very simple dialog defined as:

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

public class MyDialog{

  private String promptReply = null; // local variable to return the prompt reply value

  public String showAlert(String ignored, Context ctx)
    LayoutInflater li = LayoutInflater.from(ctx);
    View view = li.inflate(R.layout.promptdialog, null);

    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setTitle("Dialog Title");

    builder.setPositiveButton("OK", (myActivity)ctx);
    builder.setNegativeButton("Cancel", (myActivity)ctx);

    AlertDialog ad = builder.create();

    return "dummystring";    


And when I try to display it in the onCreate()

after call setContentView()

for the main layout of the activity, the dialog just doesn't show:

MyDialog dialog = new MyDialog();
dialog.showAlert("Why isn't this shown???", this);


On the other hand, if I place the same exact call before calling setContentView()

for the main layout of the activity, the dialog shows that it is ok.

My question is why?

Why critical order in this case?

What am I missing?


source to share

1 answer

In your code, to inflate the view, use something like this:

View layout = inflater.inflate(R.layout.promptdialog,
                           (ViewGroup) findViewById(R.id.layout_root));


where layout_root

is the ID of the top level layout of your custom dialog.



All Articles