Transfer image from one ImageButton to another - java

I have two ImageButtons, each with a different image within. I need to find Java code which essentially allows ImageButton2 to display ImageButton1's image. I've been flailing around, but I think the magic command should be something like this:
ImageButton2.setImageResource( ImageButton1.getImageAlpha() );
ImageButton2.setImageResource( ((BitmapDrawable) ImageButton1.getDrawable()).getBitmap() );
int id = ImageButton1.getId();
ImageButton2.setImageResource( R.drawable.(id) );
But none of these compile. The answer has to be something like this:
ImageButton2.setImageResource( ImageButton1.getImageResource() );
Anyone see the solution? I've been working on this all day. Thanks.

That can be achieved following way:
Drawable drawable = imageButton1.getDrawable();
Drawable mutatedDrawable = drawable.mutate();
imageButton2.setImageDrawable(mutatedDrawable);
By default, drawables would be shared. If you want changes made one drawable not to affect to the same drawable attached to another view, then you have to explicitly mutate() the drawable.

Related

Android: How to insert images into edit text

I am trying to create a blog type app, at the moment I am using an image button to get replaced with an image using the picasso libaray. Although, how would I go about adding/inserting image(s) from my gallery to an edit text. I am not sure how to go about doing this:
Although the image isn't very clear in describing your question, you can however use the below code to add a drawable in EditText.
<EditText
...
android:drawableLeft="#drawable/some_drawable"
android:drawablePadding="10dp"/>
Try using imageView.setCompountDrawablesWithInstrinsicBounds(drawable, null, null, null)
This way you can dynamically pass in the images you want to display from a gallery.

Android Change image depending on a global variable

the question is simple.
I have 2 images in PNG format (logo1.png and logo2.png) in the project. Currently the project is loaded (in a imageview) the logo1.png, but I would do, depending on the value of a variable load the logo1.png or load logo2.png in imageview control.
The project currently has 20 Activitys with this picture (each with its own layout in XML), I will not be changing code on the 20 screens, it could do with a simple instruction to verify the value of the variable, but would have to make change in the 20 screens.
wonder if there is no way to do that depending on the value of a variable, change the image in the ImageView.
will be able to access the value of the variable from the same XML?
Thanks in advance.
I'm a bit confused, but I'll give it a shot.
Yes, you can have a global variable where you define which image will load. But from my understanding, you would need to change the code in the activities that load that image to make them load the image dynamically via the code-behind.
I suspect it would be something like this:
Get info from the database indicating what value to load.
SetImageToLoad(someValue)
In each class that loads the image, you'll need to retrieve the value that you previously set in Step #2.
public class HelperClass
{
int resIDOfImageToLoad = 0;
public static void SetImageToLoad(String imageName)
{
if(imageName.equals("abc"))
{
resIDOfImageToLoad = R.id.abc;
}
else if(imageName.equals("xyz"))
{
resIDOfImageToLoad = R.id.xyz;
}
}
public static int GetResourceIDOfImageToLoad()
{
return resIDOfImageToLoad;
}
}
Then in the class that needs to load the image, you would call something like this
ImageView myImage = (ImageView)findViewById(...)
myImage.setImageResource(HelperClass.GetResourceIDOfImageToLoad());
If i understand you correctly. Create a new XML View of the image e.g. logo.xml and use it in all 20 Views and when you want to change that image change only in the logo.xml.
Assuming you have a base Activity class, you can define a method getLogo() in the base class that would return the png (or the filename or whatever suits you). Then just call that method when inflating the layout.
Initially, you'd have to change all the Activities, but after that you just need to change the base class if you decide to change the logic that chooses which image to show. (If this is not what you intended, please clarify your question).
I would recommend using Style or Themes. Read over the section that talks about inheritance, and declare a separate Style for each logo. You can then reuse the Style in each one of your XML files.
If you decide to programmatically determine which image to use, you can declare a static method that can be used application wide to determine which logo to use in each Context, then setImageResource() accordingly.

Remove the image from a imageview Android [duplicate]

This question already has answers here:
How to clear an ImageView in Android?
(18 answers)
Closed 4 years ago.
I'm trying to make an ImageView that holds a gallery of images. By touching the user request to load the next image. If the next image isn't found in the server or takes time to load I need the old image to be empty.
setVisibility(View.GONE) or setVisibility(View.INVISIBLE) don't work for me because when invisible/gone I stop the onTouch() detecting (and the user is locked to current image).
How can I make the ImageView to load a empty bitmap or clear (remove) current bitmap?
I always use
imageView.setImageDrawable(null);
Try:
imageView.setImageResource(0);
This will set the image view to use no resource.
From what I've noticed, the "working" or not of certain method when clearing image depends on the method used to populate ImageView.
So if you set img.setImageBitmap(bmp) then to clear you should use img.setImageBitmap(null).
When you img.setImageResource(resId) then to clear you should use img.setImageResouce(0).
Etc.
Certainly imageView.setImageResource(0) works. It has never failed for me and I have used it many times.
setImageResource is usually passed the reference R.drawable,(the reference for the picture), which is stored as an int, but displayed in the R.java class as a hex value, 0xf2fs... So assuming this reference exist it will show a picture, if you later pass that same imageview a reference which does not exist the old picture will no longer show. So, if you pass it 0, or 5 or an int which does not match a resource referenced in your R.java class it will remove the picture completely from the src of the imageView. So if you are passing 0 to the old reference of the imageView.

How to remove the background of a (regular) view (Android)

I have the following code:
View view = new View(this);
view.setBackgroundDrawable(...);
...
And here I want to remove that background.
Just turn it back as it was before.
I tried these and failed:
view.setBackgroundDrawable(null);
view.setBackgroundColor(0xFF000000);
view.setBackgroundColor(0x00000000);
Any more ideas?
view.setBackgroundDrawable(null); should work.
You may try one of these:
v.setBackgroundColor(Color.WHITE);
//or
v.setBackgroundColor(Color.parseColor("#ff0000")); //whatever color
Make sure the view you're applying the background to is the correct instance.
That's because view.setBackgroundColor(int) expects a color resource not an "actual" integer value. Try declaring it in your colors.xml, see this. However, I'm not quite sure what you mean by "removing" the background. If you want it to have the original value, then I suggest you store the original drawable (using getBackground()) somewhere. Otherwise you will most likely lose formatting, since most default backgrounds in Android are Drawable resources (selectors), not simple colors.

EditText background color

I have an Activity that consists only of a huge EditText, full screen.
To match with my app's colours, i changed the background color of the EditText. But although it occupies the whole screen, it only changes the first line. Everytime i press enter i go to a new line and that line gets the background i want.
What propertie can i set in order to do this? I just can't see it!
I would wrap the EditText with a View and set the background color on the View.
In order to change the background of those Views that come with a background already, its better to provide your own image, and then use patch9 for proper resizing
I hope that helps
#everyone:
While searching for a solution for this same kind of problem i found this page. While reading the solutions i thought to myself: "There must be a better way to do this, instead of making your own styles and widgetborders, etcetera, etcetera, ....
Therefore, i present to you: the 1 LINE OF CODE SOLUTION!!! No trolling.
<your-widget-component-that-has-a-background-color>.getBackground().setColorFilter(Color.<your-desired-color>, PorterDuff.Mode.MULTIPLY);).
It works like this:
"getBackground()" fetches the background from the component
"setColorFilter" will call a filtering on the background image itself
"Color." determines what color you want to pass onto the filter
"PorterDuff.Mode." sets the kind of manipulation you would like to do with the given color and the fetched background image. People with knowledge of image editing software (GIMP, Photoshop, ...) might recognise these modes. Basically, each mode has a certain effect on how the color is applied to the background image. to simply "override" the color of the image, while preservind it's gradients, borders and such, use "MULTIPLY"
I haven't profoundly read the PorterDuff class documentation, but honestly, ain't it a powerful bit of code?
With kind regards,
How to set the background color of an EditText widget:
editText.setBackgroundColor(Color.RED);

Categories

Resources