I was just wondering if there was a way to change the opacity of the background image for a View (ie. TextView, etc.).
I know that I can set the background image like this:
android:background="#drawable/my_drawable_image"
Or I can set a specific background colour with an alpha setting like this:
android:background="#10f7f7f7"
Is there a way I can control the opacity (set the alpha) if I'm setting the background as a drawable image? And I want to do this in the XML Layout. I already know that I could grab the Drawable object and programmatically set the alpha, but I want to see if I can do it in the layout.
I ended up just going with the programmatical solution, since it doesn't look like it can be done via the XML layouts.
Drawable rightArrow = getResources().getDrawable(R.drawable.green_arrow_right_small);
// setting the opacity (alpha)
rightArrow.setAlpha(10);
// setting the images on the ImageViews
rightImage.setImageDrawable(rightArrow);
This might make your Work simpler
View backgroundimage = findViewById(R.id.background);
Drawable background = backgroundimage.getBackground();
background.setAlpha(80);
Alpha Values 0-255, 0 means fully transparent, and 255 means fully opaque
from: This Answer
You can also use XML to change the transparency:
android:alpha = "0.7"
The value of alpha ranges from 0 to 1
You can embed the image in xml, so you'll be able to see it in the Graphical Layout
<LinearLayout
style="#style/LoginFormContainer"
android:id="#+id/login_layout"
android:orientation="vertical"
android:background="#drawable/signuphead">
And change the code like this to make it transparent:
Drawable loginActivityBackground = findViewById(R.id.login_layout).getBackground();
loginActivityBackground.setAlpha(127);
The answer you gave didn't exactly answer the question you asked. Here's what I did.
Drawable login_activity_top_background = getResources().getDrawable(R.drawable.login_activity_top_background);
login_activity_top_background.setAlpha(127);
LinearLayout login_activity_top = (LinearLayout) findViewById(R.id.login_activity_top);
login_activity_top.setBackgroundDrawable(login_activity_top_background);
Related
Im use an image for my layout background is there any way to opaque it in xml?
I use android:alpha="2" but nothing happened!
try this:
View backgroundImage = findViewById(R.id.background_image);
Drawable background = backgroundImage.getBackground();
background.setAlpha(80);
Android SDK provides the following icons.
Is there a way to set a color to those .. and if possible, how to do so?
<ImageView
android:id="#+id/share_button"
style="#style/IconNav"
android:src="#android:drawable/ic_menu_share"/>
<ImageView
android:id="#+id/bookmark_button"
style="#style/IconNav"
android:src="#android:drawable/ic_input_get"/>
UPDATE
After doing a complete refresh of the project, it turns out the tint attribute in Xml did the trick.
For the short answer
.. this is the solution that worked for me - adding the property to the ImageView xml:
android:tint="#color/grass_dark"
The answer from #goldenb is a thorough run through of the different ways to solve for this, so am marking that one as the answer.
You can indeed use a tint as a way of changing an ImageView's colour, BUT you should be reminded that the android:tint will always be applied on top of the original colour.
as stated by blogger danlew
ImageView's tint mixes the tint color with the original asset. What you want is for the tint color to take over entirely; instead it
applies the tint on top of the existing color. So, for example, if the
source asset is black, and you want it to be #77FFFFFF (a translucent
shade of white), you'll actually end up getting that shade of white
with a black background beneath it.
android:tint is limited to ImageView. You want to be able to tint any Drawable in any View.
One possible alternative would be for you to use android ColorFilter
According to the official documentation:
A color filter can be used with a Paint to modify the color of each pixel drawn with that paint. This is an abstract class that should never be used directly.
There are lots of more or less complex things you can do with ColorFilter but how can you apply this then?
One simple example from another so question is:
//White tint
imageView.setColorFilter(Color.argb(255, 255, 255, 255));
or
imageView.setColorFilter(ContextCompat.getColor(context,R.color.COLOR_YOUR_COLOR))
Or a more complete answer here in SO from here
ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);
// we can create the color values in different ways:
redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );
You should check these links if you want to learn more
SO - What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?
setColorFilter()
Fast Android asset theming with ColorFilter
SO-Modifying the color of an android drawable
You can use a bitmap with a tint. Add this to your drawables folder.
ic_input_get_colored.xml :
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#android:drawable/ic_input_get"
android:tint="#color/yourDesiredColor"/>
How to make background opacity of the View (for ex. EditField )with transparent, infliction, subtraction "Screen" like it is in Photoshop?
I know if I set background to "#android:color/transparent" for EditField then white background will be visible, but not main backgrounf of the layout
I need to implement this one:
You can't do it simply.. not by any code.. the only way to do that is to get a drawable .png image(you need photoshop to create it) that has this shape(semi tranaparent on the frame ans fully transparent inthe center with rounded corners).. make it with small width .. and use 9patch tool for stretching (so you can get flexible width)...
I got a picture that I want to use.
I set it as following:
ImageView menu = new ImageView(this);
menu.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
menu.setImageResource(R.drawable.menu);
They annoying thing is that I get white pixels on the sides of it cause I want to keep the aspect of the pic.
I can stretch the image by using menu.setScaleType(ScaleType.FIT_XY); but that will make the person on it look really fat. The picture is dark and the pixels are white so they do show quite well. :/
Is there I way I can first apply black color and then the image above that color?
To set a background color to any view on android you can use android:background attribute in layout xml or by calling setBackgroundColor(int id) in java code.
But if you really want just to set the image in bounds you can give a try to android:scaletype="centerCrop"
Using set BackgroundColor will also remove any padding, borders and what not attached to the object.
If that is the result you want, that is a reasonable approach.
If blasting the objects current style will cause problems, I would look to use CSS to set the background color and change the css styles through code if things are happening after page load.
Consider using a border around the image that is colored the way you want to hide what is underneath?
I am using an image as the background of a linear layout in my android program.I need to set opacity for this.Can anyone tell me how can I do this?..The image that is translucent doesnt show the opacity,doesn't know the reason though.
Thanks in advance for your valuable comments
If you set the background of the LinearLayout programatically this shouldn't be any problem.
What you are looking for is the Drawable.setAlpha(int alpha) method. From a personal project:
ImageView image = (ImageView) row.findViewById(R.id.list_icon);
image.setImageResource(R.id.something);
image.setAlpha(110);
Not exacly the same, but maybe you get the point.
You are using a drawable as the background of your layout. The challenge here is to get the variable representing your drawable. This is done here:
In the activity to the layout:
Resources res = getResources();
Drawable background = res.getDrawable(R.drawable.*the id*);
// The layout which are to have the background:
LinearLayout layout = ((LinearLayout) findViewById(R.id.*you get it*));
// Now that we have the layout and the background, we ajust the opacity
// of the background, and sets it as the background for the layout
background.setAlpha( *a number* );
layout.setBackgroundDrawable(background);
And it should work. At least it did work for me.