Style applied to an AlertDialog not working correctly - java

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.

Related

Remove dim duration of AlertDialog

The AlertDialog's dim effect can be disabled with:
alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Also, the dim amount can be changed by using:
alertDialog.getWindow().setDimAmount(0.0f);
However, whenever I call these functions, the change seems to be animated for a short duration (a long value between 100-200). Is there a way to make the change instant?
When trying to replace the standard dim with a mimicking View, the screen seems to flash due to the animation of the dim change.
Edit: Here's the base application theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
The AlertDialog was created from an AlertDialog.Builder (click listeners have been added later):
dialogBuilder=new AlertDialog.Builder(this);
dialogBuilder.setTitle("Title").setIcon(null).setMessage("Description")
.setNeutralButton("Neutral",null).setNegativeButton("Cancel",null).setPositiveButton("Ok",null);
alertDialog=dialogBuilder.create();
add this to your theme:
<item name="android:backgroundDimEnabled">false</item>
And control the theme strictly from code
If you don't want to control the theme of the dialog by the main theme
create a subclass for the dialog only and use it the following way
<style name="DialogTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
<item name="android:backgroundDimEnabled">false</item>
</style>
dialogBuilder = new AlertDialog.Builder(this, R.style.DialogTheme);
If you want all the dialogs to have disabled dim then you can do something like this:
Go to styles.xml, then in your main theme add:
<item name="alertDialogTheme">#style/MyAlertDialogTheme</item>
Then in below that add another theme (the one we are referencing above)
<style name="MyAlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:backgroundDimEnabled">false</item>
</style>
Also, if you want a very low dim you can change it to:
<item name="android:backgroundDimAmount">0.05</item>
As the other answers here suggest, I figured that it's better to just disable the dim effect altogether. I didn't want to override all the AlertDialogs, so it seems to work well enough with just using a background View and fading it in/out with the AlertDialog's setOnShowListener and setOnDismissListener.
alertDialog=dialogBuilder.create();
alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){
#Override public void onShow(DialogInterface dialog) {
background.setAlpha(0f);background.setVisibility(View.VISIBLE);background.animate().alpha(1f).setDuration(dur);
}
});
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener(){
#Override public void onDismiss(DialogInterface dialog){
background.animate().alpha(0f).withEndAction(runnable_background_gone);
}
});

Can I change my android application style at runtime?

I want to change the manifest style when the user chooses a setting in the preferences file or settings. I have a setting in my apk which includes a list of preferences with three entries {Style_1, Style_2, Style_3}. I want to change the style color, like primaryColor, when the user clicks a style. How can I do this?
Yes you can do this easily, I do it all the time.
Just call this method before setContentView like this:
setTheme(R.style.Theme);
setContentView(R.layout.activity_layout);
Now what I do is that i take a static int variable in the app constants and change it according to my theme. Then i do something like this
//This is in my constants file
public static int CURRENT_THEME = R.style.AppTheme;
//This is in my onCreate of every Activity.
setTheme(Constants.CURRENT_THEME);
setContentView(R.layout.activity_layout);
Hope this helps.
Yes you can set Theme like this:
activity.setTheme(R.style.theme_large);
activity.setTheme(R.style.theme_small);
<style name="theme_large">
<item name="main_background">#drawable/background_red</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="button_light">#color/button_light</item>
</style>
<style name="theme_small">
<item name="main_background">#drawable/background_red</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="button_light">#color/button_light</item>
</style>
Create Two Style
setTheme(darkTheme ? R.style.AppThemeDark : R.style.AppThemeLight);
<style name="AppThemeDark">
<item name="main_background">#drawable/background_dark</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="button_light">#color/button_dark</item>
</style>
<style name="AppThemeLight">
<item name="main_background">#drawable/background_light</item>
<item name="colorPrimaryDark">#color/colorPrimaryLight</item>
<item name="button_light">#color/button_light</item>
</style>
I think, setting theme after a button click or option choosen could be harsh for an Activity to handle.
What I am suggesting you is to reload your activity on button click but before that simply save the style_name , the user want to apply in internal memory or Shared Preferences. You can apply shared preference simply looking at here.
At the start of onCreate of your activity, apply that fetching part of shared preferences and apply the theme as user has indicated. This would help you to preserve the theme for that user until s/he uninstall that application or clears the app data.
If s/he is using the application for first time, the stored style_name string would be null so load your application with default theme.
You can simply reload your activity on button click by using the below code:
public void onClick (View v){
Intent intent = getIntent();
finish();
startActivity(intent);
}
Hope it helps !!

Changing theme of Android Application via CheckBox onClick

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.

How do you change the action bar text color without Sherlock?

I don't want to use the Sherlock action bar, but I do want to change the text color. I know how to change the actual text
ActionBar actionBar = getActionBar();
actionBar.setTitle("Whatever");
but I don't know how to change the color. I have also tried this:
<style name="ActionBar.Solid.Example" parent="#android:style/Widget.Holo.Light.ActionBar.Solid">
<item name="android:background">#drawable/ab_background_textured_example</item>
<item name="android:backgroundStacked">#drawable/ab_stacked_solid_example</item>
<item name="android:backgroundSplit">#drawable/ab_background_textured_example</item>
<item name="android:progressBarStyle">#style/ProgressBar.Example</item>
<item name="android:textColor">#android:color/white</item>
</style>
From the "Android Holo Colors Generator", I added the line
<item name="android:textColor">#android:color/white</item>
but it didn't do anything.
Any help? Thanks!
Seems you must use
<item name="android:actionBarStyle">#style/Widget.MyApp.ActionBar</item>
Here is solved the similiar problem:
Action Bar Sherlock 4 title text colour - where am I going wrong?

Android action bar overlay & hidden by default in xml

I need to have the action bar hidden on application start. Then, after some user action, it has to show up again overlaying the content. Currently I turn it off in a custom style by setting <item name="android:windowActionBar">false</item>, and then call
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
getActionBar().hide();
in onCreate() of the activity.
That seems a little awkward though. Can I do all of it in xml configs?
I don't know about the hiding part, but you cna style the ActionBar as an overlay in your apps theme like this:
<style name="AppTheme" parent="#style/Theme.AppCompat">
<item name="android:windowNoTitle">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowActionBarOverlay">true</item>
</style>

Categories