Android Testing: Dialog. Check it isShowing() - java

Here is my method, it works fine and shows the Dialog.
public void showDialog(){
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.mylayout);
dialog.show();
}
I have a test project and I would like to test that the Dialog is showing up. I would like to apply the .isShowing() method. Something like this...
assertTrue(dialog.isShowing());
But I don't know how to get to the dialog variable within my test.
I am not using Robotium (this isn't an option for me).
I'm currently using the ActivityUnitTestCase to test with. If any more information is required please don't hesitate to ask.
EDIT
I have attempted to use the answer below by making the Dialog public
public Dialog getDiag(){
return dialog;
}
Using this answer: I have a new problem when I run showDialog() in the test, it breaks when it hits: dialog.show();
android.view.WindowManager$BadTokenException: * Unable to add window -- token null

Declare Dialog outside showDialog function and then implement a method which returns this Dialog instance.
public Dialog getDiag(){
return dialog;
}
and then do something like this
assertTrue(new YourClassName().getDialog().isShowing());

Related

Activity which implements multiple dialog listeners?

I’m getting really confused with dialog boxes in Android and need some advice.
Everything was initially going well. I had numerous dialogs being created within MainActivity opened via a Navi Drawer. The dialogs where created very simply using code like this:
private void exportDialog() {
LayoutInflater inflater = this.getLayoutInflater();
final View formElementsView = inflater.inflate(R.layout.export_data, null, false);
AlertDialog msgBox = new AlertDialog.Builder(this)
.setView(formElementsView).setTitle("Export Responses")
.setIcon(android.R.drawable.ic_menu_share)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Code...
}
})
.create();
msgBox.show();
}
But I started to notice problems when the device resumed from lock screen. Switching/pausing/resuming worked fine, but the locked screen seemed to kill the activity context and when the app resumed the dialogs became graphically corrupt and I got the “Activity has leaked window” error message in the logs.
So, I started again. I moved all the dialogs into DialogFragment classes which seems to be the ‘proper’ way to do it. This removes the error and graphical glitches. Great!
But I can’t work out how to add more than one dialog listener to MainActivity. So I’ve got:
public class MainActivity extends Activity implements LogInDialog.NoticeDialogListener{
but can I add more?
Until I work out how to do this, I’ve altered many of the MainActivity methods and moved them into the relevant dialog classes, which works fine but seems a rather linear approach. It would be nice if MainActivity could act upon each dialog response.
As you can guess, I’m not a professional developer so I’m getting rather lost!
UPDATE:
OK, it seems the original problem of graphic glitches and "leaked window" messages was because the dialog boxes were not being dismissed correctly. Adding the following seems to have greatly improved matters:
#Override
public void onDestroy() {
super.onDestroy();
if (DialogBox1!=null){
DialogBox1.dismiss();
}
DialogBox1= null;
if (DialogBox2!=null){
DialogBox2.dismiss();
}
DialogBox2= null;
if (DialogBox3!=null){
DialogBox3.dismiss();
}
DialogBox3= null;
...
use inner classes that implements LogInDialog.NoticeDialogListener

How to close a Java SWT window with a button?

As a newbie to Java, and many years of iOS and .NET experience, I found this to be massively confusing. What I want I thought would be very simple - I want a dialog (called from a main window) with OK and Cancel buttons. When you click OK, it does something, then dismisses the dialog. When you click Cancel, it just dismisses the dialog.
However, doing this using the SWT shell Dialog class is not obvious. How do you get a button to dismiss the dialog, and return execution back to the main Window?
Use Shell.close() rather than dispose() - so shlCheckOut.close().
Shell.close sends the SWT.Close event and then calls dispose.
With some trial-and-error and a lot of fruitless searching, I found in your button code, you have to call the .dispose() method of the dialog's shell variable. For example, my dialog is CheckOutDialog, thus I named the shell variable shlCheckOut. In the createContents() method, I put the button code as such:
...
Button btnCancel = new Button(shlCheckOut, SWT.NONE);
btnCancel.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
shlCheckOut.dispose();
}
}
}

Issue to call setMessage() on ProgressDialog

I'm having an issue creating a ProgressDialog in my onCreateDialog() method.
The code is as follows:
Dialog dialog;
switch(id){
case CONNECTING:
dialog = new ProgressDialog(this);
dialog.setMessage("Connecting").setTitle("");
return dialog;
Eclipse throws me an error setMessage wouldn't be a valid Method of the type ProgressDialog, though I expect it to be there since the documentation for API8 (which I use) says so.
AFAIK the instantiation should be possible since ProgressDialog ihnerits from Dialog right?
Can someone help me at this? It's really weird.
You need to change your code to:
Dialog dialog;
switch(id){
case CONNECTING:
dialog = new ProgressDialog(this);
((ProgressDialog)dialog).setMessage("Connecting");
dialog.setTitle("");
return dialog;
Alliteratively, you can change dialog to type ProgresssDialog if you are always returning a ProgresssDialog, but I doubt it.
The issue is that Dialog doesn't have a setMessage method. Which is the type of the variable dialog.
Edit:
This line:
dialog.setMessage("Connecting").setTitle("");
Also looks wrong since setMessage() returns void.

Android activity restart

Hey guys, i am making an android application where i want to show a dialog box about legal agreement everytime the application starts, i have a public method showalert(<>); which shows an alertdialog by building a dialog with alertbuilder. I added a call to showalert() method on the onCreate() method of the main activity to show it, but whenever the user rotates the screen, he gets the dialog everytime. The activity restarts itself when the phone is rotated. I tried adding android:configChanges="keyboardHidden|orientation" to my manifest but that doesnt help on this case. Also can i know how to register a new application class on manifest file. I am trying to create an application class and put the code to show dialog on the new class's oncreate method. But i am not being able to load the class when the app starts.
I also checked Activity restart on rotation Android but i dont seem to get a thing. I am pretty much a newbie to android programming, could someone simplify that for me?
Any help would be appreciated. :)
you could maybe look at the onRetainNonConfigurationInstance() activity method, which is called just before destroying and re-creating the activity on screen orientation change.
it allows you to retain an object that could for instance contain a test variable to know if your legal thing was already shown or not.. example :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String test = (String) getLastNonConfigurationInstance();
if (!("textAlreadyShown").equals(test)) {
//here : show your dialog
}
}
#Override
public String onRetainNonConfigurationInstance() {
return "textAlreadyShown";
}
Set the main activity to an activity that just shows the legal notice, when it is accepted/cleared, show a second activity ( which is currently the main activity )?

Showing Dialog after another Dialog: HANGS

I have a really weird problem, which I am unable to debug so far...
Thing is...my app needs to download something to work. So in the beginning of the onCreate() method, I check if that something is already downloaded. If not, I pop a dialog up asking the user to download it.
if (!isInstalled) {
showDialog(DIALOG_INSTALL);
} else {
start();
}
Where start() method performs some other action. Now, that showDialog calls this:
builder = new AlertDialog.Builder(MyApp.this);
builder.setMessage("Would you like to install...")
.setCancelable(false)
.setPositiveButton("Install", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
aManager.install(MyApp.this);
}
});
dialog = builder.create();
return dialog;
My dialog is shown and I am clicking, so aManager.install() is called. I am passing the context because that aManager.install() pops up a ProgressDialog to show downloading progress and spawns a new thread in which everything is downloaded. So obviously before creating my dialog I make a Handler to receive the response from that aManager.install(). And the response MAY vary, because for example the internet connection isn't available (Exception raised and catched and listener called with different code).
Now, when that happens (Exception) I would like to call another dialog saying "something went wrong, would you like to retry?"...so another call to showDialog(DIALOG_REINSTALL) (this time with another code).
Thing is...the showDialog() gets called (I can verify this by logging) but the dialogs doesn't show up. Instead my application JUST HANGS!?!?!?
Does someone have a clue why it's doing this???? No exception raised, absolutely nothing from logcat, I can't tell WHERE it's hanging...just see that the method is called and the dialog should be displayed...
Thank you very much!!
Looks like you have a deadlock. I would put the download code on the separate thread e.g. use AsyncTask. In task.onPreExecute() you can dismiss 1st dialog and pop-up your progress dialog which you update by overwriting task.onProgressUpdate()
Use .show() instead of .create().

Categories

Resources