Home » Android » Changing TextView Opacity in Android

Changing TextView Opacity in Android

Posted by: admin May 14, 2020 Leave a comment

Questions:

So I’m trying to dynamically change the opacity of a TextView in my android app. I have a seekbar and as I slide the thumb to the right, the TextView I have layered under it should start becoming transparent. When the thumb reaches about half way across the seekbar, the text should be completely transparent. I’m trying to use the setAlpha(float) method inherited from View on my TextView, but Eclipse is telling me setAlpha() is undefined for the type TextView. Am I calling the method in the wrong way? Or is there another way to change the opacity?

Here’s my code (classicText is the TextView, gameSelector is the seekbar):

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch){
    classicText.setAlpha(gameSelector.getProgress());
}
How to&Answers:

you can set the alpha like this

int alpha = 0;
((TextView)findViewById(R.id.t1)).setTextColor(Color.argb(alpha, 255, 0, 0));

as the alpha you getting from the seekbar that will be set into the text color

Answer:

This worked for me:

1. Create class AlphaTextView.class:

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));
    getBackground().setAlpha(alpha);
    return true;
  }    
}

2. Add this instead of using TextView to create a textview in your xml:

...
   <!--use complete path to AlphaTextView in following tag-->
   <com.xxx.xxx.xxx.AlphaTextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="sample alpha textview"
         android:gravity="center"
         android:id="@+id/at"
         android:textColor="#FFFFFF"
         android:background="#88FF88"
        />
...

3. Now you can use this textview in your activity like:

at=(AlphaTextView)findViewById(R.id.at);

at.onSetAlpha(255); // To make textview 100% opaque
at.onSetAlpha(0); //To make textview completely transperent

Answer:

change method to following

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch)
{
    classicText.setAlpha((float)(gameSelector.getProgress())/(float)(seekBar.getMax()));
}

Answer:

It may be late but if anyone is looking for this now, all you have to do is:

textView.setAlpha();

in brackets enter number between 0 and 1

Answer:

View.setAlpha (float xxx);

range of xxx – 0 – 255, 0 being transparent and 255 being opaque.

int progress = gameSelector.getProgress();
int maxProgress = gameSelector.getMax();
float opacity = (progress / maxProgress)*255;
classicText.setAlpha(opacity);