Android AlertDialog won't display - android

I have an app that shows a welcome screen via an alert dialog. I use the following code in the onCreate method of the Activity:
wsBuilder = new AlertDialog.Builder(this);
wsBuilder.setIcon(android.R.drawable.ic_dialog_alert);
wsBuilder.setTitle(R.string.instructions_title);
wsBuilder.setMessage(R.string.welcome_1);
wsBuilder.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
wsBuilder.show();
When I start the app, most of the time the screen darkens like it does when the dialog is going to
display, but the dialog never shows up. The screen just stays darkened and none of the touch events get through. I can click the back button on the phone to dismiss the dialog and then the app works like normal, but I can't figure out why the dialog doesn't fully display. Once in a while the dialog actually displays, but most of the time it doesn't.
Any help in running down this issue would be greatly appreciated.

OnCreate may not be the best place for it as the application is loading try using it onStart
public void onStart()
{
//Your code here
}

Activity would be better for wellcome screen.

Related

Android dialog setNegativeButton without listener

Curiosity question here.
I use a lot of dialogs builders and most of the time my negative cancel button do nothing except dismiss the dialog. The code I found everywhere on the web is this :
builder.setNegativeButton(
"cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
I happened to find out that this code do exactly the same :
builder.setNegativeButton("cancel", null);
So my question is then : is that a bad habit not to manually dismiss the dialog, and if yes why ?
It is somewhat documented behaviour, see:
http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog
When the user touches any of the action buttons created with an AlertDialog.Builder, the system dismisses the dialog for you.
So with null listener you exercise this implicit documented behaviour of Dialog.
What can go possibly wrong? (yeah, I think there's at least 50% chance that some custom ROM out there is not behaving properly... then again, who cares about custom ROMs failing to follow documented behavior, I don't any more, too much of that BS).
I learned this myself in an Android course in school. Basically, you only need to implement the button listener if you need additional functionality.
So it is not "habit" to include the click listener, it is just clear intent.

Making a button appear in Android when a condition is fulfilled

As the title says I'd like to know the best method for generating a button upon some condition being fulfilled in my code. In this case, I'd like clicking on a particular imageView "s02" to make a button appear in my activity.
I know that you can make AlertDialogs appear using code like this:
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// code code code code code }
});
I tried substituting Button for AlertDialog on the first line but I couldn't use Builder on Button.
Also, should I create the button in a separate section of code then simply make it appear when the condition is set, or should I put the button's functionality in the code creating the button?
Putting your button in the xml layout and just making it appear when needed is the easiest route. If that isn't good enough, you can create a Button via new Button(Context), set all the parameters you need, then add it to the parent layout.

Display Android dialog on top of another?

I have 2 alert dialogs, dialog A and dialog B. Clicking on one of dialog A's buttons will bring up dialog B. I then want to have a button that will dismiss dialog B and return to dialog A.
Is there a way to do this apart from dialog B performing a showDialog(dialogA) ?
This works, but you can see the reload of dialog A, instead of just returning to an already existing dialog A. Performing a dismiss in dialog B just dismisses both of them.
A minor question, but I'd like to see if there is a way to stack them on top of one another.
Thanks
Using the basic dialog building blocks it is not possible to have them stack, you will need to re-show the first dialog.
The reason for this is that when you press a dialog button it internally will dismiss the dialog as part of the process for calling the click handler you assigned for each button in the dialog builder API.
One way around this is to make a custom dialog layout that doesn't have the dismiss behavior, by setting up your own buttons in the layout, rather than using those created by the dialog builder methods. Then in the click handler for you own buttons simply show the second dialog without dismissing the first.
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
As one reply mentioned, you cannot do this with standard dialogs. But you can do it by making the first dialog be an activity that is styled to look like a dialog, and the second is actually a dialog.
Just set the theme of the activity in your layout like so:
<activity android:theme="#android:style/Theme.Dialog">
See this topic on making an activity that looks like a dialog.
https://stackoverflow.com/a/1979631/602661
dismiss the dialog from within itself.
Edit, here is some clearer code.
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
return;
}
});
alertDialog.show();
You should use inside your custom layout one view/button and based on this view/button click you can create another dailog without cancel first one, if you use builder.setNegativeButton or builder.setPositiveButton your current dialog will be close, my working code like as,
AlertDialog.Builder builder = new AlertDialog.Builder(ActivityAppImages.this,R.style.your_style);
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.your_custom_layout, null);
final Button mButtonCreateOtherDailog = (Button)dialoglayout.findViewById(R.id.txt_create_second_dailog);
mTextView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//create your other dailog here
}
});
builder.setView(dialoglayout);
builder.show();

Homing Out of Activity Causes Dialog Boxes Not To Appear When Restarted

I've been poking around at this problem and I can't seem to figure it out. I have a simple app with a few normal views and a GL surface view, I make a few dialog boxes using onCreateDialog() and everything seems fine.
#Override
protected Dialog onCreateDialog(int id)
{
super.onCreateDialog(id);
Dialog m_Dialog = null;
// help dialog
if (id == HELP_DIALOG)
{
m_Dialog = new Dialog(this);
m_Dialog.setContentView(R.layout.help_dialog);
m_Dialog.setTitle("Instructions - Press BACK to close");
}
}
However if I use home to exit the app then go back into the app the dialogs no longer appear, however the screen dims as if the dialog was being displayed. I am getting the call to onPrepareDialog() even when the dialog does not show, I tried some things in there like calling show() off of the dialog. It gets a bit more strange, if I then switch to my GL surface view and back the dialogs work again. I am using a ViewAnimator to switch between my views. I am pretty sure I am handling the lifecycle correctly, over riding onPause() / onResume()
#Override
protected void onResume()
{
super.onResume();
m_Sensors.StartSensors();
m_GameThread.Pause(false);
glSurface.onResume();
}
As always, thanks for the help.
I haven't tried working with GL on android, but I have experienced some home button/re-open app weirdness myself recently - in my case it turned out to be connected to the issues below, which you might want to check out:
http://code.google.com/p/android/issues/detail?id=5277
http://code.google.com/p/android/issues/detail?id=2373
Hope it can help you get on the right track.

how to chain (immediately reuse) the same dialog

I'm trying to create an introduction to my program with a helpful dialog message system.
I don't want to overload the user with too much text at once so I want to break up my dialog into parts.
Each part of course would have its own message.
I use a separate static class to handle message delivery and flow logic; and it's working fine.
I also actually use 3 Dialogs.
One for the first message (since you can't go back), one for the middle message and one for the final message (since you can't go forward).
I'm able to call the middle message from the first message with no problem. I'm also able to return to the first message. But when I try to reshow the middle message from the middle message dialog the new dialog doesn't appear.
Example:
Let's say I have 4 messages, so the middle message will need to appear twice:
First message appears: user clicks next
Middle message appears: user clicks previous
First message appears: user clicks next
Middle message appears: user clicks next
Middle message appears: user clicks next
Final message appears
The problem is that I get no dialog on step 5.
I'm using onPrepareDialog to reinitialize the dialogs as they're used. Right now it's basically a clone of onCreateDialog where each case in the switch calls the builder method appropriate for that dialog.
This is the code for my middle dialog method. (The other 2 are about the same. You can guess what they look like from this.)
protected AlertDialog buildMiddleNoticeDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder
.setTitle(Notice.getTitle())
.setMessage(Notice.getMessage())
.setCancelable(false)
.setNegativeButton(resources.getString(R.string.notice_next_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
showDialog(Notice.next());
}
})
.setPositiveButton(resources.getString(R.string.notice_previous_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
showDialog(Notice.previous());
}
});
return builder.create();
}
I used a normal dialog with a TextSwitcher inside and two buttons to step forward or backward. The TextSwitcher simply changes the text and some animation are possible for text change. Try that!
The functionality of the two buttons simply depends on the position in my string array where all the messages are stored in ordered positions.
Thats my activity which is started with a dialog theme: http://saintfeintcity.org/projects/sfc/repository/entry/trunk/src/org/saintfeintcity/activities/TippsAndTricksActivity.java

Categories

Resources