Set color of alertdialog - android

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>

Related

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

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);

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>

Android N: ProgressDialog and AlertDialog display issue

Problem:
I am using AlertDialog and ProgressDialog at multiple places in the app. They are displaying fine with Android versions below Android N Preview. However when I tested them with Android N Preview (NDP3 on Nexus 5X), the translucent background doesn't seem to cover the whole screen.
Translucent background of dialog not covering the whole screen in Android N:
This is how I'm creating the dialog in the activity:
public static void showDialog(final Activity activity, final String title, final String message, final String positiveButtonText, final String negativeButtonText, final DialogInterface.OnClickListener positiveButtonListener, final DialogInterface.OnClickListener negativeButtonListener, final Boolean cancellable) {
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
alert.setTitle(title);
alert.setMessage(message);
alert.setPositiveButton(positiveButtonText != null ? positiveButtonText : activity.getString(R.string.button_text_ok), positiveButtonListener);
alert.setNegativeButton(negativeButtonText != null ? negativeButtonText : activity.getString(R.string.button_text_cancel), negativeButtonListener);
if (cancellable != null) {
alert.setCancelable(cancellable);
} else {
alert.setCancelable(false);
}
//creating an alert dialog from our builder.
alertDialog = alert.create();
if (!activity.isFinishing() && alertDialog != null) {
alertDialog.show();
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(activity, android.R.color.black));
alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(activity, android.R.color.black));
}
}
});
}
Here is the activity theme:
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
<!-- Base application theme. Include the items here which are common to all version. -->
<style name="AppTheme.Base" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!--Customize Action Bar-->
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowActionBar">true</item>
<item name="actionBarStyle">#style/Theme.TradeRev.ActionBar</item>
<item name="android:actionBarStyle">#style/Theme.TradeRev.ActionBar</item>
<item name="colorPrimary">#color/light_green</item>
<item name="colorPrimaryDark">#color/green_title_bar</item>
<item name="colorAccent">#android:color/white</item>
<item name="actionMenuTextColor">#android:color/white</item>
<item name="android:actionMenuTextColor">#android:color/white</item>
<item name="android:homeAsUpIndicator">#drawable/button_back_up_navigation</item>
<!-- Title Text Color -->
<item name="android:textViewStyle">#style/AppTheme.Widget.TextView</item>
</style>
Question:
Is this a issue with Android N or has something changed in the API's which we need to incorporate for Android N and above?
This is a known issue in N DP. See https://code.google.com/p/android/issues/detail?id=205765
It is a platform issue. You do not need to work around this in your app.

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);

How to change the theme color of default fragment dialog

I have shown a dialog fragment. I have done it, but I want to change background (Theme) color grey to white.
My dialog code:
public class TestDialog extends DialogFragment {
public static TestDialog newInstance() {
return new TestDialog();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return container;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dailog_fragment, null))
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
TestDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
use this
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
and then make the custom MyDialog style.
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#color/orange_transparent</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:gravity">center</item>
</style>
</resources>
note that it should be in style.xml.
In your manifest file, the activity definition of the dialog activity, add the android:theme attribute as follows.
<activity
android:name=".YourActivityName"
android:theme="#styles/style"
/>
I Got The Solution:
I am using below style theme for set the white background color.
<style name="MyFragment">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:colorBackground">#android:color/white</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
We can also use Holo Theme
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileEditActionBar.this, AlertDialog.THEME_HOLO_LIGHT);

Categories

Resources