To set Background:
RelativeLayout layout =(RelativeLayout)findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
Is the best way to do it?
androidbackgrounddrawablejava
RelativeLayout layout =(RelativeLayout)findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
Is the best way to do it?
Best Answer
layout.setBackgroundResource(R.drawable.ready);
is correct.Another way to achieve it is to use the following:
But I think the problem occur because you are trying to load big images.
Here is a good tutorial how to load large bitmaps.
UPDATE:
getDrawable(int ) deprecated in API level 22
getDrawable(int )
is now deprecated in API level 22. You should use the following code from the support library instead:If you refer to the source code of ContextCompat.getDrawable, it gives you something like this:
More details on ContextCompat
As of API 22, you should use the
getDrawable(int, Theme)
method instead of getDrawable(int).UPDATE:
If you are using the support v4 library, the following will be enough for all versions.
You will need to add the following in your app build.gradle
Or using ResourceCompat, in any API like below: