Reputation: 2632
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.setView(view);
builder.setPositiveButton("OK", (myActivity)ctx);
builder.setNegativeButton("Cancel", (myActivity)ctx);
AlertDialog ad = builder.create();
ad.show();
return "dummystring";
}
}
And when I try to display it in onCreate()
after calling setContentView()
for the activity's main layout, the dialog simply 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 activity's main layout, the dialog shows just fine.
My question is why?
Why is the order critical in this case?
What am I missing?
Upvotes: 3
Views: 149
Reputation: 9362
In your code to inflate 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 top level layout of your custom dialog.
Upvotes: 2