I want to change the background of the Alert dialog. I have tried following code piece to do so:
<style name="Theme.AlertDialog" parent="#android:style/Theme.Holo.Light.Dialog">
<item name="android:background">#android:color/transparent</item>
<item name="android:textColor">#659F26</item>
<item name="android:windowTitleStyle">#style/Theme.AlertDialog.Title</item>
</style>
<style name="Theme.AlertDialog.Title" parent="#android:style/TextAppearance.DialogWindowTitle">
<item name="android:background">#drawable/cab_background_top_example</item>
</style>
Java Code:
ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.Theme_AlertDialog);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
alertDialogBuilder.setTitle(getResources().getString(R.string.confirmation));
alertDialogBuilder.setMessage(getResources().getString(R.string.msg));
alertDialogBuilder.show();
My app is showing dialog like this:
while i want it to look like:
Please suggest, what i am doing wrong.
Use this code when creating a dialog:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
create your own layout As many customise done with title then set your layout
dialog.setContentView(R.layout.yourlayout);
NOTE: use
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); before
dialog.setContentView(R.layout.yourlayout);
otherwise it give error.
ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.Theme_AlertDialog);
In this line use instead of "this" use ActivityName.this
Related
I have create a dialog using AlertDialog class. With custom view. I want to set entry and exit animation for it. when I try to do that dialog converts itself to small black background view. How to get animation with proper view ??
final AlertDialog.Builder builder = new AlertDialog.Builder(
/*getContext()*/
new android.support.v7.view.ContextThemeWrapper(getContext(), R.style.StyleDialog));
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_email, null);
builder.setView(view);
builder.setCancelable(false);
alert = builder.create();
alert.show();
These are the styles
<style name="StyleDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">#style/DialogAnimation</item>
</style>
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/fadein</item>
<item name="android:windowExitAnimation">#anim/fadeout</item>
</style>
tried both android.support.v7.view.ContextThemeWrapper and android.view.ContextThemeWrapper but no difference.
Using android.app.AlertDialog class.
I found the solution. Just set background(in my case white) for layout which I had not given. Because when we set our own style then it use black background by default and if do not set style then it uses white background by default.
android:background="#color/bg_white"
For proper width, set first view width to match_parent.
android:layout_width="match_parent"
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'm applying a theme:
<style name="myAlertTheme" parent="#android:style/AlertDialog">
<item name="android:textColor">#ff000000</item>
</style>
<style name="RadioButton" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:textColor">#ff000000</item>
</style>
<style name="HSDroidStyle" parent="android:Theme">
<item name="android:background">#ffd3d3d3</item>
<item name="android:textColor">#ff000000</item>
<item name="android:textSize">20dp</item>
<item name="android:radioButtonStyle">#style/RadioButton</item>
</style>
But when I create an Alert, the text shows up as white instead of black. To create the alert I'm using:
AlertDialog alertDialog = new AlertDialog.Builder(
new ContextThemeWrapper(act, R.style.myAlertTheme)).create();
alertDialog.setTitle(title);
alertDialog.setMessage(msg);
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (exitOnFailure) mainAct.finish();
return;
} });
alertDialog.show();
Now the "OK" in the button changes if I change the size in the myAlertTheme, but not the title or the text. So I'm guessing that there is some other textColor attribute that addresses this. I've tried most of the ones I can think of, but none of them work.
Any ideas?
hooked82 is right. Prior to Honeycomb, Android did not support text styling properly for AlertDialogs. You could apply styles to an AlertDialog, but it would ignore the text styles.
Here's a good SO answer that would prove to be a good solution for you: How to change theme for AlertDialog
Here's a really useful blog post that uses the propsed solution above to style an AlertDialog properly.
AFAIK there isn't a way to modify the Title/Message text colors (Please correct me if I'm wrong). So one way of doing this would be to create a custom view for your AlertDialog and do something like the following:
LayoutInflater factory = LayoutInflater.from(this);
final View layout = factory.inflate(R.layout.alert_layout, null);
alertDialog.setView(layout);
Try to declare the AlertDialog using this statement:
AlertDialog alertDialog = new AlertDialog(mContext, R.style.myAlertTheme);