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.
Related
I want to have an ImageView component within my activity and use Java to change the source.
The content provider is something like this:
content://com.spotify.music/image/OTIzMzlhZThkNjA2YWVkODRhZjY3NDU2YzgxNDM4MmM3NzlhMTU4MA%3D%3D%0A
I want to be able to change this image source whenever I want in the code.
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.
I'm in process of replacing JFace TableViewer with NatTable. In my implementation with TableViewer I have images in cells, and I have implementation of ILabelProvider which is aware of how to get image for concrete state of object at runtime. So I call ILabelProvider.getImage(element) from ColumnLabelProvider.
In NatTable I know the way to add an image via registring configAttribute against configLabel. And for configAttribute I should explicitly tell what image to use. Surely I can create label for every state, register image for every label and use ConfigLabelAccumulator to tie it all togeather. But the amount of images is quite huge, and moreover I don't want to duplicate this logic. So is there more appropriate way for such a case? Just delegating to existing ILabelProvider?
In cases where you have quite some dynamic for retrieving the Image the label solution is insufficient (e.g. when thinking about a shop system with different images per row object). In such cases you typically implement a custom ImagePainter and implement the code the determine the Image to use in the getImage() method.
The following snippet can be used as a starting point where you only need to implement your custom logic to determine the Image to use. This way you only need to register one ImagePainter. In NatTable this is also done for some static images like for example the TreeImagePainter.
public class ContentDependentImagePainter<T> extends ImagePainter {
IRowDataProvider<T> dataProvider;
public ContentDependentImagePainter(IRowDataProvider<T> dataProvider) {
this.dataProvider = dataProvider;
}
#Override
protected Image getImage(ILayerCell cell, IConfigRegistry configRegistry) {
// get the row object
T rowObject = dataProvider.getRowObject(cell.getRowIndex());
Image result = null;
// perform your custom logic to determine the Image
return result;
}
}
I want to be able to add a text-messaging balloon every time the user revives data from a HttpGet, I want it so that it looks nearly identical to the default Android text messaging UI. I'm fine with all the code, I just need a way to create the UI and create another text balloon every time data comes back from a HttpGet request.
Thanks ever so much, for the answering this questions and I'm sure there's an easy way to do it, yet I've found no way by using the 'ole Google.
I am doing something similar for my app am doing the following to achieve it:
You will need a 9-Patch-Image (a stretchable PNG, see here) that represents the bubble. You want to make the part stretchable that does not include the corners of the bubble. You can create the bubbles using an image editor of your choice (I'd recommend a vector graphics editor like Inkscape). Then use the 9-Patch editor included in the Android Developer Tools to transform the PNG image into a 9-Patch PNG.
Create a custom layout file for one bubble. Create a textview inside it, and add your bubble as a background resource. (android:background)
Use an arraylist with a custom adapter to inflate and fill your items.
So far, this will give you identical bubbles as background for all messages.
If you want to get fancy, you can create different bubbles for participants, and use the setBackgroundResource method in your Adapter to set the correct background.
Further, if you wish to align them left or right, like in the message app, you will need to add spacers to the left and right of your TextView in the layout file. I used FrameLayouts with a fixed width. Make sure to set their visibility to GONE.
As with swapping the different bubble colors, just set the visibility of the left/right spacer.
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.