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.
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);
I have a full screen layout and I want to use an opaque blurry version of image or bitmap as the background. So I need to know:
1) how to set a image/bitmap as background of a layout like LinearLayout or RelativeLayout. Is there a different approach for bitmap vs. image?
2) How to make it look blurry.
RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
Drawable dr = new BitmapDrawable(bit);
(view).setBackgroundDrawable(drawable);
There is no difference between bitmap and image .. just go through this link for more details ..
Android - ImageView: setImageBitmap VS setImageDrawable
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 have ToggleButtonwith an image setted in android:background property. This background image fits all the background.
I would like to center a smaller background image inside a large ToggleButton avoiding to enlarge the image to fit all the background.
Does someone have a clue?
If you want to lay text on top of an image, you're probably going to have to set the image in code instead of XML. Something like:
Resources r = getResources();
Drawable myimage = r.getDrawable(R.drawable.myimage);
Button mybutton = findViewById(R.id.mybutton);
myimage.setBounds(0, 0, myimage.getIntrinsicWidth(), myimage.getIntrinsicHeight());
mybutton.setCompoundDrawables(myimage, null, null, null);
Ok, I got it:
First, we create our ToggleButton in our XML layout. Think our drawable is a selector which contains two (for selected and unselected states) images smaller than the background of the ToggleButton:
<ToggleButton
android:id="#+id/messageSeeMoreToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff=""
android:textOn=""
android:background="#drawable/selector_wall_toggle_see_more_button"
android:contentDescription="#string/conversation_see_more_Button_description" />
Then, in our class Activity, we find our ToggleButton as usual:
viewHolder.messageSeeMoreToggleButton = (ToggleButton) view.findViewById(R.id.messageSeeMoreToggleButton);
Now, we are going to change the appearance of the drawable, using StateListDrawable.setLevel. We can adjust the value in setLevel for better result:
StateListDrawable stateListDrawable = (StateListDrawable) viewHolder.messageSeeMoreToggleButton.getBackground();
stateListDrawable.setLevel(3000);
So, we will have our image centered inside the ToggleButton background, avoiding to enlarge the image.
I hope it helps someone.
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);