Home > DeveloperSection > Forums > Trying to set opacity (alpha) for a view in android
Anurag Sharma
Anurag Sharma

Total Post:40

Points:280
Posted on    January-13-2016 11:09 PM

 Android Android  Android Styles 
Ratings:


 1 Reply(s)
 250  View(s)
Rate this:
I am using a  button :
<Button 
     android:text="Submit" 
     android:id="@+id/Button01" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content">
</Button>
In my onCreate() event, I am calling Button01 like this:

setContentView(R.layout.main);
View Button01 = this.findViewById(R.id.Button01);
Button01.setOnClickListener(this);
There is a background in the application, and I want to set an opacity on this submit button. How can I set an opacity for this view? Is it something that I can set on the java side, or can I set in the main.xml file?

On the java side I tried Button01.mutate().SetAlpha(100), but it gave me an error


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Supported
Posted on    January-13-2016 11:24 PM

Hi Anurag,

You just need to extend TextView and override onSetAlpha:

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;
  }
}

Don't want to miss updates? Please click the below button!

Follow MindStick