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.
Related
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(...);
...
}
I'm trying to style all my dialog fragments to look the same in my app. The dialogs coming from my settings fragment are styled exactly the way I want it. For my custom dialog fragments, the style is similar but not exactly the same. For some reason the spinner, timepicker, datepicker, radiobuttons, and edittext widgets inside my custom dialog fragments don't pick up the same style. In fact, the widgets blend in with the white background and you can't see that they are there. What am I doing wrong?
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"/>
<style name="Theme.Base" parent="AppTheme">
<item name="colorPrimary">#color/PrimaryBackgroundColor</item>
<item name="colorPrimaryDark">#color/SecondaryBackgroundColor</item>
<item name="colorAccent">#color/ColorBackgroundAccent</item>
<item name="android:textColorPrimary">#color/PrimaryTextColor</item>
<item name="android:alertDialogTheme">#style/AppTheme.DialogStyle</item>
</style>
<style name="AppTheme.DialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorPrimary">#color/PrimaryBackgroundColor</item>
<item name="colorAccent">#color/ColorBackgroundAccent</item>
</style>
I'm applying the theme to my custom dialog fragment like this:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppTheme_DialogStyle);
My settings dialog looks like this (Exactly how I want it):
Settings Dialog Fragment
My custom dialog fragment looks like this:
Custom Dialog Fragment
As you can see, the radio button selected color red and you can't see the unselected radio button.
Finally got an answer!!!
It's an issue or bug with AppCompat 22+.
Check out link here
Apparently this was a bug with fragments and widgets weren't getting the material themed in a fragment. It seems they fixed this issue, but the issue still holds in a dialog fragment based on what I'm going through.
The problem comes when you use the inflater instance passed to Fragment#onCreateView(). The workaround for now is to instead used the LayoutInflater from getActivity().getLayoutInflater() according to google.
So I changed my code to:
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
from:
View view = LayoutInflater.from(getActivity().getApplicationContext()).inflate(R.layout.dialoge, null);
All my widgets are now themed. Thanks everyone. Hopes this helps someone else.
I believe you need to set the theme on the actual Dialog and not the Fragment
Use this constructor to create your AlertDialog:
AlertDialog.Builder(Context context, int theme)
ie
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme)
I think you need to add one more item in style of your dialog. android:textColorSecondary will show color of un selected checkbox.
in your style add it.
</style>
<style name="AppTheme.DialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorPrimary">#color/PrimaryBackgroundColor</item>
<item name="colorAccent">#color/ColorBackgroundAccent</item>
<item name="android:textColorSecondary">#000000</item>
</style>
It will make un Checked checkbox or toggle button edge color black. you need to change #000000 to color your want to show.
See if this helps -
Android appcompat-v7:21.0.0 change material checkbox colors
In short, try setting android:textColorSecondary.
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 am attempting to style a DialogFragment and, despite extensive research within SO, developer.android and elsewhere, it's just not working right.
I am sure that I have applied everything correctly; in my DialogFragment onCreateDialog, I am putting:
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.FibroDialog));
and in values/styles.xml, I have:
<style name="FibroDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#drawable/panel_background</item>
</style>
and in values-v14/styles.xml, I have:
<style name="FibroDialog" parent="#android:style/Theme.Holo.Dialog">
<item name="android:windowBackground">#drawable/panel_background</item>
</style>
If I run that, I get the top row of dialogs in the image below, where nothing has changed at all, but I would expect the window to use my panel_background drawable. If I change windowBackground to background, I get the bottom row of dialogs in the image below, where it all goes wrong.
So how do I change this, so the entire dialogs have the purple background? I know I can use the layoutInflator and put a background colour in the layout, but that only does the middle bit.
I don't know if this is of any relevance, but the DialogFragment is launched from a SherlockFragment
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....