Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the color of dialog multichoice items

I'm creating an alert dialog that has a couple multi choice items on it, and I want to change the color of the checkbox to better fit with my app's theme. Here is a picture:

AlertDialog

You can see how the multi choice items are in their default green color, whereas my EditText has the correct accent color. Here is my code for creating the dialog:

public class OrderDrinkDialogFragment extends DialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {

        AlertDialog.Builder builder = new AlertDialog.Builder(DrinkDetailActivity.activity);

        //set dialog title
        builder.setTitle(R.string.dialog_title);

        builder.setView(R.layout.order_drink_dialog);

        //MultiChoice for double shot and mixed with soda
        builder.setMultiChoiceItems(R.array.dialog_checkboxes,null,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked)
                    {

                    }
                });

        Dialog dialog = builder.create();

        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

        return dialog;
    }
}

Here is my XML file that I'm setting the layout from (order_drink_dialog.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/username"
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/order_dialog_name_padding"
        android:layout_marginEnd="@dimen/order_dialog_name_padding"
        android:layout_marginBottom="@dimen/order_dialog_name_padding"
        android:hint="@string/dialog_name_hint"
        android:theme="@style/AppTheme"
        />
</LinearLayout>

I've searched online extensively, and all I can find are people using alternatives like CheckBox and CheckedTextView but none of those give the look or the proper functionality that I want. I want the exact same textbox, but with a background that is my app's accent color instead of the default green.

like image 625
mithunm93 Avatar asked Dec 25 '22 22:12

mithunm93


1 Answers

Set alert dialog theme as the second parameter of AlertDialog.Builder constructor. On my emulator(Nexus 5), following code works fine.

    public class OrderDrinkDialogFragment extends DialogFragment
    {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState)
        {

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.MyAlertDialogTheme);

Also you need to add accent color to theme.

<style name="MyAlertDialogTheme" parent="@android:style/Theme.Material.Light.Dialog">
    <item name="colorAccent">#E91E63</item>
    <item name="android:colorAccent">#E91E63</item>
</style>
like image 69
muumuutech Avatar answered Jan 05 '23 06:01

muumuutech