Reputation: 225
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());
}
Upvotes: 14
Views: 51849
Reputation: 316
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))
Upvotes: 1
Reputation: 1012
Now there is a Attribute on XML as
android:alpha="0.5"
to change the Opacity through the Layout
Upvotes: 10
Reputation: 239
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
Upvotes: 3
Reputation: 19250
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
Upvotes: 12
Reputation: 29199
change method to following
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch)
{
classicText.setAlpha((float)(gameSelector.getProgress())/(float)(seekBar.getMax()));
}
Upvotes: 6
Reputation: 30855
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
Upvotes: 42
Reputation: 1671
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);
Upvotes: 0