To set Background:
RelativeLayout layout =(RelativeLayout)findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
Is the best way to do it?
RelativeLayout layout =(RelativeLayout)findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
Is the best way to do it?
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:
Inside the app/res/your_xml_layout_file.xml
I'm using a minSdkVersion 16 and targetSdkVersion 23 The following is working for me, it uses ContextCompat.getDrawable(context, R.drawable.drawable);
Instead of using:
layout.setBackgroundResource(R.drawable.ready);
Rather use:
layout.setBackground(ContextCompat.getDrawable(this, R.drawable.ready));
getActivity()
is used in a fragment, if calling from a activity usethis
You can also set the background of any Image:
now, inside any function like onCreate, onResume
Try this:
and for API 16<: