I have a bitmap drawable image to be displayed in ImageView, below is my code:
BitmapDrawable bitmapGrass = (BitmapDrawable)
getResources().getDrawable(R.drawable.grass);
bitmapGrass.setTileModeX(TileMode.REPEAT);
I want to set that bitmap as background for ImageView, like this:
ImageView bgGrass = (ImageView) findViewById(R.id.image_grass);
bgGrass.setBackground(bitmapGrass);
Now the problem is, the method setBackground() is added in API level 16, but I want my app to support API level 4. I know I can use this code:
bgGrass.setBackgroundDrawable(bitmapGrass);
But the method setBackgroundDrawable() is deprecated. The other way to set the background is using the method setBackgroundResource(int), but the required parameter is integer (not drawable).
Is there any way to implement that bitmap without using deprecated method, perhaps converting the bitmap drawable into resource?
Thanks!
Actually it's fine to call setBackgroundDrawable though it's deprecated in newer platforms. you can check the sdk level at runtime and call the specific method:
if(Build.VERSION.SDK_INT >= 16) {
//call setBackground()
} else {
//call settBackgroundDrawable()
}
Related
I am adding backwards compatibility for an app and setTint is used on a Drawable retrieved from a LayerDrawable. Code is below.
Drawable background = layerDrawable.getDrawable(0);
background = DrawableCompat.wrap(background);
DrawableCompat.setTint(background.mutate(), color);
This works the first time but if I then try and change it again afterwards, it doesn't change. Please note, this is the case for Android SDK < 21. 21 and above it works.
So after reading the documentation like a good boy for the DrawableCompat.wrap method, I realised I needed to add the "wrapped" drawable back into the LayerDrawable.
int id = layerDrawable.getId(0);
Drawable background = layerDrawable.getDrawable(0);
background = DrawableCompat.wrap(background);
layerDrawable.setDrawableByLayerId(id,background);
DrawableCompat.setTint(background.mutate(), color);
Can anyone tell me the right way of using getDrawable() , I recall last time I used it was just fine with one parameter, but now I am getting this method is deprecated error, anyone? Bellow is the snippet I am trying to use
private void toggleUi() {
ImageView imageView = (ImageView) findViewById(R.id.silent_icon);
Drawable silentImage;
if(silent) {
silentImage = getResources().getDrawable(R.drawable.silent_on);
} else {
silentImage = getResources().getDrawable(R.drawable.silent_off);
}
imageView.setImageDrawable(silentImage);
}
Will be glad if anyone can help thanks.
Since API 22 you should call ContextCompat.getDrawable(context, R.drawable.***) instead of getResources().getDrawable(R.drawable.***).
You can see the documentation of ContextCompat from the support library for more details.
I want to change the background of my framelayout (which holds all my pages!) by:
FrameLayout fl = (FrameLayout)findViewById(R.id.container);
fl.setBackground(getResources().getDrawable(R.drawable.juraquiz_app_background));
but apparently I cant. Is there a way to do it, so its compatible with APIs lower than 16?
for API's lower that 16 you can use setBackgroundDrawable
Use different methods for different APIs:
final Drawable drw = getResources().getDrawable(R.drawable.juraquiz_app_background);
if(android.os.Build.VERSION.SDK_INT < 16)
{
fl.setBackgroundDrawable(drw);
}
else
{
fl.setBackground(drw);
}
You will need to add an annotation to your method (or to your class, if you prefer) to get rid of Lint warnings:
#SuppressLint("NewApi")
how can I set an xml background file that placed in drawable for a view without using #SuppressLint("NewApi") ?
for example I created a drawable xml file for my textview
when I call TV.setBackground(getResources().getDrawable(R.drawable.tv_pic_back)); eclipse automatically add #SuppressLint("NewApi") at the first of my function.
how can I use that without #SuppressLint("NewApi") ?
I have a class where I put a lot of code to handle the different APIs, so that you use one line of code for one API, and another line of code for another API.
public static void setBackgroundDrawable(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(drawable);
}
else {
view.setBackgroundDrawable(drawable);
}
}
This will still give you a warning because setBackgroundDrawable is deprecated, but if you instead would use setBackground(drawable) for all versions then your application would crash on API levels lower than Jelly Bean (API 16).
However, in your case all you need to do is actually setBackgroundResource(R.drawable.tv_pic_back); because you don't need to get the drawable from the resource id yourself, Android will do that for you if you give it your resource id when you call the right method.
The Android developer reference will tell you which methods are deprecated and which methods are implemented in which API version.
The setBackgroundDrawable() method of the View class in is now deprecated in android SDK API level 16.
The new method is setBackground() but of course it's only available in API 16.
How can I workaround it if I want my application to be compatible with previous API levels ? (at least API 14)
The goal is to eliminate any warnings and an #SupressWarnings is not a solution for me.
The usual way is this one:
if (android.os.Build.VERSION.SDK_INT >= 16)
setBackground(...);
else
setBackgroundDrawable(...);
On the other hand you could use reflections:
try {
Method setBackground = View.class.getMethod("setBackground", Drawable.class);
setBackground.invoke(myView, myDrawable);
} catch (NoSuchMethodException e) {
setBackgroundDrawable(myDrawable);
}
IMO a warning is better than having to catch an exception and an unnecessary reflection.
You can use
viewobj.setBackgroundResource(drawble_object);
Setting bitmap to ImageView.
ImageView imageView = (ImageView) findViewById(R.id.imageSlice4);
imageSlice4.setBackground(new BitmapDrawable(getResources(), slicedImagesArrayList.get(3)));