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();
Related
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.
I have an application in which my requirement is to show Home screen and then if any button is clicked open another activity in Popup(Which means remaining part of first activity will be visible but not click-able) and if any button is clicked in the second activity launch the third activity same as second.
Also I am not looking for setting in manifest: #android:style/Theme.Dialog
In the image you can see a pop up and a parent screen behind that popup.Please help me to solve this. I have to open an activity on top of another activity with some part of previous visible.
Thanks in advance.
just add this tag to your activity tag in your manifest file
android:theme="#android:style/Theme.Dialog"
then the activity will look according to your requirements hope this helps ..
We can set the style of an activity to open it as pop up using:- android:theme="#android:style/Theme.Translucent.NoTitleBar" in Manifest for activity you want to open as Popup and providing margins to layout.
You can use AlertDialog and inflate its custom layout in its setView() method.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog
// layout
builder.setView(inflater.inflate(R.layout.yourLayout, null));
AlertDialog ad = builder.create();
ad.setTitle("Your title");
ad.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
ad.show();
through the help of various tutorials, I've managed to write a custom dialog that displays a listview populated by records from a local database. I have set click listeners and figured out how to retrieve the record at the listview item clicked by setting the cursor at the position returned and so on...Now what I want to do is, dismiss this dialog when an item is clicked, and automatically open a new dialog with this cursor's content as the name of the table from which to re-populate the new listview. I'd like to know if anyone knows the best way of doing this in terms of application structure.
Currently, I am calling my dialog to show in my Activity like this:
public void onClick(View view) {
switch(view.getId()) {
case R.id.pickerbutton:
showDialog(DIALOG_PICK_CATEGORY);
break;
}
}
protected Dialog onCreateDialog(int id) {
dialog = null;
switch(id) {
case DIALOG_PICK_CATEGORY:
CustomDialogList.Builder customBuilder = new
CustomDialogList.Builder(SendCookieActivity.this);
customBuilder.setTitle(R.string.category);
dialog = customBuilder.create();
break;
}
return dialog;
}
After this dialog is shown, the user picks a category from the CustomDialogList dialog. I am having a hard time thinking of how to make it so that after the category is picked, this dialog is dismissed (or looks like it's dismissed) and the same one with newly populated items appears (or can be a completely new dialog too). and when someone presses the back button, the previous dialog is shown. Think of it as a file explorer but with only two levels of depth. I'd like to keep using my CustomDialogList because I have customized its look to match everything else in my app. Perhaps what would help me with this problem besides or instead of code, would be some diagrams of how this type of UI flow has been implemented before along with some pseudo code.
Thanks.
Assuming your custom dialog extends Dialog (or one its sub-classes). Have your Activity implement DialogInterface.onDismissListener. Then after you create the dialog with...
dialog = customBuilder.create();
...use dialog.setOnDismissListener(this); before you show it.
Your Activity will have to implement...
#Override
public void onDismiss(DialogInterface dialog) {
// Identify which dialog was dismissed and do something
}
When I have had to do this in the past, I have the onCancel for the dialog open the new dialog.
I want the user to enter a string into an EditText into a popup. I have taken a look in Android Developers here .
But this kind of popup is not explained. How can I make it?
On the same page you linked to, check out how they say to create a custom dialog.
Create an XML layout for the dialog that has an EditText. Then show it:
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
dialog.show();
Create your own custom dialog by extending Dialog. Your custom dialog class will have an onCreate() callback, in which you can call setContentView to whatever view you want, just like with an Activity.
Simply create a view to look however you'd like and use that one. Then, when you want to use your dialog, simply get an instance, like Dialog myDialog = new MyCustomDialog(getParent(), R.style.some_style); and then myDialog.show();
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.