I want to change the color of the blue Title and blue line that comes in AlertDialog ( Holo Theme ), I was able to change the color of Title by looking into android styles.xml and themes.xml but i cannot find any property or style that i can use/change to change the color of my XML.
Following is the style that I am using to change the title color
<style name="myAlertDialog" parent="android:Theme.Holo.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowTitleStyle">#style/myAlertDialogTitleStyle</item>
</style>
<style name="myAlertDialogTitleStyle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">#style/myAlertDialogTitleStyleTextAppearance</item>
</style>
<style name="myAlertDialogTitleStyleTextAppearance">
<item name="android:textSize">22sp</item>
<item name="android:textColor">#ffff8800</item>
</style>
EDIT: I was mistaken in my original answer.
Unfortunately the styles used for dialogs are internal and cannot be used in the same manner as some of the other easy-to-manipulate holo elements. I have created an open source project for dealing with this in very simple situations (I hope to extend it down the road and make it more.. legit). Here is a link to a new answer that addresses this more in depth: How can I change the color of AlertDialog title and the color of the line under it
For posterity, here is my naive old answer:
Look at the answer posted here. For your particular goal, you will want to be overriding the dialogTitleDecorLayout style (I think!) as opposed to the listSeparatorTextViewStyle style.
If you are curious where this is originally set, you can poke around the themes.xml file in your android sdk folder on your computer and search for the dialogTitleDecorLayout attribute. This should lead you to a dialog_title_holo layout file which should also be on your computer. There you should find the following code within the layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:fitsSystemWindows="true">
<TextView android:id="#android:id/title" style="?android:attr/windowTitleStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="60dip"
android:paddingLeft="32dip"
android:paddingRight="32dip"
android:gravity="center_vertical|left" />
<ImageView android:id="#+id/titleDivider"
android:layout_width="match_parent"
android:layout_height="4dip"
android:scaleType="fitXY"
android:gravity="fill_horizontal"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:src="#android:drawable/divider_strong_holo" />
<FrameLayout
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:foreground="?android:attr/windowContentOverlay">
<FrameLayout android:id="#android:id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
The code of note is the #android:drawable/divider_strong_holo reference. This file also exists on your computer and it should be a blue 9patch. You will need to create a similar 9patch of a different color. Good Luck! Even if this isn't quite the right attribute I think you see what you may need to do here...
After some tinkering, I've come up with a way to re-brand the dialog the moment it is shown:
private AlertDialog showWithThemeColors() {
Log.d(TAG, "showWithThemeColors()");
AlertDialog dialog = this.builder.show();
try {
ViewGroup decorView = (ViewGroup) dialog.getWindow().getDecorView();
FrameLayout windowContentView = (FrameLayout) decorView.getChildAt(0);
FrameLayout contentView = (FrameLayout) windowContentView.getChildAt(0);
LinearLayout parentPanel = (LinearLayout) contentView.getChildAt(0);
LinearLayout topPanel = (LinearLayout) parentPanel.getChildAt(0);
View titleDivider = topPanel.getChildAt(2);
LinearLayout titleTemplate = (LinearLayout) topPanel.getChildAt(1);
TextView alertTitle = (TextView) titleTemplate.getChildAt(1);
int textColor = this.context.getResources().getColor(R.color.customer_text_on_primary);
alertTitle.setTextColor(textColor);
int primaryColor = this.context.getResources().getColor(R.color.customer_primary);
titleDivider.setBackgroundColor(primaryColor);
} catch (Exception e) {
Log.e(TAG, "showWithThemeColors() - Could not re-brand dialog with theme colors.", e);
}
return dialog;
}
Note, that this solution can break anytime the underlying system layout for the dialog changes. However, it is the original system dialog with all the advantages of such a solution.
There is a library which does exactly that for all your dialogs:
https://github.com/inmite/android-styled-dialogs
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
My Style
<style name="myAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowTitleBackgroundStyle">#color/colorPrimary</item>
</style>
My AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext(), R.style.myAlertDialog);
builder.setTitle("Dialog Title");
builder.setMessage("This is message!");
builder.create().show();
My custom style has completely no effect. Any help?
After searched so much online regarding this problem, I found out that windowTitleBackgroundStyle doesn't work, but windowTitleStyle works however, the result is not what I wanted:
<style name="myAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowTitleStyle">#style/myDialogTitle</item>
</style>
<style name="myDialogTitle" parent="TextAppearance.AppCompat.Title">
<item name="android:background">#color/colorPrimary</item>
</style>
It looks like there are fixed paddings inside the whole AlertDialog layout. Besides that, getResource().getIdentifier() and findViewById() methods are not working so far. Therefore, setCustomTitle() becomes the only way to modify the title of AlertDialog.
1. Creating a Custom Title Layout for AlertDialog
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_light"
android:orientation="vertical"
android:paddingStart="24dp" //following Material Design guideline
android:paddingTop="16dp"
android:paddingEnd="24dp"
android:paddingBottom="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Marvel Cinematic Universe"
android:textAppearance="#style/TextAppearance.AppCompat.Title" //key point 1, using AlertDialog default text style
android:textColor="#android:color/white" />
</LinearLayout>
2. Applying Custom Title Layout to AlertDialog Using "setCustomTitle()"
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
View titleView = getLayoutInflater().inflate(R.layout.dialog_title, null);
builder.setCustomTitle(titleView); //key point 2
builder.setMessage(R.string.main_marvel_info);
builder.setPositiveButton("OK", null);
builder.create().show();
I have an app with multiple buttons in a ScrollView, and I add more programmatically after the Activity start (custom user buttons).
All buttons have the same styles and attributes, but on screen the ones that are created programmatically have slightly bolder text.
I examined them in the layout inspector and they all have the same values for attributes like getTextSize (20), getTypefaceStyle (NORMAL), height, width, styles, ...
What could cause a different in boldness with the same values for those attributes?
Example of one XML Button :
<Button
android:id="#+id/button_autre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_selector2o"
android:text="Bonbons"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textStyle="normal"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_marginTop="20dp"
/>
Example of one created Button :
LinearLayout layout = (LinearLayout) view.findViewById(R.id.buttons_container);
LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutparams.setMargins(0, px, 0, 0); // px calculated to be 20dp
ContextThemeWrapper ndfContext = new ContextThemeWrapper(getActivity(), R.style.button_style_xml);
Button addbutton = new Button(ndfContext);
addbutton.setLayoutParams(layoutparams);
addbutton.setText("Bonbons");
addbutton.setBackgroundResource(R.drawable.background_selector2o);
layout.addView(addbutton);
Content of button_style_xml :
<resources>
<style name="ndftype_style" parent="AppTheme.NoActionBar">
<item name="android:background">#drawable/background_selector2o</item>
<item name="android:textAppearance">#style/TextAppearance.AppCompat</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">20sp</item>
<item name="android:layout_marginTop">20dp</item>
<item name="android:textAllCaps">false</item>
<item name="android:textStyle">normal</item>
</style>
</resources>
Result :
As is often the case, I got an idea while writing the question, thanks Canary Effect.
Despite all buttons having the same style and no font assigned to them anywhere, it seems Android was attributing a different default font to both kinds. I'm not sure why this is the case.
Adding
<item name="android:fontFamily">sans-serif</item>
on my programmatically created buttons solved the issue.
You might want to add it to your XML buttons too to be safe.
If you target API level < 16, running the following on all your buttons should work :
button.setTypeface(Typeface.DEFAULT);
I've been asked to match the look of an Alert Dialog in our app to the one used by the app's theme.
I managed to apply a style to all Alert Dialogs in the app using it as part of the app's theme, but there are situations where the style is not applying correctly.
It happens for example when the Alert Dialog contains a 'Single Choice List' as its' message.
The title looks fine, so is the background and the button bar, but the list itself is problematic.
At first, the radio buttons as well as their textual description were black colored, as if android is using the default color.
I somehow managed to color the radio buttons as I wish, by using these attributes:
<item name="android:colorControlNormal">#color/text_secondary</item>
<item name="android:colorControlActivated">#color/text_secondary</item>
But the text color still remains black, and I've tried EVERY possible text color attribute exposed by android.
It looks like this:
Now this is the full style defined for the Alert Dialogs:
<style name="GenericAlertDialog.Alter" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</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:windowTitleStyle">#style/DialogTitle</item>
<item name="android:textColor">#color/text_secondary</item>
<item name="android:textColorPrimary">#color/primary</item>
<item name="android:background">#color/window_background</item>
<item name="android:colorAccent">#color/accent</item>
<item name="android:textColorAlertDialogListItem">#color/text_secondary</item>
<!--<item name="android:textColorSecondary">#color/text_secondary</item>-->
<item name="android:colorControlNormal">#color/text_secondary</item>
<item name="android:colorControlActivated">#color/text_secondary</item>
</style>
This is my Theme definition:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">#color/window_background</item>
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/text_primary</item>
<item name="android:textColorSecondary">#color/text_secondary</item>
<item name="android:textColorHint">#color/text_hint</item>
<item name="android:buttonStyle">#style/GenericButton</item>
<item name="android:checkboxStyle">#style/GenericCheckBox</item>
<item name="android:alertDialogTheme">#style/GenericAlertDialog</item>
<item name="alertDialogTheme">#style/GenericAlertDialog</item>
</style>
This is the code I'm using to create a custom Alert Dialog:
AlertDialog.Builder dialogBuilder = null;
try
{
dialogBuilder = new AlertDialog.Builder(i_OwnerActivity, R.style.GenericAlertDialog_Alter);
LayoutInflater layoutInflater = i_OwnerActivity.getLayoutInflater();
// Inflate the dialog's custom title view and set it's text to the matching one to this class
View customTitleView = layoutInflater.inflate(R.layout.dialog_title, null);
TextView customTitleTextView = (TextView) customTitleView.findViewById(R.id.DialogTitleText);
// Set text of customTitleView
dialogBuilder.setCustomTitle(customTitleView);
// Create an event handler for clicking on the negative button
dialogBuilder.setNegativeButton(R.string.action_dialog_negative_cancel, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface i_Dialog, int i_Which)
{
// Do Something
}
});
} catch (Exception e)
{
LogUtils.logException(AlterDialogUtils.class, e);
}
return dialogBuilder;
And finally, here's the code I'm using to create an Alert Dialog with a 'Single Choice List':
dialogBuilder.setSingleChoiceItems(R.array.squelch_modes, m_InitialState, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// Do Something
}
});
What am I doing wrong? How can I change the color of the text?
It is also worth saying that I'm using AppCompat's AlertDialog.
Just found this old post with Google, but since there is no answer I will add what did the trick in my case:
remove the android: prefix from the textColorAlertDialogListItem in your alertdialog style
<item name="textColorAlertDialogListItem">#color/text_secondary</item>
I guess that's because of the parent being an AppCompat theme, but I am not sure about this. I still added both (with and without prefix) in my style...
I know it's probably late... but here's your answer: android.support.v7.app.AlertDialog in AppCompat support library use this layout by default (unless you provide your own adapter) for singleChoiceDialog:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?attr/textColorAlertDialogListItem"
android:gravity="center_vertical"
android:paddingLeft="#dimen/abc_select_dialog_padding_start_material"
android:paddingRight="?attr/dialogPreferredPadding"
android:paddingStart="#dimen/abc_select_dialog_padding_start_material"
android:paddingEnd="?attr/dialogPreferredPadding"
android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
android:drawableStart="?android:attr/listChoiceIndicatorSingle"
android:drawablePadding="20dp"
android:ellipsize="marquee" />
The attribute used to set this up in the theme is singleChoiceItemLayout so you can override it with your own layout to get whatever UI you want.
If you just want to change the text color just define the attribute textColorAlertDialogListItem as you can see from the layout it's the one used for android:textColor.
In general when I need something like this i go and look at the source code since it is available. The support libraries source code can be found here, while most of the framework source code can be find here.
I am a new android developer and I am working on a project where I want to have custom color themes as an option in the settings in one of my apps. I have four check boxes, call them checkBox1, checkBox2, checkBox3, checkBox4. I also have four custom styles in my styles.xml file, call them theme1, theme2, theme3, theme4. Here is my .java file coding regarding handling the onClick of these CheckBoxes:
checkBoxListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(blueCheckBox.isChecked()){
//Change theme to blue
}
if(redCheckBox.isChecked()){
//Change theme to red
}
if(greenCheckBox.isChecked()){
//Change theme to green
}
if(bwCheckBox.isChecked()){
//Change theme to Black & White
}
}
};
blueCheckBox.setOnClickListener(checkBoxListener);
redCheckBox.setOnClickListener(checkBoxListener);
greenCheckBox.setOnClickListener(checkBoxListener);
bwCheckBox.setOnClickListener(checkBoxListener);
}
How can I set the style of my entire application to the corresponding custom declared styles I have in the body's of these if statements? Thank you for your time, I appreciate any help you can offer.
for this you have to create some themes under /style/
For Example :
<resources>
<style name="LightTheme" parent="#android:style/Theme.Light">
</style>
<style name="BlackTheme" parent="#android:style/Theme.Black">
</style>
</resources>
and at java side you can use it like...
if (lightThemeCheckBox.isChecked()) {
getApplication().setTheme(R.style.LightTheme);
} else {
getApplication().setTheme(R.style.BlackTheme);
}
i hope this will help you.
for more visit this link:
http://www.anddev.org/applying_a_theme_to_your_application-t817.html
Hi You are able to supply the button for Checkbox by set the Background for the CheckBox as Follows :
Below the Code is for backgound xml file for Checkbox: Name as checkbox_selection.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/check_active_320" android:state_checked="true" android:state_focused="true"/>
<item android:drawable="#drawable/check_inactive_320" android:state_checked="false" android:state_focused="true"/>
<item android:drawable="#drawable/check_inactive_320" android:state_checked="false"/>
<item android:drawable="#drawable/check_active_320" android:state_checked="true"/>
Here You found that there was having the drawable resource such as check_active_320 when User checked on CheckBox and check_inactive_320 for Checkbox which is not checked.
the code to set the above xml to CheckBox as Follows:
<CheckBox
android:id="#+id/radioread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textread"
android:layout_alignBottom="#+id/textread"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:button="#drawable/checkbox_selection"
android:focusable="false" />
Here You can find the android:button as the value of our style.
i have an application , it has a button , when pressed it will fire a dialog with the default dialog shape , i wonder if i can change the default shape of dialog to an oval shape and also to apply special style to it ,
as explained in the images attached below :
1- the Default Dialog Shape:
2-The Oval Dialog Shape (which i try to achieve):
my dialoge code :
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
TextView text = (TextView) dialog.findViewById(R.id.dialog_text);
text.setText(Html.fromHtml(getString(R.string.text_4)));
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.pic);
Button dialogButton = (Button) dialog.findViewById(R.id.dialog_Button);
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
style code of default dialog:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="2dp" android:height="2dp" android:color="#B22222" />
<solid android:color="#FCE6C9" />
<padding android:left="2dp" android:top="2dp" android:right="2dp"
android:bottom="2dp" />
<corners android:radius="5dp" />
</shape>
i hope to have this done by code rather than using 9-patch image so it will be easy to control the dilaoge dimensions and adjust the text inside it as i neeed ,
Any advice will be appreciated , thanks .
Not exactly sure how to do this on android, but the approach I use in java is make the JFrame or JDialog rendered surface transparent and then draw my own custom shape inside. To make them transparent I use AWTUtilities.setWindowOpacity
On this article you will find another ideas, like capturing the desktop before the frame or dialog is rendered and using it to patch your frame:
http://today.java.net/pub/a/today/2008/03/18/translucent-and-shaped-swing-windows.html
More information:
http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/
Here you have an implementation, not for android but it may help:
http://www.codeproject.com/KB/java/shaped-transparent-jframe.aspx
Neat idea. On Android, to give an activity a transparent background, put this in your manifest file:
<activity android:name=".MyActivity" android:theme="#android:style/Theme.Translucent.NoTitleBar" />
Then, in your layout file for that activity, add this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myRoundBackground" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/myImageView"
android:layout_alignTop="#+id/myImageView"
android:layout_alignRight="#+id/myImageView"
android:layout_alignBottom="#+id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />
</RelativeLayout>
Then you can programmatically set the text on the TextView with the id myImageViewText .
Have a look at this quick action menu totorial. Try applying this tutorial with shape = oval background. I think you will get what you are trying to achieve.
I think a transparent background with image sliced in oval for will work for you. and place the button to left of the oval image.
THis method is similar to the one you are currently trying :
In your custom_dialog.xml give the relative layout an android:background="#drawable/ovaldialog" .
And in the ovaldialog.xml try giving the same parameters you are giving to edit the style by giving android:shape="oval" and also give the parameters to corners such as android:topLeftRadius="8dp" and android:bottomRightRadius="10dp" and play with those values until you get a desired kind of shape. To give the red color to the dialog give it a stroke of width 2/3dp and an android:color value.
Hope this helps