Set Alpha/Opacity of Layout
You can set the alpha on the layout and it's children (or any other view for that matter) using AlphaAnimation with 0 duration and setFillAfter option.
Example:
AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
alpha.setDuration(0); // Make animation instant
alpha.setFillAfter(true); // Tell it to persist after the animation ends
// And then on your layout
yourLayout.startAnimation(alpha);
You can use one animation for multiple components to save memory. And do reset() to use again, or clearAnimation() to drop alpha.
Though it looks crude and hacked it's actually a good way to set alpha on set ov views that doesn't take much memory or processor time.
Not sure about getting current alpha value though.
How to Set Opacity (Alpha) for View in Android
I just found your question while having the similar problem with a TextView. I was able to solve it, by extending TextView and overriding onSetAlpha
. Maybe you could try something similar with your button:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class AlphaTextView extends TextView {
public AlphaTextView(Context context) {
super(context);
}
public AlphaTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AlphaTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onSetAlpha(int alpha) {
setTextColor(getTextColors().withAlpha(alpha));
setHintTextColor(getHintTextColors().withAlpha(alpha));
setLinkTextColor(getLinkTextColors().withAlpha(alpha));
return true;
}
}
Change the opacity/transparency of a view and affect the child views too
That's because you are changing the backgound of the layout and not the layout itself. So instead of myRelativeLayout.getBackground().setAlpha()
, use this: myRelativeLayout.setAlpha(0.4f)
.
Set Alpha/Opacity of Layout of widget
As stated in Android documentation: You can NOT extend a class supported by RemoteView
. So, you can't use your AlphaLayout
class.
For any layout background, there are ten different drawables that you can use. You want your image to be transparent. This (as far as I know) can't be done giving what we have.
However, here is a good start:
- Use a LinearLayout instead of AlphaLayout.
- Add a Shape Drawable as its background:
android:background="@drawable/widget_background"
Then, set the background as you want. Here is a suggestion:
res/drawable/widget_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Corners">
<gradient android:startColor="#CC111111" android:endColor="#CC7f7f7f" android:angle="45"/>
<padding android:left="4dp" android:top="1dp" android:right="4dp" android:bottom="1dp" />
<corners android:radius="4dp" />
<stroke android:width="2dp" android:color="#FFAfAfAf"/>
</shape>
I believe this is the standard way of making widgets layouts. Good Luck!
How to change opacity to Layout in Android?
setAlpha work on ,api +11 you can use some thing like this for every version:
first.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
status = true;
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.3f);
alphaAnimation.setFillAfter(true);
alphaAnimation.setDuration(10);//duration in millisecond
layout.startAnimation(alphaAnimation);
}
});
UPDATE:
for getting back to original opacity you can do this:
AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f,1.0f);
alphaAnimation.setFillAfter(true);
alphaAnimation.setDuration(10);
layout.startAnimation(alphaAnimation);
How to decrease/Increase the Transparency of Layout not Views
try with:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="#66FF0000" >
You can see that:
android:color="#66FF0000" // Partially transparent red
I sugest you to check this infos:
Android set alpha opacity for a custom view
Android and setting alpha for (image) view alpha
Related Topics
Deadobjectexception on Android App
How to Make Edittext Not Focused When Creating Activity
How to Create Button Dynamically in Android
Eclipse with Android Sdk, Error: Java Heap Space
How to Get Ruby Generated Hmac for Sha256 That Is Url Safe to Match Java
Call External JavaScript Functions from Java Code
How to Pass an Arraylist to a Varargs Method Parameter
Java Hotspot(Tm) 64-Bit Server Vm Warning: Ignoring Option Maxpermsize
How to Remove a Specific Element from a JSONarray
Parcelable and Inheritance in Android
Get Button Coordinates and Detect If Finger Is Over Them - Android
Android:Table Has No Column Named "Variable Name" MySQL Database Error