I have a view defined as "public class ChessBoard extends View" in this class in one of the code flows I want to popup a Dialog box and then get a result from that dialog box.
I have tried the answer here: How to create a Custom Dialog box in android? but I have no Activity to pass to the constructor.
since your chessboard is a view there should be a getContext()-method (see here: https://developer.android.com/reference/android/view/View.html#getContext() ). you can cast the result to an activity:
Activity activity = (Activity) getContext()
Use this code
///-----------------------------------------------------
dialog_=new Dialog(this);
dialog_.setContentView(R.layout.dialog_submit);//this is path of xml file
dialog_.show();
Button submit_btn=dialog_.findViewById(R.id.but_submit); //button on dialog
Button cancel_btn = dialog_.findViewById(R.id.but_cancel);
final EditText edit_username=dialog_.findViewById(R.id.edit_name);
I used a custom dialog as a notify in the app, so I want it to keep show even the activity is changing, can it? or I should use other thing?
Use getApplicationContext().
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
Just want to know if it is possible to make a chronometer appear inside of a AlertDialog?
AlertDialog.Builder popup = new AlertDialog.Builder(ScoreNewGame.this);
popup.setTitle("Timer");
What next though?
Kind regards
You can use any View (including Chronometer) in a dialog with the AlertDialog.Builder.setView() method.
how can I add a new dialog with new layout on the currently running activity without making changes to the layout file of currently running activity. I cannot add a new activity as the old activity should remain active while the new dialog is displayed.
Here is a small snippet to get started on creating a Dialog and applying a layout file to it.
// create an instance of Dialog
Dialog dialog= new Dialog(c, R.style.CustDialog);
//inflate a layout
LayoutInflater inflater = this.getLayoutInflater();
View root = inflater.inflate(R.layout.custom_alert, null);
// set the layout for the Dialog
dialog.setContentView(root);
If you read the Dialog Docs it gives the different methods you can use.
Note the docs say
The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:
So for this reason you may want to look at AlertDialog which you can find plenty of examples of on SO or the Google.
This answer gives an example of creating a custom Dialog class.
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();