RelativeLayout layout = findViewById(R.id.relativeLayout); Drawable drawable = getResources().getDrawable(R.drawable.background); layout.setBackgroundDrawable(drawable);
RelativeLayout layout = new RelativeLayout(context); Drawable drawable = ContextCompat.getDrawable(context, R.drawable.background); layout.setBackgroundDrawable(drawable);In this example, we create a new RelativeLayout programmatically and retrieve a Drawable from our resources folder using the ContextCompat.getDrawable method. We then set the background of the RelativeLayout to this drawable using the setBackgroundDrawable method. The package library for this method is android.widget.