'how to set drawable background of edit text programmatically

Hello every one I am new in the android studio I have a picture in my drawable folder and I want it to save the picture as a background of the edit text programmatically for this I write a code bellow but its not working and it showing a red line error

here is my code.

edit_text.setBackground(R.drawable.background1);

any idea for this how I will write it please don't suggest me another post as I am new here.



Solution 1:[1]

You should retrieve the background drawable using ContextCompat.getDrawable method:

edit_text.setBackground(ContextCompat.getDrawable(this, R.drawable.background1));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lpizzinidev