简体   繁体   中英

Android : Alert dialog background color not changing behind positive button

I am having trouble with the custome AletDialog with edittext in in, when i set the background color to dialog the color at the back does change except the part that includes the positive and Negative buttons.

在此处输入图像描述

layout file for the dialoge:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorNoteBook"
    android:padding="16dp">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="INSTRUCTIONS:" />

    <com.example.recical.ui.MyLab.LinedEditText
        android:id="@+id/editText_instructions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView2"
        android:layout_alignParentStart="true"
        android:layout_marginTop="20dp"
        android:background="@color/colorNoteBook"
        android:ems="10"
        android:gravity="start|top"
        android:inputType="textMultiLine"
        android:minLines="8" />
</RelativeLayout>

Java Class

public class CustomDialog extends AppCompatDialogFragment {

    private EditText et_in_dialog;
    private CustomDialogListener listener;

    @NonNull
    @Override**strong text**
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());

        LayoutInflater inflater=getActivity().getLayoutInflater();
        View view=inflater.inflate(R.layout.layout_dialog,null);
        builder.setView(view)
//                .setTitle("Instructions")
               .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
               .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                         String instructions=et_in_dialog.getText().toString();
                         listener.applyText(instructions);
                    }
                });
        et_in_dialog=view.findViewById(R.id.editText_instructions);
        return builder.create();

    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        try {
            listener=(CustomDialogListener)context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()+"must implement CustomDialogListener");
        }
    }

    public interface CustomDialogListener{            // this the interface tabbed activity will call
        void applyText(String instructions);
    }
}

How am i supposed to style that area behind Ok and custom buttons? I want to make it fully yellow

Add Theme in style


    <style name="AlertDialog" parent="Base.Theme.AppCompat.Dialog.Alert">
        <item name="android:windowBackground">@color/sub_primary_color</item>
    </style>

And build with Style like follow

val builder = AlertDialog.Builder(activity, R.style.AlertDialog)
val dialog = builder.create()
dialog.show()

it work for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM