Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment buttons color change in Lollipop

I would like my Fragments to look consistent with the rest of the app and color palette which I applied so I would like to change the colors not only of title, but also of positive/negative buttons: enter image description here

I tried to do this like this, but unfortunetaly it doesn't work:

public void onStart() {
        super.onStart();
        Dialog d = getDialog();
        int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
        View divider = d.findViewById(dividerId);

        if(currentapiVersion< Build.VERSION_CODES.LOLLIPOP) {
            divider.setBackgroundColor(getResources().getColor(R.color.accent));
            LinearLayout ll = (LinearLayout) d.findViewById(R.id.dialog_background);
            ll.setBackgroundResource(R.drawable.backrepeat_reversed);
        }
        if(currentapiVersion == Build.VERSION_CODES.LOLLIPOP) {
            int buttonsId = d.getContext().getResources().getIdentifier("android:id/negativeButton", null, null);
            Button b = (Button) d.findViewById(buttonsId);
            b.setTextColor(getResources().getColor(R.color.accent));
        }
        int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
        TextView tv = (TextView) d.findViewById(textViewId);

        tv.setTextColor(getResources().getColor(R.color.accent));
    }

How to change the color of these buttons? Maybe it is possibile to do it in the whole application through styles.xml file?

like image 794
fragon Avatar asked Jan 12 '15 23:01

fragon


People also ask

How do I change the button color in alert dialog?

You can just create a style and apply that theme on the dialog box. So whenever you want to change the color of AlertDialog box, just change color in styles. xml and all the dialog boxes will be updated in the whole application.

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

How do I change the color of my button on Kotlin?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.

What is the difference between dialog & DialogFragment?

DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs. It allows the FragmentManager to manage the state of the dialog and automatically restore the dialog when a configuration change occurs.


3 Answers

To change the color of the buttons and checkboxes displayed in Alert dialog you can edit:

values-21/styles.xml

<style name="AppTheme" parent="...">
  ...
  <item name="android:timePickerDialogTheme">@style/AlertDialogCustom</item>
  <item name="android:datePickerDialogTheme">@style/AlertDialogCustom</item>
  <item name="android:alertDialogTheme">@style/AlertDialogCustom</item>
</style>

<style name="AlertDialogCustom" parent="android:Theme.Material.Light.Dialog.Alert">
  <item name="android:colorPrimary">#00397F</item>
  <item name="android:colorAccent">#0AAEEF</item>
</style>

Which will make the dialogs have the selected color for the buttons text and checkboxes:

DialogDate picker

like image 27
peceps Avatar answered Sep 30 '22 18:09

peceps


If you can create the dialog using AlertDialog the following worked for me:

public class DialogTest extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity()).setTitle("Test")
                .setMessage("This is a dialog.")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                }).show();
    }

    @Override
    public void onStart() {
        super.onStart();
        ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.RED);
        ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.RED);
    }
}
like image 56
Jared Rummler Avatar answered Sep 30 '22 16:09

Jared Rummler


values-21/styles.xml

<style name="AppTheme" parent="...">
  ...
  <item name="alertDialogTheme">@style/AlertDialogCustom</item>
</style>


<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/buttons_color</item>
</style>

In Lollipop and above you shouldn't use android: prefix

like image 20
Konstantin Konopko Avatar answered Sep 30 '22 18:09

Konstantin Konopko