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

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.

Related

Transfer image from one ImageButton to another

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.

Override color value in color.xml file

Is there a way to override a color that is already set in your color.xml file? For example - I set the background color on the layout to backgroundColor = "#color/background" The value of background = #000000
Can I override the actual color of background in Java and set the #color/background to say - #FFFFFF?
It's possible, but rather costly. #color/background is equivalent to R.color.background, where R is your generated class. At runtime, you could use reflection to make the fields writable, then change them. I'm not really convinced it would work how you want.
A different idea would be to use themes for the various colors you want, possibly letting the user switch between them. That'd keep your design and logic separate.
You can use .setBackgroundColor(getResources().getColor(R.color.myColor)); to set the background color in your Java code. This should override the xml background when it is set in the android:background xml tag, it did on my tested table row at least.

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.

SMS Balloons / Long Chat Boxes

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.

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