I was wondering if someone could help me out. I am trying to create a custom AlertDialog. In order to do this, I added the following line of code in styles.xml
<resources>
<style name="CustomAlertDialog" parent="android:Theme.Dialog.Alert">
<item name="android:windowBackground">#drawable/color_panel_background</item>
</style>
</resources>
color_panel_background.9.png is located in drawable folder. This is also available in Android SDK res folder.
The following is the main activity.
package com.customdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class CustomDialog extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTheme(R.style.CustomAlertDialog);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("HELLO!");
builder .setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//dialog.cancel();
}
});
AlertDialog alertdialog = builder.create();
alertdialog.show();
}
}
In order to apply the theme to an AlertDialog, I had to set the theme to the current context.
However, I just can't seem to get the app to show customized AlertDialog. Can anyone help me out with this?
In Dialog.java (Android src) a ContextThemeWrapper is used. So you could copy the idea and do something like:
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
And then style it like you want:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AlertDialogCustom" parent="#android:style/Theme.Dialog">
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">10sp</item>
</style>
</resources>
I was having this AlertDialog theme related issue using sdk 1.6 as described here: http://markmail.org/message/mj5ut56irkrkc4nr
I solved the issue by doing the following:
new AlertDialog.Builder(
new ContextThemeWrapper(context, android.R.style.Theme_Dialog))
I have written an article in my blog on how to configure the layout of an AlertDialog with XML style files. The main problem is that you need different style definitions for different layout parameters. Here is a boilerplate based on the AlertDialog style of Holo Light Platform version 19 for a style file that should cover a bunch of the standard layout aspects like text sizes and background colors.
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
...
<item name="android:alertDialogTheme">#style/MyAlertDialogTheme</item>
<item name="android:alertDialogStyle">#style/MyAlertDialogStyle</item>
...
</style>
<style name="MyBorderlessButton">
<!-- Set background drawable and text size of the buttons here -->
<item name="android:background">...</item>
<item name="android:textSize">...</item>
</style>
<style name="MyButtonBar">
<!-- Define a background for the button bar and a divider between the buttons here -->
<item name="android:divider">....</item>
<item name="android:dividerPadding">...</item>
<item name="android:showDividers">...</item>
<item name="android:background">...</item>
</style>
<style name="MyAlertDialogTitle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
</style>
<style name="MyAlertTextAppearance">
<!-- Set text size and color of title and message here -->
<item name="android:textSize"> ... </item>
<item name="android:textColor">...</item>
</style>
<style name="MyAlertDialogTheme">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowTitleStyle">#style/MyAlertDialogTitle</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
<item name="android:windowIsFloating">true</item>
<item name="android:textAppearanceMedium">#style/MyAlertTextAppearance</item>
<!-- If you don't want your own button bar style use
#android:style/Holo.Light.ButtonBar.AlertDialog
and
?android:attr/borderlessButtonStyle
instead of #style/MyButtonBar and #style/MyBorderlessButton -->
<item name="android:buttonBarStyle">#style/MyButtonBar</item>
<item name="android:buttonBarButtonStyle">#style/MyBorderlessButton</item>
</style>
<style name="MyAlertDialogStyle">
<!-- Define background colors of title, message, buttons, etc. here -->
<item name="android:fullDark">...</item>
<item name="android:topDark">...</item>
<item name="android:centerDark">...</item>
<item name="android:bottomDark">...</item>
<item name="android:fullBright">...</item>
<item name="android:topBright">...</item>
<item name="android:centerBright">...</item>
<item name="android:bottomBright">...</item>
<item name="android:bottomMedium">...</item>
<item name="android:centerMedium">...</item>
</style>
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">#color/colorAccent</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#color/teal</item>
</style>
new AlertDialog.Builder(new ContextThemeWrapper(context,R.style.AlertDialogCustom))
.setMessage(Html.fromHtml(Msg))
.setPositiveButton(posBtn, okListener)
.setNegativeButton(negBtn, null)
.create()
.show();
You can directly assign a theme when you initiate the Builder:
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity(), R.style.MyAlertDialogTheme);
Then customize your theme in your values/styles.xml
<!-- Alert Dialog -->
<style name="MyAlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorBackground">#color/alertDialogBackground</item>
<item name="android:windowBackground">#color/alertDialogBackground</item>
</style>
I was struggling with this - you can style the background of the dialog using android:alertDialogStyle="#style/AlertDialog" in your theme, but it ignores any text settings you have. As #rflexor said above it cannot be done with the SDK prior to Honeycomb (well you could use Reflection).
My solution, in a nutshell, was to style the background of the dialog using the above, then set a custom title and content view (using layouts that are the same as those in the SDK).
My wrapper:
import com.mypackage.R;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAlertDialogBuilder extends AlertDialog.Builder {
private final Context mContext;
private TextView mTitle;
private ImageView mIcon;
private TextView mMessage;
public CustomAlertDialogBuilder(Context context) {
super(context);
mContext = context;
View customTitle = View.inflate(mContext, R.layout.alert_dialog_title, null);
mTitle = (TextView) customTitle.findViewById(R.id.alertTitle);
mIcon = (ImageView) customTitle.findViewById(R.id.icon);
setCustomTitle(customTitle);
View customMessage = View.inflate(mContext, R.layout.alert_dialog_message, null);
mMessage = (TextView) customMessage.findViewById(R.id.message);
setView(customMessage);
}
#Override
public CustomAlertDialogBuilder setTitle(int textResId) {
mTitle.setText(textResId);
return this;
}
#Override
public CustomAlertDialogBuilder setTitle(CharSequence text) {
mTitle.setText(text);
return this;
}
#Override
public CustomAlertDialogBuilder setMessage(int textResId) {
mMessage.setText(textResId);
return this;
}
#Override
public CustomAlertDialogBuilder setMessage(CharSequence text) {
mMessage.setText(text);
return this;
}
#Override
public CustomAlertDialogBuilder setIcon(int drawableResId) {
mIcon.setImageResource(drawableResId);
return this;
}
#Override
public CustomAlertDialogBuilder setIcon(Drawable icon) {
mIcon.setImageDrawable(icon);
return this;
}
}
alert_dialog_title.xml (taken from the SDK)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/title_template"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginTop="6dip"
android:layout_marginBottom="9dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip">
<ImageView android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingTop="6dip"
android:paddingRight="10dip"
android:src="#drawable/ic_dialog_alert" />
<TextView android:id="#+id/alertTitle"
style="#style/?android:attr/textAppearanceLarge"
android:singleLine="true"
android:ellipsize="end"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView android:id="#+id/titleDivider"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:scaleType="fitXY"
android:gravity="fill_horizontal"
android:src="#drawable/divider_horizontal_bright" />
</LinearLayout>
alert_dialog_message.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:paddingBottom="12dip"
android:paddingLeft="14dip"
android:paddingRight="10dip">
<TextView android:id="#+id/message"
style="?android:attr/textAppearanceMedium"
android:textColor="#color/dark_grey"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip" />
</ScrollView>
Then just use CustomAlertDialogBuilder instead of AlertDialog.Builder to create your dialogs, and just call setTitle and setMessage as usual.
For Custom Dialog:
just call super(context,R.style.<dialog style>) instead of super(context) in dialog constructor
public class MyDialog extends Dialog
{
public MyDialog(Context context)
{
super(context, R.style.Theme_AppCompat_Light_Dialog_Alert)
}
}
For AlertDialog:
Just create alertDialog with this constructor:
new AlertDialog.Builder(
new ContextThemeWrapper(context, android.R.style.Theme_Dialog))
I guess it cannot be done. At least not with the Builder. I'm working with 1.6 and the Implementation in Builder.create() is:
public AlertDialog create() {
final AlertDialog dialog = new AlertDialog(P.mContext);
P.apply(dialog.mAlert);
[...]
}
which calls the "not-theme-aware" constructor of AlertDialog, which looks like this:
protected AlertDialog(Context context) {
this(context, com.android.internal.R.style.Theme_Dialog_Alert);
}
There is a second constructor in AlertDialog for changing themes:
protected AlertDialog(Context context, int theme) {
super(context, theme);
[...]
}
that the Builder just doesn't call.
If the Dialog is pretty generic anyway, I'd try writing a subclass of AlertDialog, calling the second constructor and use that class instead of the Builder-mechanism.
Better way to do this use custom dialog and customize according your needs here is custom dialog example.....
public class CustomDialogUI {
Dialog dialog;
Vibrator vib;
RelativeLayout rl;
#SuppressWarnings("static-access")
public void dialog(final Context context, String title, String message,
final Runnable task) {
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom);
dialog.setCancelable(false);
TextView m = (TextView) dialog.findViewById(R.id.message);
TextView t = (TextView) dialog.findViewById(R.id.title);
final Button n = (Button) dialog.findViewById(R.id.button2);
final Button p = (Button) dialog.findViewById(R.id.next_button);
rl = (RelativeLayout) dialog.findViewById(R.id.rlmain);
t.setText(bold(title));
m.setText(message);
dialog.show();
n.setText(bold("Close"));
p.setText(bold("Ok"));
// color(context,rl);
vib = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
n.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
vib.vibrate(15);
dialog.dismiss();
}
});
p.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
vib.vibrate(20);
dialog.dismiss();
task.run();
}
});
}
//customize text style bold italic....
public SpannableString bold(String s) {
SpannableString spanString = new SpannableString(s);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0,
spanString.length(), 0);
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
// spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0,
// spanString.length(), 0);
return spanString;
}
}
Here is xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
>
<RelativeLayout
android:id="#+id/rlmain"
android:layout_width="fill_parent"
android:layout_height="150dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#569CE3" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25dip"
android:layout_marginTop="10dip" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Are you Sure?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textSize="13dip" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/relativeLayout1"
android:layout_alignRight="#+id/relativeLayout1"
android:layout_below="#+id/relativeLayout1"
android:layout_marginTop="5dip" >
</RelativeLayout>
<ProgressBar
android:id="#+id/process"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="3dip"
android:layout_marginTop="3dip" />
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/relativeLayout2"
android:layout_below="#+id/relativeLayout2"
android:layout_toLeftOf="#+id/process" >
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textSize="13dip"/>
</RelativeLayout>
<Button
android:id="#+id/next_button"
android:layout_width="90dip"
android:layout_height="35dip"
android:layout_alignParentBottom="true"
android:textColor="#drawable/button_text_color"
android:background="#drawable/blue_button"
android:layout_marginBottom="5dp"
android:textSize="10dp"
android:layout_alignRight="#+id/relativeLayout3"
android:text="Okay" />
<Button
android:id="#+id/button2"
android:text="Cancel"
android:textColor="#drawable/button_text_color"
android:layout_width="90dip"
android:layout_height="35dip"
android:layout_marginBottom="5dp"
android:background="#drawable/blue_button"
android:layout_marginRight="7dp"
android:textSize="10dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/next_button"
/>
</RelativeLayout>
Anyone trying to do this within a Fragment (using the support library i.e. pre API 11) should go with this:
public class LoadingDialogFragment extends DialogFragment {
public static final String ID = "loadingDialog";
public static LoadingDialogFragment newInstance() {
LoadingDialogFragment f = new LoadingDialogFragment();
return f;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
StyleAlertDialog adb = new StyleAlertDialog(getActivity(), R.style.Your_Style);
adb.setView(getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog_layout, null));
return adb;
}
private class StyleAlertDialog extends AlertDialog {
protected StyleAlertDialog(Context context, int theme) {
super(context, theme);
}
}
}
#Rflexor gave me the nudge to extend AlertDialog and expose the constructor thanks
Arve Waltin's solution looks good, although I haven't tested it yet. There is another solution in case you have trouble getting that to work.... Extend AlertDialog.Builder and override all the methods (eg. setText, setTitle, setView, etc) to not set the actual Dialog's text/title/view, but to create a new view within the Dialog's View do everything in there. Then you are free to style everything as you please.
To clarify, as far as the parent class is concerned, the View is set, and nothing else.
As far as your custom extended class is concerned, everything is done within that view.
I"m not sure how Arve's solution would work in a custom Dialog with builder where the view is inflated via a LayoutInflator.
The solution should be to insert the the ContextThemeWrapper in the inflator through cloneInContext():
View sensorView = LayoutInflater.from(context).cloneInContext(
new ContextThemeWrapper(context, R.style.AppTheme_DialogLight)
).inflate(R.layout.dialog_fingerprint, null);
You can override the default theme used by DialogFragments spawned by an activity by modifying the activity's theme's attributes....
set the activity's theme in AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld">
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"> <!-- set all Activity themes to your custom theme -->
.....
</application>
</manifest>
in the values/styles.xml, override the item used to determine what theme to use for spawned DialogFragments
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat">
<!-- override the default theme for DialogFragments -->
<item name="android:dialogTheme">#style/AppTheme.Dialog</item>
</style>
.....
</resources>
in the values/styles.xml, define and configure the theme you want to use for DialogFragments
<?xml version="1.0" encoding="utf-8"?>
<resources>
.....
<!--
configure your custom theme for DialogFragments...
-->
<style name="AppTheme.Dialog" parent="Theme.AppCompat.Dialog.MinWidth">
<!-- override the default theme for DialogFragments spawned by this DialogFragment -->
<item name="android:dialogTheme">#style/AppTheme.Dialog</item>
<!--
OPTIONAL: override the background for the dialog...i am using a dark theme,
and for some reason, there is no themes for dialogs with dark backgrounds,
so, i made my own.
-->
<item name="android:windowBackground">#drawable/dialog__window_background</item>
<!--
add the title to the dialog's theme. you can remove it later by using
DialogFragment.setStyle()
-->
<item name="android:windowNoTitle">false</item>
<item name="windowNoTitle">?android:windowNoTitle</item>
</style>
.....
</resources>
OPTIONAL: if you use a dark theme, and overrode android:windowBackground like i did in AppTheme.Dialog, then add a drawable/dialog__window_background.xml file with the contents:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="16dp"
android:insetTop="16dp"
android:insetRight="16dp"
android:insetBottom="16dp">
<shape android:shape="rectangle">
<corners android:radius="?dialogCornerRadius" />
<solid android:color="?android:colorBackground" />
</shape>
</inset>
It can done simply by using the Builder's setView(). You can create any view of your choice and feed into the builder. This works good. I use a custom TextView that is rendered by the dialog builder. I dont set the message and this space is utilized to render my custome textview.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setMessage("Description");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
Related
on android O when i do this :
<style name="MyTheme" parent="#android:style/Theme.Material.Light.NoActionBar">
<item name="android:textColor">#ff0000</item>
</style>
The text button color of my alertdialog change, but this not work under lollipop. Worse on lollipop it's change the color of the title of the alertdialog instead.
How from kitkat to android O I can globally change the font color of the button of all my alertdialog ?
With the MaterialComponents theme and the MaterialAlertDialogBuilder you can define globally the style using the materialAlertDialogTheme attribute in your app theme.
Something like:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="materialAlertDialogTheme">#style/My_MaterialAlertDialog</item>
</style>
Then you can define a custom style:
<style name="My_MaterialAlertDialog" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<!-- Style for positive button -->
<item name="buttonBarPositiveButtonStyle">#style/PositiveButtonStyle</item>
<!-- Style for negative button -->
<item name="buttonBarNegativeButtonStyle">#style/NegativeButtonStyle</item>
<!-- Style for neutral button -->
<item name="buttonBarNeutralButtonStyle">#style/NeutralButtonStyle</item>
</style>
with the button style defined by:
<style name="PositiveButtonStyle" parent="#style/Widget.MaterialComponents.Button">
<item name="android:textColor">#FFFFFF</item>
<item name="backgroundTint">#color/primaryDarkColor</item>
</style>
<style name="NegativeButtonStyle" parent="#style/Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#color/primaryDarkColor</item>
</style>
<style name="NueutralButtonStyle" parent="#style/Widget.MaterialComponents.Button.TextButton.Dialog">
....
</style>
With the version 1.1.0 of the library you can also simply override the default color using the materialThemeOverlay in the custom style:
<style name="My_MaterialAlertDialog" parent="#style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="materialThemeOverlay">#style/DialogButtonOverlay</item>
</style>
<style name="DialogButtonOverlay">
<item name="colorPrimary">#color/...</item>
</style>
You need to write a theme for AlertDialog and set it to AppTheme. It will change you alet dialog theme globally.
<style name="AppAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">#style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">#style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#color/colorPrimaryText</item>
<item name="android:backgroundTint">#android:color/transparent</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#color/colorPrimaryText</item>
<item name="android:backgroundTint">#android:color/transparent</item>
</style>
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
...
<item name="alertDialogTheme">#style/AppAlertDialog</item>
</style>
Create a Java Class for your custom alert dialog
public class Dialog {
private static final int resId = R.layout.dialog_dialog;
private AlertDialog alertDialog;
/**
* Custom Dialog
*
* #param context
* #param titleText
* #param message
* #param positiveText
* #param negativeText
* #param type
* #param dialogListener
*/
public Dialog(final Context context,
String titleText,
String message,
String positiveText,
String negativeText,
Type type,
final DialogListener dialogListener) {
TextView labelTitle, labelMessage, buttonPositive, buttonNegative;
View view = LayoutInflater.from(context).inflate(resId, null, false);
labelTitle = view.findViewById(R.id.labelTitle);
labelMessage = view.findViewById(R.id.labelMessage);
buttonPositive = view.findViewById(R.id.buttonPositive);
buttonNegative = view.findViewById(R.id.buttonNegative);
if (Utils.isNotEmpty(titleText)) labelTitle.setText(titleText); //(HMI2Utils.isNotEmpty is a null check
if (Utils.isNotEmpty(message)) labelMessage.setText(message);
if (Utils.isNotEmpty(positiveText)) buttonPositive.setText(positiveText);
if (Utils.isNotEmpty(negativeText)) buttonNegative.setText(negativeText);
switch (type) {
case DANGEROUS:
labelTitle.setTextColor(ContextCompat.getColor(context, R.color.white));
}
buttonNegative.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
dialogListener.onNegativeButtonClick();
}
});
buttonPositive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
dialogListener.onPositiveButtonClick();
}
});
Utils.hideSoftKeyboard((Activity) context);
ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.MyDialogTheme);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setView(view);
alertDialog = alertDialogBuilder.create();
Window window = alertDialog.getWindow();
if (window != null) {
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(ContextCompat.getColor(context, R.color.app_theme_color));
}
alertDialog.show();
}
and for xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rlRoot"
android:layout_width="736.15px"
android:layout_height="532.66px"
tools:ignore="Overdraw">
<View
android:id="#+id/focus_eater_dummy"
android:layout_width="1px"
android:layout_height="1px"
android:focusable="true" />
<ImageView
android:layout_width="736.15px"
android:layout_height="532.66px"
android:scaleType="fitXY"
android:src="your drawable" />
<RelativeLayout
android:id="#+id/container"
android:layout_width="736.15px"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="67.83px"
android:layout_marginRight="67.83px"
android:layout_marginTop="57.855px"
android:orientation="vertical">
<TextView
android:id="#+id/labelTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_horizontal"
android:maxLines="1"
android:text="#string/delete_message_string"
android:textColor="#color/white"
android:textSize="24sp" />
<TextView
android:id="#+id/labelMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18px"
android:lineSpacingExtra="18px"
android:gravity="center_horizontal"
android:lineSpacingMultiplier="1"
android:text="#string/delete_message_string"
android:textColor="#color/white"
android:textSize="24sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/containerButtons"
android:layout_width="736.15px"
android:layout_height="532.66px"
android:weightSum="2"
android:gravity="bottom"
android:layout_gravity="bottom"
android:orientation="horizontal">
<TextView
android:id="#+id/buttonPositive"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="#dimen/margin_button_vertical_dai"
android:background="#drawable/popup_button_selector"
android:gravity="center"
android:textColor="#color/white"
android:textSize="32sp"
tools:text="OK" />
<TextView
android:id="#+id/buttonNegative"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="#drawable/popup_button_selector"
android:gravity="center"
android:text="Cancel"
android:textColor="#color/white"
android:textSize="32sp" />
</LinearLayout>
</FrameLayout>
make your custom changes and call the dialog like
new Dialog(context,
"Title 1",
"Message 2",
"OK",
"Cancel",
Dialog.Type.DANGEROUS,
new Dialog.DialogListener() {
#Override
public void onPositiveButtonClick() {
//implement Click here
}
#Override
public void onNegativeButtonClick() {
//implement Click here
}
}
);
dialog.dismiss();
I am trying to customize the accent color for AlertDialog buttons. But it is not taking any affect it seems it is inheriting the color from system. Here is my style/theme.
<color name="actionable_items">#0574ac</color> <!-- it is blue color -->
<style name="LLDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<!--buttons color-->
<item name="colorAccent">#color/actionable_items</item>
<!--item RadioButton or CheckBox color-->
<item name="colorControlActivated">#color/actionable_items</item>
<item name="colorPrimary">#color/actionable_items</item>
<item name="colorPrimaryDark">#color/actionable_items</item>
<item name="android:listChoiceIndicatorMultiple">#color/actionable_items</item>
<item name="android:listChoiceIndicatorSingle">#color/actionable_items</item>
</style>
Here is my code which is trying to build the Alertdialog.
final CustomPopupBuilder removePlaceDialog = new CustomPopupBuilder(new ContextThemeWrapper(context,
R.style.LLDialog));
removePlaceDialog.setTitle(getString(R.string.delete_place, placeName));
removePlaceDialog.setMessage(getString(R.string.delete_place_message));
removePlaceDialog.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
....
....
}
});
removePlaceDialog.setNegativeButton(R.string.cancel, null);
removePlaceDialog.create().show();
The final AlertDialog doesn't have buttons with the same text color. The text color is similar to green. It seems like it is inheriting the color from the system instead of the customized theme.
Here is the image :
EDIT1:
I tried the use the AlertDialog.Builder but it gives me the same result.
final AlertDialog.Builder removePlaceDialog = AlertDialog.Builder(new ContextThemeWrapper(context,
R.style.LLDialog));
removePlaceDialog.setTitle(getString(R.string.delete_place, placeName));
removePlaceDialog.setMessage(getString(R.string.delete_place_message));
removePlaceDialog.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
....
....
}
});
removePlaceDialog.setNegativeButton(R.string.cancel, null);
removePlaceDialog.create().show();
Edit2:
I also tried to change the accent color for the dialog box but I don't see that color:
<style name="LLDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<!--buttons color-->
<item name="colorAccent">#990000</item>
...
...
</style>
Even this doesn't change the button text color :(.
Please check your AlertDialog import. It should be imported from v7 support lib for styles to be applied on older Android versions. I had the same problem and changing import line from
import android.app.AlertDialog
to
import android.support.v7.app.AlertDialog
helped me.
2019 Update:
Cause of google released AndroidX libraries, the new answer would be
import androidx.appcompat.app.AlertDialog;
Thx to #ChristosThemelis
I had the same problem and this is how I solved it:
In styles.xml, declare your Theme and set the attribute android:alertDialogTheme:
<style name="YourTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- your theme attributes here -->
<item name="android:alertDialogTheme">#style/YourDialogTheme</item>
</style>
<style name="YourDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert" >
<!-- your dialog-theme attributes here -->
</style>
Now, if you show an AlertDialog like so...
AlertDialog.Builder builder = new AlertDialog.Builder(activity); //where activity is an Activity with the theme attribute set to android:theme="#style/YourTheme" (in AndroidManifest)
//...
builder.show();
...the dialog should have the accentColor and everything set to what you specified in #style/YourDialogTheme
Create a new style like this below:
<style name="AlertDialogCustom" parent="#android:style/Theme.Holo.Light.Dialog">
<!--buttons color-->
<item name="colorAccent">#color/actionable_items</item>
<!--item RadioButton or CheckBox color-->
<item name="colorControlActivated">#color/actionable_items</item>
<item name="colorPrimary">#color/actionable_items</item>
<item name="colorPrimaryDark">#color/actionable_items</item>
<item name="android:listChoiceIndicatorMultiple">#color/actionable_items</item>
<item name="android:listChoiceIndicatorSingle">#color/actionable_items</item>
</style>
And then in your class:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AlertDialogCustom));
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
If your theme not reflecting your AlertDialog buttons color, you can manage it programmatically also, that could be useful .
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AppCompatAlertDialogStyle));
builder.setCancelable(false);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
String message ="message";
builder.setTitle("title");
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
// builder.setNegativeButton("No, Thanks", null);
//builder.show();
AlertDialog alert = builder.create();
alert.show();
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
pbutton.setTextColor(getResources().getColor(R.color.colorAccent));
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
nbutton.setTextColor(getResources().getColor(R.color.accent));
Here you can change color of both buttons if you required, hope it might userful for you
Here an example hope it will help you
public class CustomDialogUI {
Dialog dialog;
Vibrator vib;
RelativeLayout rl;
#SuppressWarnings("static-access")
public void dialog(final Context context, String title, String message,
final Runnable task) {
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom);
dialog.setCancelable(false);
TextView m = (TextView) dialog.findViewById(R.id.message);
TextView t = (TextView) dialog.findViewById(R.id.title);
final Button n = (Button) dialog.findViewById(R.id.button2);
final Button p = (Button) dialog.findViewById(R.id.next_button);
rl = (RelativeLayout) dialog.findViewById(R.id.rlmain);
t.setText(bold(title));
m.setText(message);
dialog.show();
n.setText(bold("Close"));
p.setText(bold("Ok"));
// color(context,rl);
vib = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
n.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
vib.vibrate(15);
dialog.dismiss();
}
});
p.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
vib.vibrate(20);
dialog.dismiss();
task.run();
}
});
}
//customize text style bold italic....
public SpannableString bold(String s) {
SpannableString spanString = new SpannableString(s);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0,
spanString.length(), 0);
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
// spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0,
// spanString.length(), 0);
return spanString;
}
}
XML layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
>
<RelativeLayout
android:id="#+id/rlmain"
android:layout_width="fill_parent"
android:layout_height="150dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#569CE3" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25dip"
android:layout_marginTop="10dip" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Are you Sure?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textSize="13dip" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/relativeLayout1"
android:layout_alignRight="#+id/relativeLayout1"
android:layout_below="#+id/relativeLayout1"
android:layout_marginTop="5dip" >
</RelativeLayout>
<ProgressBar
android:id="#+id/process"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="3dip"
android:layout_marginTop="3dip" />
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/relativeLayout2"
android:layout_below="#+id/relativeLayout2"
android:layout_toLeftOf="#+id/process" >
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textSize="13dip"/>
</RelativeLayout>
<Button
android:id="#+id/next_button"
android:layout_width="90dip"
android:layout_height="35dip"
android:layout_alignParentBottom="true"
android:textColor="#drawable/button_text_color"
android:background="#drawable/blue_button"
android:layout_marginBottom="5dp"
android:textSize="10dp"
android:layout_alignRight="#+id/relativeLayout3"
android:text="Okay" />
<Button
android:id="#+id/button2"
android:text="Cancel"
android:textColor="#drawable/button_text_color"
android:layout_width="90dip"
android:layout_height="35dip"
android:layout_marginBottom="5dp"
android:background="#drawable/blue_button"
android:layout_marginRight="7dp"
android:textSize="10dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/next_button"
/>
</RelativeLayout>
I was able to solve this in my use-case by making the parent of the style for the alert dialog the same as the parent for my main style.
I'm developing an Android application and I have a question about custom dialogs.
I do this to open a custom dialog:
protected void showSetFriendEmailDialog()
{
// Create the dialog.
final Dialog emailDialog =
new Dialog(FriendHomeActivity.this, android.R.style.Theme_DeviceDefault);
emailDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
emailDialog.setCancelable(true);
emailDialog.setContentView(R.layout.dialog_add_friend_email);
// Get dialog widgets references.
final EditText editFriendsEmail = (EditText)emailDialog.findViewById(R.id.editEmailAddFriendEmail);
Button btnAccept = (Button)emailDialog.findViewById(R.id.btnAddFriendEmail);
// Set on click lister for accept button
btnAccept.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
// Get selected values
String friendEmail =
editFriendsEmail.getText().toString();
// Close dialog.
emailDialog.dismiss();
// TODO: Call api to send email to web service using friendsEmail var.
Log.v(TAG, "Friend email: " + friendEmail);
}
});
//now that the dialog is set up, it's time to show it
emailDialog.show();
}
And this is its layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textAddFriendEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/layout_set_friend_email"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editEmailAddFriendEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnAddFriendEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok" />
</LinearLayout>
But it opens full screen.
What do I have to do to open it as an AlertDialog?
you have to use android.R.style.Theme.Dialog instenad of android.R.style.Theme_DeviceDefault
Use R.style.Theme_AppCompat_Dialog in order to get the proper dialog.
Just make a custom style and apply it to dialog...
<style name="full_screen_dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
I am creating a custom dialog. Its example code is:
final AlertDialog dialog;
protected AlertDialog createDialog(int dialogId) {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(parent);
AlertDialog fDialog = null;
switch(dialogId) {
case Constants.cusDialogtId:
builder = new AlertDialog.Builder(parent);
builder.setTitle("Title");
LayoutInflater inflater = (LayoutInflater)parent.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.customdialog, null);
builder.setView(view);
fDialog = builder.create();
break;
}
dialog = fDialog;
return dialog;
}
The problem is that when the dialog is shown, it has a gray background of the native dialog whose some top and bottom border is also shown with my custom dialog.
Is there some way to show only my custom dialog view...???
The XML I am using is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#drawable/bgsmall" >
<EditText android:id="#+id/redeemamount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:hint="Enter amount"
android:inputType="numberDecimal">
</EditText>
<Button android:id="#+id/submitRedeemAmountButton"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:text="Submit"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:background="#drawable/buttoncorner"
android:layout_marginTop="20dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginBottom="20dip">
</Button>
</LinearLayout>
I don't think you can remove the borders by using AlertDialog.Builder.
What you can do is create a CustomDialog class that extends Dialog and in the constructor of your CustomDialog you inflate your customdialog.xml.
Also you will need to create a custom style for your dialog, that hides the borders. Here is an example:
<style
name="CustomStyle"
parent="android:Theme.Dialog">
<item
name="android:windowBackground">#color/transparent</item>
<item
name="android:windowContentOverlay">#null</item>
</style>
Also define the transparent color:
<color
name="transparent">#00000000</color>
And you will create your dialog using :
CustomDialog dialog=new CustomDialog(this,R.style.CustomStyle);
Create a custom theme:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#null</item>
</style>
</resources>
then use it:
builder = new AlertDialog.Builder(parent, R.style.CustomDialog);
Update
The constructor above is indeed API 11+. To work around this you need to extend AlertDialog (since its constructors are protected) and and then use constructor with theme parameter. To insert your custom view follow the instructions here - the FrameLayout trick described at the beginning.
How do I remove this "shadowBox" effect around the dialog box that fades the background?
I know I can create like totally custom view but let's say I want to keep it simple. I have xml layout for dialog with only textView and 2 buttons and I want to stop the fade only for this one dialog.
Try:
dialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);
create style.xml inside values and put things like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="progress_dialog">
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">#00ffffff</item>
</style>
And in the end, put this style to your dialog box.
Simply use
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
here give the sample class
public void showDialog () {
Dialog dialog = new Dialog(this);
Window window = dialog.getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
window.requestFeature(window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.sample);
ImageView imggameover, imgplayagain;
imggameover = (ImageView) dialog.findViewById(R.id.gameover);
imgplayagain = (ImageView) dialog.findViewById(R.id.playagain);
imgplayagain.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), random.class));
onStop();
finish();
}
});
dialog.show();
}
Dialog's layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gameoverlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
>
<ImageView
android:id="#+id/gameover"
android:layout_width="225dip"
android:layout_height="160dip"
android:background="#drawable/gameover"
></ImageView>
<ImageView
android:id="#+id/playagain"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:layout_marginTop="100dip"
android:background="#drawable/playagain"
></ImageView>
</RelativeLayout>
Just that you know it is also possible to change the dim amount by writing this in your theme:
<item name="android:backgroundDimAmount">0.5</item>