Can't change AlertDialog items colors - java

I created multiple alert dialogs in different activities. Now all of sudden the AlertDialog items appear in red !
Here is my code
AlertDialog.Builder builder = new AlertDialog.Builder(this, MyDialogStyle);
builder.setTitle("Select Method :");
final String[] choices = new String[]{"Weight Based", "Body Surface Area"};
builder.setItems(choices, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// the user clicked on choice[which]
switch (choices[which]) {
case "Weight": {
create_item(false);
break;
}
case "Body Surface Area": {
create_item(true);
break;
}
}
}
});
builder.show();
styles.xml (v21)
<style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#000000</item>
<item name="android:textColor">#000000</item>
<item name="android:textColorPrimary">#000000</item>
<item name="android:colorPrimary">#000000</item>
<item name="android:colorAccent">#000000</item>
<item name="android:textColorAlertDialogListItem">#000000</item>
<item name="android:textColorSecondary">#000000</item>
<item name="android:text">#000000</item>
<item name="android:textAppearanceListItemSecondary">#000000</item>
<item name="android:textAppearance">#000000</item>
<item name="android:color">#000000</item>
styles.xml
<style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#000000</item>
<item name="android:textColor">#000000</item>
<item name="android:textColorPrimary">#000000</item>\
<item name="android:textColorAlertDialogListItem">#000000</item>
<item name="android:textColorSecondary">#000000</item>
<item name="android:text">#000000</item>
<item name="android:textAppearance">#000000</item>
</style>
I tried changing the parent style or deleting it, but both give the same bright red result ! any ideas ?

try use this style ,,, hope it'll work for you
<style name="AlertDialogCustom" parent="#android:style/Theme.Dialog">
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">10sp</item>
</style>

Related

Changing color of the Status bar with colorPrimaryDark is not working

SO, I'm trying to change the color of the status bar, but for some reason, it's not working. My API level is 29. None of the colors from the color.xml file work. Here's my code:
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowContentTransitions">true</item>
</style>
<style name="tab_text" parent="#android:style/TextAppearance.Widget.TabWidget">
<item name="android:fontFamily">#font/cerebrisans_medium</item>
</style>
</resources>
colors.xml:
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#03A9F4</color>
<color name="colorAccent">#D81B60</color>
</resources>
When I change from .DarkActionBar to .noActionBar I get error in my splash screen activity. Line: "getSupportActionBar().hide();"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen_);
getSupportActionBar().hide();
statusBarColor();
private void statusBarColor() {
try {
if (Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(this, R.color.yellow));
}
} catch (Exception e) {
e.getMessage();
}
}

Android customizing DialogFragment issue [duplicate]

This question already has answers here:
How to set round corners for a custom Dialog?
(2 answers)
Closed 2 years ago.
I'm trying to customize layout of a DialogFragment my idea was to create a rounded corners dialog with buttons text color set to colorPrimary:
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
public class ExitDialogFragment extends DialogFragment {
ExitDialogListener listener;
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_exit, null))
.setMessage(R.string.exit_dialog_title)
.setPositiveButton(R.string.confirm_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.onDialogPositiveClick(ExitDialogFragment.this);
}
})
.setNegativeButton(R.string.back_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.onDialogNegativeClick(ExitDialogFragment.this);
}
});
return builder.create();
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
try {
listener = (ExitDialogListener) context;
} catch (Throwable e) {
throw new ClassCastException("Must implement NoticeDialogListener");
}
}
public interface ExitDialogListener {
void onDialogPositiveClick(DialogFragment dialog);
void onDialogNegativeClick(DialogFragment dialog);
}
}
The dialog layout (dialog_exit.xml) is defined in this way:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_dialog"
android:orientation="vertical"
android:theme="#style/DialogTheme">
</LinearLayout>
Here I defined DialogTheme (styles.xml):
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
</style>
<style name="Launcher" parent="AppTheme">
<item name="android:windowBackground">#drawable/launch_screen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="colorControlNormal">#color/colorPrimary</item>
<item name="colorControlActivated">#color/colorPrimary</item>
</style>
<style name="UriEditText" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">#color/text_color_clear</item>
<item name="colorControlActivated">#color/text_color_clear</item>
</style>
</resources>
And here's the dialog background (rounded_dialog.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/white" />
<corners android:radius="20dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
Nothing is applied. The dialog is just rectangular and buttons color is set to AppTheme colorAccent.
Use CardView as container layout in rounded_dialog.xml.
Here is rounded_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/colorPrimary"
app:cardCornerRadius="16dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<!--Put a Constraint Layout(or any other container) here with required views-->
</androidx.cardview.widget.CardView>

Text Color Not Changing in AlertDialogue

I am trying to apply custom color in AlertDialogue. Its changing text color as well background color in it but not changing SingleChoiceItems text color. You can see it black color in it with page number like below image.
I am using style like below code
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="android:textColor">#color/TextColorLite</item>
<item name="android:background">#color/colorPrimaryDarkLite</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">?attr/MainAccentColor</item>
<item name="android:buttonBarButtonStyle">#style/DialogButtonStyle</item>
</style>
<style name="DialogButtonStyle" parent="#style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">?attr/TextColor</item>
<item name="android:textStyle">bold</item>
</style>
Java Code For Dialoge
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity(), R.style.AlertDialogTheme);
builder.setTitle("Go to Page:");
builder.setSingleChoiceItems(mPageOptions, mPageIndx - 1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int item) {
mOptionDiag.dismiss();
mPageIndx = item + 1;
updateQuotesListServer();
updatePageInfo();
}
});
builder.setNegativeButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
});
mOptionDiag = builder.create();
mOptionDiag.show();
What I should do for change this black text color ?
Thanks
I have got solved adding this items in style.
<!--For List Text-->
<item name="textColorAlertDialogListItem">#color/TextColorLite</item>
<!--For Radio-->
<item name="android:textColorSecondary">#color/TextColorLite</item>
Thanks
In your resources folder open styles.xml and add a custom theme like for example this:
<style name="radiobuttonstyle" parent="Material.Drawable.RadioButton">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#color/md_green_900</item>
<item name="android:textSize">20sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#color/md_green_400</item>
<item name="colorControlNormal">#color/md_green_900</item>
<item name="colorControlActivated">#color/md_green_400</item>
</style>
and in your RadioButton in your layout add it as the theme of your RadioButton like this:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioyes"
android:theme="#style/radiobuttonstyle"
android:text="Yes"
android:textColorLink="#color/md_light_green_900"
android:textColorHighlight="#color/md_green_900"
android:textColor="#color/md_light_green_900" />
PS: that is my own custom radio button so it should work.

How to automatically apply material primary and accent colors to dialog

I have custom theme with changed primary and accent colors. But AlertDialog is not picking up those colors automatically when I create dialog using:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
This is what I get - dialog with default colors:
And this is what I would like to have - dialog with custom colors:
I can create custom AlertDialog theme and apply it during AlertDialog creation:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppTheme_Dialog);
I would like to skip calling constructor with theme parameter.
Is there a way to force custom colors on all themes without deriving them in styles, including AlertDialog base theme?
Minimup API level I have to support is 15.
My activity code
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog();
}
public void dialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Dialog");
builder.setMessage("Lorem ipsum dolor ...");
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
builder.show();
}
}
My customized style
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.Dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
My colors
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
Add this in your the App theme:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- your style -->
<item name="alertDialogTheme">#style/AppTheme.Dialog</item>
And then,
import android.support.v7.app.AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);

How to change the text color of a menu item in Theme.AppCompat.Light.DarkActionBar theme?

I have black background with white letters.
Any ideas?
Thanks.
<style name="PacerTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:itemBackground">#android:color/black</item>
<item name="android:itemTextAppearance">#style/myCustomMenuTextApearance</item>
</style>
<style name="myCustomMenuTextApearance" parent="#style/Widget.AppCompat.Light.ActionBar.Solid">
<item name="android:textColor">#android:color/white</item>
</style>
Normal:
Click in menu button, and show :
Modify the style like this:
<style name="PacerTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:itemTextAppearance">#style/myCustomMenuTextApearance</item>
</style>
<style name="myCustomMenuTextApearance" parent="#android:style/TextAppearance.Widget.IconMenu.Item">
<item name="android:textColor">#android:color/white</item>
</style>
For "myCustomMenuTextApearance" style use for parent
#android:style/TextAppearance.Widget.IconMenu.Item
instead of
#style/Widget.AppCompat.Light.ActionBar.Solid
I found the solution, program change text color, as follows:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
for(int i=0; i<menu.size(); i++)
{
MenuItem mi = menu.getItem(i);
String title = mi.getTitle().toString();
Spannable newTitle = new SpannableString(title);
newTitle.setSpan(new ForegroundColorSpan(Color.WHITE), 0, newTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mi.setTitle(newTitle);
}
return true;
}
The XML is as follows:
<style name="PacerTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:itemBackground">#android:color/black</item>
</style>

Categories

Resources