Alert dialog theme does not inherit `windowBackground` from activity theme - android

In my app I have this theme in the styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#000000</item>
<item name="colorAccent">#color/neon_green</item>
<item name="android:windowBackground">#color/black_17</item>
</style>
but the attribute windowBackground does not cause the dialog background to change. The dialog background color is still white. The neon_green highlight color does appear for the dialog text though.
Only the background color that does not change.
AlertDialog.Builder builder =
new AlertDialog.Builder(new ContextThemeWrapper(act, R.style.AppTheme));
builder.setMessage("text")
.setPositiveButton("pos ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton("neg", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.create().show();

I just added this to my styles
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColor">#color/white</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="android:background">#color/black_17</item>
</style>
then I passed the styles to the builder
AlertDialog.Builder builder =
new AlertDialog.Builder(act,
R.style.AppCompatAlertDialogStyle);

Related

Unable to show button in material dialog

I am using material design theme in app. I am trying to show Material Design dialog in app, but in dialog positive and negative buttons are not showing. Below is my code:
new MaterialAlertDialogBuilder(getActivity())
.setMessage("Do you want to close this application ?")
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext,"Hello",Toast.LENGTH_SHORT).show();
}
}).show();
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Caketale" parent="Theme.MaterialComponents.Light">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryVariant">#color/primaryDark</item>
<item name="colorOnPrimary">#color/white</item>
<item name="colorSecondary">#color/accent</item>
<item name="colorSecondaryVariant">#color/accent</item>
<!-- Secondary brand color. -->
<!-- <item name="colorSecondary">#color/teal_200</item> -->
<!-- <item name="colorSecondaryVariant">#color/teal_700</item> -->
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
How can I resolve this issue?
To use it with the design library, use this style.
<style name="AlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorAccent</item>
</style>
Then you use it like this.
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme);
builder.setTitle("AppCompatDialog");
builder.setMessage("Lorem ipsum dolor...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();

How to change the List view text color the in Alert Dialog box using style in android?

I have used this line to add style to dialog box
AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle)
I am able to style the Title name however the text color in the list remains black color only.
public class NotificationModeDialog extends DialogFragment {
public ArrayList<String> list = new ArrayList<>(); //initialized the array of list
public int i;
final String notificationListItems[]={
"CallAlert","address","name","Message","id","contact","ymail","Gmail"};//the data to be displayed in dialog box
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final boolean[] notificationSelected={false,false,false,false,false,false,false,false};//initializing the starting state
//initializing the alertDialog here AlertDialogStyle is added for styling
AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle)
.setTitle("Notification Mode")
.setMultiChoiceItems(notificationListItems, notificationSelected ,new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int j, boolean isChecked) {
if(isChecked)
{
list.add(notificationListItems[j]); //add items
}
else if(list.contains(notificationListItems[j]))
{
list.remove(notificationListItems[j]);//remove items
}
}
})
//when person click on "ok" this statement is called
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int j) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel(); //cancel the changes and turn of the dialog box
}
}).create();//this will create the dialog box
dialog.show(); //show the dialog box
return dialog; //return the dialog box to layout
}
}
This is styles.xml code.
<!-- Styling the alert BOX -->
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<!-- Used for the buttons -->
<item name="colorAccent">#e4e4e4</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#64f24c49</item>
</style>
Create a alert dialog style in styles.xml:
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColor">#color/white</item>
</style>
Then add the style to you current app theme style:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Alert Dialog Style -->
<item name="android:alertDialogStyle">#style/MyAlertDialogStyle</item>
</style>

Set color of alertdialog

I display an alertdialog in my app but the left and right side of the title shows a different color. I have set my own background color in thems.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ACCAppTheme" parent="android:Theme.Holo">
<!-- Change default text colour from dark grey to black -->
<!-- <item name="android:windowBackground">#drawable/ic_logo</item> -->
<item name="android:background">#140051</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textColorHint">#c0c0c0</item>
<item name="android:actionBarStyle">#style/AppTheme.ActionBar</item>
</style>
<!-- <style name="OverFlow" parent="#android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:textSize">18sp</item>
</style> -->
<style name="AppTheme.ActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#140051</item>
<item name="android:height">50dp</item>
<item name="android:titleTextStyle">#style/AppTheme.ActionBar.Text</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:showDividers">beginning</item>
</style>
<style name="AppTheme.ActionBar.Text" parent="#android:style/TextAppearance">
<item name="android:textAllCaps">false</item>
<item name="android:textColor">#android:color/white</item>
</style>
</resources>
but what appears is like this
here's the code that creates the alert dialog
public class AppMessages {
public static final int MESSAGE_INFO = 1;
public static final int MESSAGE_CRITICAL = 2;
public interface AlertButtonClickListener {
public abstract void onButtonClicked(boolean value);
}
public static void showMessage(Context context,
String message,
String title,
int messageType,
final AlertButtonClickListener target) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message)
.setTitle(title)
.setIcon(R.drawable.ic_dialog_information)
.setCancelable(false)
.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (target != null) {
target.onButtonClicked(true);
}
}
});
switch (messageType) {
case AppMessages.MESSAGE_CRITICAL:
builder.setIcon(R.drawable.ic_dialog_exclamation);
break;
case AppMessages.MESSAGE_INFO:
builder.setIcon(R.drawable.ic_dialog_information);
break;
}
builder.show();
}
public static void yesNo(final Context mContext,
final String title,
final String msg,
final String positiveBtnCaption,
final String negativeBtnCaption,
final boolean isCancelable,
final AlertButtonClickListener target) {
((Activity) mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(title)
.setMessage(msg)
.setIcon(R.drawable.ic_dialog_question)
.setCancelable(false)
.setPositiveButton(positiveBtnCaption,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
target.onButtonClicked(true);
}
})
.setNegativeButton(negativeBtnCaption,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int id) {
target.onButtonClicked(false);
}
});
AlertDialog alert = builder.create();
alert.setCancelable(isCancelable);
alert.show();
if (isCancelable) {
alert.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface arg0) {
target.onButtonClicked(false);
}
});
}
}
});
}
}
here's how I call the it
AppMessages.showMessage(thisActivity,
"New update found. Click Ok to download it now.",
"New Update Found",
1,
new AppMessages.AlertButtonClickListener() {
#Override
public void onButtonClicked(boolean value) {
APKDownloader.downloadAPK(thisActivity);
}
});
Try this,
Customize AlertDialog Theme
Create a new Android XML resource file under res/values/
You'll want to create a new style that inherits from the default alertdialog theme:
<style name="CustomDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:bottomBright">#color/white</item>
<item name="android:bottomDark">#color/white</item>
<item name="android:bottomMedium">#color/white</item>
<item name="android:centerBright">#color/white</item>
<item name="android:centerDark">#color/white</item>
<item name="android:centerMedium">#color/white</item>
<item name="android:fullBright">#color/orange</item>
<item name="android:fullDark">#color/orange</item>
<item name="android:topBright">#color/blue</item>
<item name="android:topDark">#color/blue</item>
</style>
You can specify either colors or drawable for each section of the AlertDialog.
An AlertDialog will build it's display by combining 3 drawables/colors (top, center, bottom) or a single drawable/color (full).
In a theme of your own override the android:alertDialogStyle style (you can do this within the same XML file):
#style/CustomDialogTheme
Override your application's theme in the AndroidManifest within the application tag:
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/MyTheme">
Now that you've defined your application's theme, any attributes overridden in your theme will resonate throughout your app.
For example if you also wanted to change the color of your application's title bar:
You would first define a new style that inherits from the default attribute
#color/blue
and just add the new overridden item to your theme:
<style name="MyTheme">
<item name="android:windowTitleBackgroundStyle">#style/MyBackground</item>
<item name="android:alertDialogStyle">#style/CustomDialogTheme</item>
</style>

Why is My Android Alert dialog BLACK?

Can you help my figure out why is my alert dialog BLACK?!
Recently i changed my app theme to support material design, but my alert dialog got black!
Here is my create dialog Code:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(TestActivity.this);
alertDialog.setCancelable(true);
alertDialog.setTitle("sample");
alertDialog.setItems(new String[] { "a", "b" }, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertDialog.show();
and its my main style and dialog style
<style name="MaterialTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:alertDialogStyle">#style/AppDialog</item>
<item name="android:alertDialogTheme">#style/AppDialog</item>
<item name="android:textColor">#color/black</item>
<item name="android:textColorPrimary">#color/black</item>
<item name="android:dialogTheme">#style/AppDialog</item>
<item name="colorPrimaryDark">#color/myPrimaryDarkColor</item>
<item name="android:textColorHint">#color/gray_1</item>
<item name="colorAccent">#color/myAccentColor</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="android:windowBackground">#color/black</item>
</style>
<style name="AppDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#FFC107</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#4CAF50</item>
</style>
You don't set theme for AlertDialog, for using theme, change code to:
ContextThemeWrapper ctw = new ContextThemeWrapper(TestActivity.this, R.style.AppDialog);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(ctw);
you must use a light theme with your alertdialog builder such as the THEME_HOLO_LIGHT.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this,AlertDialog.THEME_HOLO_LIGHT);

Opening an Activity that contains only a Dialog

I'm trying to show a Dialog that it's inside of an Activity, and want to show this Activity without background, I only want to show the Dialog.
I'm doing this Intent
Intent intent = new Intent(getApplicationContext(), NotifyNetworkWifi.class);
startActivity(intent);
This is the Activity it's a simple Alert Dialog
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Main2Activity.this);
alertDialogBuilder.setTitle(this.getTitle() + " decision");
alertDialogBuilder.setMessage("Are you sure?");
// set positive button: Yes message
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
// set negative button: No message
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel the alert box and put a Toast to the user
dialog.cancel();
Toast.makeText(getApplicationContext(), "You chose a negative answer",
Toast.LENGTH_LONG).show();
}
});
// set neutral button: Exit the app message
alertDialogBuilder.setNeutralButton("Exit the app", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// exit the app and go to the HOME
Main2Activity.this.finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show alert
alertDialog.show();
I didn't set a content view, I just delete it and added this line
setTheme(R.style.TUPUTAMADRE);
This Style is
<style name="TUPUTAMADRE" parent="AppTheme">
<item name="android:windowFrame">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:windowBackground">#android:color/transparent</item>
<!-- Customize your theme here. -->
</style>
I don't know why is showing to me a black screen behind the Dialog, I just want to show this dialog....
What I'm doing wrong?
LOGCAT ERROR
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:122)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
at com.tupits.dialogwithnetworkavaliable.Main2Activity.onCreate(Main2Activity.java:15)
Use Android theme in your manifest for activity.
<resources>
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
try this code as your style

Categories

Resources