get ImageView src programmatically - android

How to get ImageView src programmatically and set it in another ImageView
Drawable drawable = imageViewA....?
imageViewB.setImageDrawable(drawable);

You can do something like this:
Drawable drawable = imageViewA.getDrawable();
if(drawable != null){
imageViewB.setImageDrawable(drawable);
}

You can use setImageResource(int)
imageView.setImageResource(R.drawable.bg_image);

Instead of get drawable you do...
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
and set to new image
newImageView.setImageBitmap(bitmap);

Related

What is quick way to check if ImageView has bitmap assigned?

I need to quickly and safely check if an android.widget.ImageView currently has a Bitmap of non zero value attached. What is a quick way to do this.
Update my image view is set initially drawable set to "logo.png" so if I know that it is currently set to logo.png than I know that the bitmap has not yet been set. So how can I check if background drawable is currently logo.png
Thanks
Use getDrawable() method, if it return null then nothing assigned
if( mImageView.getDrawable() != null){
//Image Assigned
}else{
//nothing assigned
}
Assign ImageView to variable in onCreate:
someImage = (ImageView) findViewById(R.id.imageView);
Then a simple if-else statement:
Bitmap bm = ((BitmapDrawable) someImage.getDrawable()).getBitmap();
boolean hasDrawable = (bm != null);
if(hasDrawable)
{
hasImage();
}
else
{
Toast.makeText(AppSettings.this, "No Bitmap", Toast.LENGTH_SHORT).show();
}
Kotlin Extension Solution:
Add this to simplify checks and make it more readable
fun ImageView.hasDrawable() = drawable != null
and then call
myView.hasDrawable()
Get Bitmap attached to ImageView
Check this out.
It looks like you have to create a bitmap from the ImageView and check to see if it is null.
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
imageView.setDrawingCacheEnabled(false);

Android - setBackgroundResource on Button

I have some buttons which i set the background with setBackgroundResource(R.drawable.cal_box_center); and the problem I'm having is that my background is a gradient which has this banding effect (annoying) and I've read that in order to remove this you'll need to set the Bitmap.Config.ARGB_8888. I looked into the API and the way to do this is to use decodeStream etc, but how can i use setBackgroundResource and still set the Config to ARGB_8888?
Thanks in advance.
You can use this code snippet:
// create button
Button btn = new Button(getApplicationContext());
//decode the resource(You can also use decodeStream and other decode method of
//BitmapFactory)
Bitmap btm = BitmapFactory.decodeResource(getResources(), R.drawable.cal_box_center);
//create another copy of your bitmap and specify Config
Bitmap newBtm = btm.copy(Bitmap.Config.ARGB_8888, true);
//use your newBtm to create a BitmapDrawable
BitmapDrawable btmDrwble = new BitmapDrawable(newBtm);
// finally set the drawable as your button's background
btn.setBackgroundDrawable(btmDrwble);

How to show image using ImageView in Android

I am looking for the way to assign image src to image view control. I read few example and they says something src="#drawable\image" but didn't understand this, also I want to assign image src at runtime by java code also want to apply default image in XML.
If you want to display an image file on the phone, you can do this:
private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageBitmap(BitmapFactory.decodeFile("pathToImageFile"));
If you want to display an image from your drawable resources, do this:
private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageResource(R.drawable.imageFileId);
You'll find the drawable folder(s) in the project res folder. You can put your image files there.
You can set imageview in XML file like this :
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/imagep1" />
and you can define image view in android java file like :
ImageView imageView = (ImageView) findViewById(R.id.imageViewId);
and set Image with drawable like :
imageView.setImageResource(R.drawable.imageFileId);
and set image with your memory folder like :
File file = new File(SupportedClass.getString("pbg"));
if (file.exists()) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap selectDrawable = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
imageView.setImageBitmap(selectDrawable);
}
else
{
Toast.makeText(getApplicationContext(), "File not Exist", Toast.LENGTH_SHORT).show();
}
In res folder select the XML file in which you want to view your images,
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/imagep1" />
shoud be #drawable/image where image could have any extension like: image.png, image.xml, image.gif. Android will automatically create a reference in R class with its name, so you cannot have in any drawable folder image.png and image.gif.
Drag image from your hard drive to Drawable folder in your project and in code use it like this:
ImageView image;
image = (ImageView) findViewById(R.id.yourimageviewid);
image.setImageResource(R.drawable.imagename);

How to use relativelayout.setBackgroundDrawable() with a bitmap?

I have a RelativeLayout object and want to dynamically change the background image with a dynamically created Bitmap object (it changes its color dynamically).
I saw that when I wanted to update the background image of the RelativeLayout object that I can only choose setBackgroundDrawable() which requires a Drawable object as a parameter.
My question is, how can I convert the dynamically created Bitmap object into a Drawable object?
BitmapDrawable(obj) convert Bitmap object into drawable object.
Try:
RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
Drawable dr = new BitmapDrawable(bit);
(view).setBackgroundDrawable(drawable);
I hope this will help you.
You can do it by this way
Drawable drawable = new BitmapDrawable(bitmap);
RelativeLayout r;
r = (RelativeLayout) findViewById(R.id.relativelayout1);
ll.setBackgroundDrawable(drawable);
Try this,
Drawable drawable = new BitmapDrawable(bitmap);
RelativeLayout rl=(RelativeLayout) findViewById(R.id.main1);
Bitmap myImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Drawable dr = new BitmapDrawable(myImage);
rl.setBackgroundDrawable(dr);
Drawable d = new BitmapDrawable(bitmap);
//Create Reference for RelativeLayout
RelativeLayout layout = (RelativeLayout) findViewById(R.id.rl);
//set Background to layout reference using the following method
Drawable drawable = getResources().getDrawable(R.drawable.bg);
layout.setBackground(drawable);
Note: R.id.rl is id of RelativeLayout
R.drawable.bg id the Image in drawable folder

Create drawable from layout

Is there a way to generate a drawable object from a layout?
In fact, I need a cropped part of my initial layout, and my idea is to transform the layout into a drawable object and then to crop drawable.
A simple version:
Bitmap snapshot = null;
Drawable drawable = null;
yourView.setDrawingCacheEnabled(true);
yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW); //Quality of the snpashot
try {
snapshot = Bitmap.createBitmap(yourView.getDrawingCache(), sizes and stuff); // You can tell how to crop the snapshot and whatever in this method
drawable = new BitmapDrawable(snapshot)
} finally {
yourView.setDrawingCacheEnabled(false);
}

Categories

Resources