'how to show alert dialog on top of any activity in an application android
I want to display an alert dialog. And this alert dialog is created in one activity. But Problem is i want to display this dialog on top of any activity in an application when i get some response from server
Solution 1:[1]
Yes i got the solution....
first i created a class for displaying alert dialog. upon receiving a response from server calling a method to display alert dialog by passing application context
package io.omoto.omotokairaliapp.Utls;
import android.content.Context;
import android.view.WindowManager;
import com.gitonway.lee.niftymodaldialogeffects.lib.NiftyDialogBuilder;
import io.omoto.omotokairaliapp.Constants.Constants;
/**
* Created by ${venkie} on ${28/1/16}.
*/
public class DisplayRegisteredMessage {
Context context;
private NiftyDialogBuilder dialogBuilder;
public DisplayRegisteredMessage(Context context) {
this.context = context;
}
public void displayMessage() {
if (Constants.FLAG == 1) {
Constants.FLAG = 0;
dialogBuilder = NiftyDialogBuilder.getInstance(context);
dialogBuilder
.withTitle("Response is already registered")
.withMessage("For this Customer we have already taken response Sorry!!");
dialogBuilder.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialogBuilder.show();
} else {
}
}
}
The above code displays alert dialog
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
Log.e("response page", response.toString());
try {
if (response.getString(Constants.STATUS).equalsIgnoreCase("SUCCESS"))
p7.deleterecord(p7b.getFlowid());
p7.deleteRows();
Constants.FLAG = 1;
DisplayRegisteredMessage displayRegisteredMessage = new DisplayRegisteredMessage(mContext);
displayRegisteredMessage.displayMessage();
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("Page 6", p7b.getFlowid());
}
Solution 2:[2]
Create alert dialog in other class (not activity) as shown below code in static method & call this method using UDF.showAlertDialog(activity) where you want to show dialog.
public class UDF {
public static void showAlertDialog(
String warning,
DialogInterface.OnClickListener positiveClickListener,
DialogInterface.OnClickListener negativeClickListener,
Context context) {
AlertDialog.Builder alertdialog = new AlertDialog.Builder(context);
alertdialog.setMessage(warning);
alertdialog.setPositiveButton("Ok", positiveClickListener);
alertdialog.setNegativeButton("Cancel", negativeClickListener);
alertdialog.show();
}
}
Edit : set this
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
and add permission in menifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
& pass getApplicationContext()
while creating dialog instade of activity or context
Solution 3:[3]
Here is a workaround that worked for me:
Suppose you have 4 activities in total:
- MainActivity
- OtherActivity1
- OtherActivity2
- OtherActivity3
I had the code for the alert dialog in the MainActivity and I wanted the alert dialog to display in all activities. I did the following:
- Make other activities extend the main activity.
public class OtherActivity1 extends MainActivity{ } public class OtherActivity2 extends MainActivity{ } public class OtherActivity3 extends MainActivity{ }
In your Main activity, save the context in a variable.
public class MainActivity extends AppCompatActivity{
public static Context contextMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
contextMain = MainActivity.this;
}
}
And wherever you are trying to call your alert dialog in the MainActivity just pass contextMain as the context.
final AlertDialog.Builder dialog = new AlertDialog.Builder(contextMain)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | JonasCz |
Solution 2 | |
Solution 3 | Suraj Rao |