Search code examples
androidandroid-edittextcolordrawable

EditText Backcolor crashes


Can anyone advise why the simple program keeps on crashing?

    EditText editText1 = (EditText) findViewById(R.id.editText1);
    View.OnClickListener onClickListener_button1 = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            ColorDrawable drawable = (ColorDrawable)editText1.getBackground();

        }

    };

Solution

  • As uncovered in comments, you are casting an InsetBackground to a ColorBackground, which fails.

    To get proper results, you need to set android:background="@color/initial_color" to the EditText in xml.