I use appcompat v22.1.0 in my App and use Toolbar. Everything was fine when I use Theme.AppCompat.Light.NoActionBar. When I start implement AlertDialog
getSupportActionBar().getThemedContext()
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(getSupportActionBar().getThemedContext(), android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(getSupportActionBar().getThemedContext());
}
builder.setTitle("Alert Dialog")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
In kotlin this worked to me:
val dialog = AlertDialog.Builder(this)
Basically your Activity is using Toolbar (which replaces Action Bar) so you need to use style for the Activity that has no Action Bar like Theme.AppCompat.Light.NoActionBar.
If you have your own style for dialog then you need to inherit the proper AppCompat theme.
<style name="myDialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
...
</style>
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));
The probem is because of Context which You are passing to build the Alert Dialog.Don't Use getApplicationContext().Try using your Activity context.Use AlertDialog.Builder builder = new AlertDialog.Builder(MainActiviy.this);
This work for me... after read several answer was...
Change my import like this:
import android.app.AlertDialog;
instead of
import android.support.v7.app.AlertDialog;
this give error Unable to add window -- token null is not for an application... so I change the context of the builder from
AlertDialog.Builder builder = new
AlertDialog.Builder(getAplicationContext()); to
AlertDialog.Builder builder = new AlertDialog.Builder(*MainActivity.this*);
You need to pass your Activity's context instead to pass your Context. Try with "this" instead "context". This work for me