Show Dialog Alert From A Non-activity Class In Android
Solution 1:
Just add this before your alertDialog.show();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
or try following if above didn't work:
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
and use this permission:
<uses-permissionandroid:name="android.permission.SYSTEM_ALERT_WINDOW" />
Solution 2:
The problem is 'You can show AlertDialogs from Activity only
'. This is not an issue of context.
Although this is not a good idea to show dialog from receiver (better is to use Notification), But if you want to do so you can create an Activity as dialog and show
Solution 3:
If you always want to get the current activity from anywhere in the app you can register an ActivityLifecycleCallback on your Application instance.
Here's an untested implementation that might get you closer.
publicclassTestAppextendsApplication {
privateWeakReference<Activity> mActivity = null;
@OverridepublicvoidonCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(newActivityLifecycleCallbacks() {
@OverridepublicvoidonActivityCreated(Activity activity, Bundle savedInstanceState) {
mActivity = newWeakReference<Activity>(activity);
}
@OverridepublicvoidonActivityDestroyed(Activity activity) {
mActivity.clear();
}
/** Unused implementation **/@OverridepublicvoidonActivityStarted(Activity activity) {}
@OverridepublicvoidonActivityResumed(Activity activity) {}
@OverridepublicvoidonActivityPaused(Activity activity) {}
@OverridepublicvoidonActivityStopped(Activity activity) {}
@OverridepublicvoidonActivitySaveInstanceState(Activity activity, Bundle outState) {}
});
}
publicActivitygetCurrentActivity() {
return mActivity.get();
}
}
Then to use this throughout your app you would do some call like this ...
Activityactivity= ((TestApp)getApplicationContext()).getCurrentActivity();
The advantages are you can always keep track of your current activity, however its a little too overkill for just handling Dialogs from within the Activity.
Solution 4:
call this method in activity class
publicstaticvoidshowAlert(Activity activity, String message) {
TextViewtitle=newTextView(activity);
title.setText("Title");
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
AlertDialog.Builderbuilder=newAlertDialog.Builder(activity);
// builder.setTitle("Title");
builder.setCustomTitle(title);
// builder.setIcon(R.drawable.alert_36);
builder.setMessage(message);
builder.setCancelable(false);
builder.setNegativeButton("OK", newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialogalert= builder.create();
alert.show();
}
Solution 5:
Here's what I made and use:
myDialog.java:
import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
publicclassmyDialog {
private Activity mActivity;
myDialog(Activity a) {
this.mActivity = a;
}
@SuppressWarnings("InflateParams")publicvoidbuild(String title, String msg) {
LayoutInflaterinflater= LayoutInflater.from(mActivity);
ViewsubView= inflater.inflate(R.layout.dialog_box_text, null);
finalTextViewmessage= subView.findViewById(R.id.message);
message.setText(msg);
AlertDialog.Builderbuilder=newAlertDialog.Builder(mActivity);
builder.setTitle(title);
builder.setView(subView);
builder.setPositiveButton("OK", newDialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialogalert= builder.create();
alert.show();
}
}
dialog_box_text.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:weightSum="1"android:orientation="horizontal"><TextViewandroid:id="@+id/message"android:layout_width="match_parent"android:layout_height="wrap_content"android:text=" "android:maxLines="1"android:textColor="@color/colorBlack" /></LinearLayout>
Example code:
publicclassMainActivityextendsAppCompatActivity {
private myDialog md;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
md = newmyDialog(this);
...
md.build("Title", "Message");
Post a Comment for "Show Dialog Alert From A Non-activity Class In Android"