The question:
To set Background:
RelativeLayout layout =(RelativeLayout)findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);
Is the best way to do it?
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
layout.setBackgroundResource(R.drawable.ready);
is correct.
Another way to achieve it is to use the following:
final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ready) );
} else {
layout.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
}
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:
ContextCompat.getDrawable(context, R.drawable.ready)
If you refer to the source code of ContextCompat.getDrawable, it gives you something like this:
/**
* Return a drawable object associated with a particular resource ID.
* <p>
* Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned
* drawable will be styled for the specified Context's theme.
*
* @param id The desired resource identifier, as generated by the aapt tool.
* This integer encodes the package, type, and resource entry.
* The value 0 is an invalid identifier.
* @return Drawable An object that can be used to draw this resource.
*/
public static final Drawable getDrawable(Context context, int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 21) {
return ContextCompatApi21.getDrawable(context, id);
} else {
return context.getResources().getDrawable(id);
}
}
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.
ContextCompat.getDrawable(context, R.drawable.ready)
You will need to add the following in your app build.gradle
compile 'com.android.support:support-v4:23.0.0' # or any version above
Or using ResourceCompat, in any API like below:
import android.support.v4.content.res.ResourcesCompat;
ResourcesCompat.getDrawable(getResources(), R.drawable.name_of_drawable, null);
Method 2
Try this:
layout.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
and for API 16<:
layout.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ready));
Method 3
RelativeLayout relativeLayout; //declare this globally
now, inside any function like onCreate, onResume
relativeLayout = new RelativeLayout(this);
relativeLayout.setBackgroundResource(R.drawable.view); //or whatever your image is
setContentView(relativeLayout); //you might be forgetting this
Method 4
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 use this
.
Method 5
You can also set the background of any Image:
View v;
Drawable image=(Drawable)getResources().getDrawable(R.drawable.img);
(ImageView)v.setBackground(image);
Method 6
If you use AndroidX, you should use:
AppCompatResources.getDrawable(context, R.drawable.your_drawable)
Previous methods listed are deprecated.
Method 7
If your backgrounds are in the drawable folder right now try moving the images from drawable to drawable-nodpi folder in your project. This worked for me, seems that else the images are rescaled by them self..
Method 8
Use butterknife to bind the drawable resource to a variable by adding this to the top of your class (before any methods).
@Bind(R.id.some_layout)
RelativeLayout layout;
@BindDrawable(R.drawable.some_drawable)
Drawable background;
then inside one of your methods add
layout.setBackground(background);
That’s all you need
Method 9
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.ready));
else if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1)
layout.setBackground(getResources().getDrawable(R.drawable.ready));
else
layout.setBackground(ContextCompat.getDrawable(this, R.drawable.ready));
Method 10
try this.
int res = getResources().getIdentifier("you_image", "drawable", "com.my.package");
preview = (ImageView) findViewById(R.id.preview);
preview.setBackgroundResource(res);
Method 11
Give a try to ViewCompat.setBackground(yourView, drawableBackground)
Method 12
setBackground(getContext().getResources().getDrawable(R.drawable.green_rounded_frame));
Method 13
Try this code:
Drawable thumb = ContextCompat.getDrawable(getActivity(), R.mipmap.cir_32);
mSeekBar.setThumb(thumb);
Method 14
I have 4 fragment for splashscreen.
i can change background by change fragment by this code
screen_main=(ConstraintLayout)view.findViewById(R.id.screen_main);
screen_main.setBackground(getContext().getResources().getDrawable(R.color.bg_screen1));
Method 15
Inside the app/res/your_xml_layout_file.xml
- Assign a name to your parent layout.
- Go to your MainActivity and find your RelativeLayout by calling the findViewById(R.id.”given_name”).
- Use the layout as a classic Object, by calling the method setBackgroundColor().
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0