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);
Related
I would like to change (map) white color to blue color. How can I do this on ImageView in Android? I tried setColorFilter by PorterDuff / LightingColorFilter / ColorMatrixColorFilter but I can't figure out how to set it up. There is a transparent background around the image.
Try to use Background Tint color function in xml or java code.
or
I would suggest another way. Take on frame layout and place view and then image view.
white portion in image should be transparent use PNG image.
and you can give color to base view color which ever you want.
I have been working on android on some time, but there is a common problem which i face many times. The problem is there are multiple view where in i assign the view a background drawable for background, such as:
As can be seen in the above example the background drawable for both the views is same but only difference in the both background drawables xml file is the solid and stroke colour. Is there any other better way to do this where i just create a generic background drawable and change the colour of the components from xml and id not possible in xml instead of creating a new drawable each time for each solid colour and stroke colour variation.
No this is not possible in XML. However, it is possible using Java.
ShapeDrawable shapeDrawable = (ShapeDrawable) ContextCompat.getDrawable(getActivity(), R.drawable.name);
shapeDrawable.getPaint().setColor(ContextCompat.getColor(mContext,R.color.colorToSet))
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 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.
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);