I am struggling with changing the TEXT color of AppCompat DialogFragments.
My App uses a DARK theme (Theme.AppCompat.NoActionBar), but for dialogs I want a LIGHT theme. I am using Build Tools, Support Library and compileSdkVersion to 25, shall it matters.
I am able to change everything else on the dialog (title, background, window background) but not the primary and accented text colors, that keep on using the (white) settings for the dark theme, resulting in white text over white background.
I've tried dozens of solutions in similar questions here in SO, like:
1) The easy one: On the styles.xml:
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="alertDialogTheme">#style/AppCompatAlertDialogStyle</item>
<item name="android:alertDialogTheme">#style/AppCompatAlertDialogStyle</item>
</style>
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- ignored !!!! -->
<item name="colorPrimary">#ff0000</item>
<!-- ignored !!!! -->
<item name="colorPrimaryDark">#ff0000</item>
<!-- ignored !!!! -->
<item name="colorAccent">#ff0000</item>
<!-- ignored !!!! -->
<item name="android:textColorPrimary">#F040FF</item>
<!-- ignored !!!! -->
<item name="android:textColor">#F040FF</item>
</style>
With this solution, the background and buttons style from AppCompat.Light.Dialog.Alert IS APPLIED, but not the text colors as you can see in the screenshot:
2) Manually specifying the style on the AlertDialog Creation:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle);
LayoutInflater inflater = getActivity().getLayoutInflater();
View hostView = mHostView = inflater.inflate(layoutId, null);
Same problem. Light background, Light text.
3) Using a ContextWrapper:
ContextThemeWrapper ctw = new ContextThemeWrapper(getActivity(), R.style.AppCompatAlertDialogStyle);
AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
Nothing :( Same thing happens
4) Manually specifying other exotic constants I came across the many posts here in SO, like
Theme_DeviceDefault_Light_Dialog_Alert
THEME_DEVICE_DEFAULT_LIGHT
This was just a desperate attempt, but anyways the text is not changed
5) Specifying the style in the fragment rather than on the dialog
Dialog_Meta newFragment = new Dialog_Meta();
newFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.AppCompatAlertDialogStyle);
newFragment.show(fragmentManager, TAG);
I used this solution time ago in a very old API version, can't remember what was the problem, but anyways, doesn't solve the present issue :(
Can anybody tell me what's going on?
The problem here is the custom View you're setting on the AlertDialog. Though you've set a certain theme for AlertDialogs in general, that View is being inflated with the Activity's theme, which doesn't have those overridden color attribute values.
There are several ways to solve this.
• Create a ContextThemeWrapper around the Activity's Context with the custom R.style, and obtain the LayoutInflater.from() that.
ContextThemeWrapper ctw = new ContextThemeWrapper(getActivity(), R.style.AppCompatAlertDialogStyle);
LayoutInflater inflater = LayoutInflater.from(getActivity());
View hostView = mHostView = inflater.inflate(layoutId, null);
...
• As discovered by the OP, rupps, the AlertDialog.Builder will already have the alertDialogTheme wrapped on the Context it's given, and its getContext() method will return the appropriate ContextThemeWrapper, which can be used for the Inflater.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(builder.getContext()); // THIS IS THE KEY
View hostView = mHostView = inflater.inflate(layoutId, null);
...
From Google's Documentation of AlertDialog.Builder's getContext() method:
/**
* Returns a {#link Context} with the appropriate theme for dialogs created by this
* Builder.
* Applications should use this Context for obtaining LayoutInflaters for inflating views
* that will be used in the resulting dialogs, as it will cause views to be inflated with
* the correct theme.
*
* #return A Context for built Dialogs.
*/
public Context getContext() {
...
• The theme can be set as the android:theme attribute on the root View of the Dialog's layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="#style/AppCompatAlertDialogStyle">
...
• Rather than handling the inflation yourself, the layout's ID can be passed in the Builder's setView() call, and it will be inflated with the alertDialogTheme.
With this method, however, the View objects from the layout won't be available until the Dialog is shown. In a DialogFragment, this will be in the onStart() method.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(R.layout.dialog);
return builder.create();
}
#Override
public void onStart() {
super.onStart();
final Dialog dialog = getDialog();
dialog.findViewById(R.id.dialog_button).setOnClickListener(...);
...
}
Related
I'm trying to change the style of an AlertDialog by using the following code :
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.AlertDialogTheme));
In my styles.xml, I have this style:
<style name="AlertDialogTheme" parent="#android:style/Theme.Holo.Dialog">
<item name="#android:background">#9933CC</item>
<item name="#android:textColor">#FAF5F7</item>
<item name="#android:textSize">5sp</item>
</style>
So basically the issues are :
1.- There is a overhead of the background, not limiting it by the AlertDialog frame.
2.- The color of the buttons are not being affected at all.
3.- The textSize does not affect either.
I had similar problems once with a ProgressDialog, it wasn't using the context I passed through the ContextThemeWrapper. It should work if you pass the context and the style separately through the constructor:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme);
I have tried to use a content wrapper:
ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo_Dialog);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);
The result of this, is a dialog box of a mix of both dark and white, horrible.
I have also tried using customized styles and etc in the past 2 hours, no luck. I believe the solution must be very simple, I just need to trick the AlertDialog Builder to think my activity is Holo dark themed. But how?
This is how I themed my activity, maybe I did something wrong there:
<style name="ThemeSolarizedLight" parent="android:Theme.Holo.Light">
<item name="android:background">#color/light_yellow</item>
<item name="android:textColor">that No Wi-fi color you see up there</item>
</style>
You are using the actionbar's theme instead use the theme made for dialog
sample:
ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo_Dialog;);
I'm using AlertDialog.Builder like this:
ContextThemeWrapper cw = new ContextThemeWrapper(context, R.style.AlertDialogTheme);
AlertDialog.Builder builder = new AlertDialog.Builder(cw);
This is my custom AlertDialogTheme style:
<style name="AlertDialogTheme" parent="#android:Theme.Dialog">
<item name="android:textSize">15sp</item>
<item name="android:windowTitleStyle">#style/custom_style</item>
</style>
The textSize attribute works fine for the list of items I put in the builder with builder.setItems(), but it doesn't work on the title, so I've tried to override the windowTitleStyle attribute, but it doesn't work.
Is it even possible or am I doing something wrong?
I'm having similar issues, and I've done just what you have. One thing to note is that windowTitleStyle isn't used in the dialog title before API 14. Before that it was textAppearanceMedium. And in the default (pre-holo) dialog it is textAppearanceLarge.
Unfortunately, I'm setting all three of these to my custom style and testing in KK, but it still doesn't update the color as I'm expecting.
I used to struggle with turning an activity's background to transparent. I would correctly set the layout background to #00000000 and I would correctly set getWindow().setBackgroundDrawable(new ColorDrawable(0)) in the onCreate method. However, with these two changes, I would always end up with a grayish-black container holding my layout. But then I found that I needed to edit the activity tag in the manifest to add android:theme="#android:style/Theme.Holo.Dialog". Voila! That was all.
But now I need to turn an AlertDialog's background to transparent. There are a number of suggestions out there, and I have tried quite a few. My latest configuration is provided below. But I always end up with the same problem I used to have the activities: the grayish-black container holding my layout. So now, my question is: how do I add android:theme="#android:style/Theme.Holo.Dialog" in the manifest file for my custom dialog?
Current code:
public void showMyDialog() {
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.CustomDialog);
AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
LayoutInflater inflater = (LayoutInflater) ctw.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_layout,
(ViewGroup) findViewById(R.id.pr_root));
builder.setView(view);
builder.show();
}
Style:
<style name="CustomDialog" parent="android:Theme.Holo.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:alertDialogStyle">#style/CustomDialog</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
Of course dialog_layout is a typical layout .xml file.
Use Dialog instead of AlertDialog.Builder and so use setContentView instead of setView.
I want to style the header of an Android dialog. Here is what a standard android dialog looks like
what I want is sth like this which I photoshopped (styled background of header and another text color for it).
I know that it is possible to pass the theme as a parameter to the Dialog()-Constructor. But I don't know how the xml-style elements would look like.
You should be able to use setCustomTitle(View) for this. I've never used it, but it should go something like this:
TextView title = new TextView(context);
title.setBackgroundColor(0xFFFF0000);
title.setTextColor(0xFFFFFFFF);
title.setLayoutParams(/* LayoutParams with MATCH_PARENT width and WRAP_CONTENT height */);
title.setPadding(/* Some padding values */);
yourDialogBuilder.setCustomTitle(title);
In terms of copying verbatim the layout of the existing Android dialog title, I'm not sure what the parameters of those values are. You may have to mess around and/or Google it to find them. (The font itself is obviously Roboto for 4.0+ and Droid for anything less.)
Its really simple...
All you have to do is to create an XML layout, for the pic you have uploaded and then just simply inflate the Dialog with the XML file you just created .....
I'll just give you a sample code, then you can easily follow
private View mView;
private Dialog mDialog;
private LayoutInflater mInflater;
Now create a function as :-
private void showCustomDialog() {
mInflater = (LayoutInflater) getBaseContext().getSystemService(
LAYOUT_INFLATER_SERVICE);
ContextThemeWrapper mTheme = new ContextThemeWrapper(this,
R.style.YOUR_STYE);
mView = mInflater.inflate(R.layout.YOUR_XML_LAYOUT_FILE, null);
// mDialog = new Dialog(this,0); // context, theme
mDialog = new Dialog(mTheme);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(this.mView);
mDialog.show();
TextViiew someText = (TextView) mView.findViewById(R.id.textViewID);
// do some thing with the text view or any other view
}
Finally make sure you have the style as :-
<style name="YOUR_STYLE">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowContentOverlay">#android:color/transparent</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
That's it .... you are done... just call this function where ever you want to show the custom dialog....
Hope the explanation was useful....