Changing TextView Opacity in Android
Asked Answered
I

7

14

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());
}
Impassion answered 12/1, 2012 at 4:48 Comment(0)
C
42

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

Cochin answered 12/1, 2012 at 5:35 Comment(0)
T
12

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
Tire answered 12/1, 2012 at 6:1 Comment(1)
I used a variation of this to change the text alpha without also changing the background alpha, thanks!Lyons
M
10

Now there is a Attribute on XML as

android:alpha="0.5"

to change the Opacity through the Layout

Modestia answered 14/10, 2020 at 3:38 Comment(0)
M
6

change method to following

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch)
{
    classicText.setAlpha((float)(gameSelector.getProgress())/(float)(seekBar.getMax()));
}
Mendiola answered 12/1, 2012 at 5:0 Comment(5)
this is not working AFAIK. i have tried it..TextView does not have method named setAlpha().check it before answering!Tire
check method developer.android.com/reference/android/view/…Mendiola
This is correct reference but you would not be able to use such method for textview or view in your eclipse directly.please check yourself.instead,you need to customize textview and then to use that class in your app.Tire
I will, when I would get some time, but if it is case, it may be due to TextView has an additional property, so changes in textview itself might not get effect, in this case, we need to set text color, by getting rgb and alpha component and set new alpha in the setTextColor method.Mendiola
Yes...Thats true! but after all,we need to use custom class for the same to make the code and approach easier!Tire
B
3

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

Bricky answered 27/4, 2020 at 18:30 Comment(0)
C
1

I think this is a simple way to set alpha or opacity programmatically for TextView. By using function withAlpha after getTextColors from textView.

// alpha value between 0..255
// 0 for transparent
classicText.setTextColor(classicText.textColors.withAlpha(100))
Cyprinid answered 2/9, 2021 at 7:32 Comment(2)
This is an excellent and concise solution. It does not rely on changing the alpha of the view itself, which also affects any background or compound drawable you might have set on the TextView. Also it does not rely on setTextColor(int) which only sets a single color. Instead this uses the ColorStateList you may already have applied to the TextView and directly adjusts the alpha channel for ALL the stateful colors it may have. No need to make a separate ColorStateList, just reuse the same one!Apotropaic
Thanks bro @TonyChan, your explaination is good, clear and easy to understandCyprinid
B
0

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);
Butters answered 12/1, 2012 at 5:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.