I have a method in my class i want to test which shows an alert dialog .
When i call this method from the test class it is executed but the dialog is not shown?
The same happens when i call a method, that shows some toast or other popup dialog
my test class extends ActivityInstrumentationTestCase2.
public void showSaveName(String name){
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_menu_save)
.setTitle(R.string.savePopupLabel)
.setMessage(R.string.savePopupMessage)
.setPositiveButton(R.string.save_yes, new alter(name))
.setNegativeButton(R.string.save_no, null)
.show();
}
when i call this from my test class
getActivity().showSaveName(name);
the dialog is not shown?
Can anyone help me to figure out why it is happening ?or if i am doing something wrong?
That's normal.
Your test classes are not meant to make anything show on the device. You are supposed to programmatically make sure that the dialog appears.
In your test class, once you display the dialog, keep the instance of the Dialg box and then do
assertTrue(yourDialogInstance.isShown());
And if your dialog has not appeared, your test will fail.
That should do it.
Can you try:
public void showSaveName(String name) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(android.R.drawable.ic_menu_save)
.setTitle(R.string.savePopupLabel)
.setMessage(R.string.savePopupMessage)
.setPositiveButton(R.string.save_yes, new alter(name))
.setNegativeButton(R.string.save_no, null);
AlertDialog dialog = builder.create();
dialog.show();
}
Related
I'm very new to Android, and have a basic question. I need at certain points to display a user notification in a dialog box, which they can simply acknowledge with the OK button.
I'm using:
myActivity.runOnUiThread(new Runnable() {
public void run() {
AlertDialog alertDialog = new AlertDialog.Builder(myContext).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("My message");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
});
This works well in the Main program, but within a called method it needs the Activity and the Context from the main program. Can anybody tell me how to pass these? getApplicationContext() seems to be acceptable, but I can't figure out how to pass the Activity.
Better still of course would be to get the parent Context and Activity within the method, but I can't get that to work either.
I'd be grateful for any help.
-update 10/07/21
Rahul has given me the solution to the problem I posed: how to pass in the Activity and Context.
The problem is that the dialog still doesn't show.
I found a variation online as follows:
AlertDialog.Builder builder = new AlertDialog.Builder(myContext);
builder.setTitle("Alert")
.setMessage("My message")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
but this doesn't work either.
I'm puzzled that such a common and simple task needs so much code. In the desktop languages I'm used to it can be done in a single line.
So my titled question stands, but can anyone see where the code is faulty?
Many thanks
You can either pass activity to the class when initializing the object or you can pass activity when calling the function.
Case 1 (Recommended)
Pass Activity when calling the function:
MyObj myObj = new MyObj();
myObj.showDialog(myValue, ActivityName.this);
Where function will look like this:
public void showDialog(int myValue, Activity activity){
...
}
Then you can use this activity instance inside the method.
Case 2
Pass Activity when initializing the object:
MyObj myObj = new MyObj(ActivityName.this);
Where Class will look like this:
class MyObj{
private Activity thisActivity;
public MyObj(Activity activity){
thisActivity = Activity;
}
...
}
Then you can use this activity instance.
When you have activity object available you can replace context object with it.
In my activity, I am loading some eight fragments, from the 8th fragment showing some alert dialog.After that session getting expired, so redirected them to the first fragment without user interaction.During that time alert dialog not getting closed which created from 8 fragments.
In the onPause() of your 8th Fragment you should hide the AlertDialog.
class XYZ extends Fragment{
//Keep the reference to AlertDialog as a member variable in your Fragment
AlertDialog mDialog;
//other member declarations ...
#Override
public void onPause() {
// check if dialog is not null and is already showing
if(mDialog !=null && mDialog.isHowing())
mDialog.hide();
super.onPause();
}
Note : If the viewpager removes 8th fragment from memory and some action that you perform in your dialog references your 8th fragment then it would cause null pointer exception.
If you using alert dialog like that:
AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
you need to keep the dialog instance which returns from the show method and call dismiss from that instance.
AlertDialog dialog = builder.show();
//Use this anywhere to close
dialog.dismiss();
From activity 1, I open activity 2. Now when I am in the screen of activivty 2, I receive an event of activity 1 and onrecieve of that event i want to display a dialog. How can I do that??
While I am in activity 2 I dont see the dialog, but when I get back to activity 1, i see the dialog.
I want to see the dialog even when i am in screen of activity 2.
I looked into this link as well, but wasnt much helpful
Showing dialog on top of another running activity (Android)?
on receiving the reqd event, i call this method.
First create separate dialog in Activity 2 and do below mentioned
Try to send broadcast when data related to activity 1 is received.
Create a Receiver class which will listen the broadcast
Now you have to use of Observer Design pattern i.e. when ever there is some event in Receiver class it will inform Activity B that data is arrived and show the appropriate dialog.
Please share the result if it not succeeded.
in your first activity (splashScreen ) write this function to create alert box
public static void MyAlertBox(String title, String mymessage, Context context)
{
new AlertDialog.Builder(context)
.setMessage(mymessage)
.setTitle(title)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
.
}
})
.show();
}
Declare global veriable :
public static Activity currentActivity= null;
in Every activity :
onResume() write "currentActivity=this ;"
Where you want to display alert just write :(activity 1)
SplashScreen.MyAlertBox("Alert",
"Alert box from activity 1",currentActivity );
hope it will work for you !!
It is possible, create a class, let's say a singleton, like this:
Class DialogDisplayer () {
singleton implementation...
weakReference context: Context
fun showDialog(whatever data if needed) {
dialogBuilder implementation ...
which uses the context from above, the rest you have
}
}
on onResume() of each activity use DialogDisplayer.context = this
and finally, show the dialog from using class, in your first activity
like this: DialogDisplayer.showDialog(whatever data if needed)
This way the class will always show the dialog with context of the current active activity. I didn't write all the correct syntax, just the idea. WeakReference to allow this class to be GC collected. Maybe there are better ideas but this should work.
Im writing an Android application in which a user selection triggers a custom Dialog, from which a selection may trigger a second Dialog.
When showing the initial Dialog from the Activity class, I'm setting an onDismissListener on it to pull out user selections which works fine other in cases where the 2nd Dialog is not triggered. The issue that I'm having is that I can't figure out how to have the first one Dialog remain open until the 2nd one is dismissed, so that the information from both is sent back to the Activity class.
Hopefully some code will make this a little more clear:
MainActivity class where I am launching the initial CustomDialog:
customDialog = new CustomDialog(this);
customDialog.show();
customDialog.setOnDismissListener(new OnDismissListener(){
public void onDismiss(DialogInterface di){
slection = customDialog.getselection();
updateUI(); //updates a listview with the results
}
});
Within the CustumDialog class where I am launching the SecondDialog on top of it:
if(specify){
SecondDialog secondDialog = new SecondDialog(context);
secondDialog.show();
secondDialog.setOnDismissListener( new OnDissmissListener(){
public void onDismiss(DialogInterface di){
// this is where I want to call the CustomDialog's dismiss() method
// so that they both close at the same time and the data from here
// can be sent back to the MainActiivty through the CustomDialog's
// onDismissListener
}
});
}
dismiss();
So, to reiterate: I'm trying to prevent the CustomDialog's dismiss() method to be called until the SecondDialog is also dismissed. Is there a way that I can call it from the SecondDialog's OnDismissListener?
You should create customDialog as an instance level variable. You then it will be accessible with onDismiss(...) of second dialog. There you can call customDialog.dismiss();
// Instance level variable
private Dialog customDialog = null;
Instanciate your customDialog, then create second dialog from within your customDialog. Your Second dialog's code would look like this.
if(specify){
SecondDialog secondDialog = new SecondDialog(context);
secondDialog.show();
secondDialog.setOnDismissListener( new OnDissmissListener(){
public void onDismiss(DialogInterface di){
// customDialog is accessible as it is declared as instance level variable
MyClassName.this.customDialog.dismiss();
}
});
}
dismiss();
I prefer to save the data in 1st dialog before going to send one and when dismiss the 2nd dialog, open the 1st dialog again with saved data .. i used this way in my developing and its effective ..
So a dialog is opened every time a text is received. I want it to not open one if there is one already open. I was trying to check if one was open by using isShowing() but I keep getting the method isShowing() is undefinded for the type AlertDialog.Builder. Here is the section of bad code. Any help would be so sweet right about now.
public class PopUpReply extends Activity{
AlertDialog.Builder alertbox;
AlertDialog.Builder alert;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// prepare the alert box
alertbox.isShowing();
alertbox = new AlertDialog.Builder(this);
There is no isShowing() method on the AlertDialog.Builder class. There is one on the Dialog class though.
AlertDialog.Builder
Dialog
An AlertDialog.Builder is used to create an AlertDialog. Once you have an instance of an AlertDialog, you can determine whether or not it is still showing by then calling isShowing() on it.