I'm using the custom ListPreference
. And I need to change its alpha
.
This is code :
public class IconPreference extends ListPreference {
private Drawable mIcon;
private TextView textView;
private TextView titleView;
private float alpha;
public IconPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public IconPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
setLayoutResource(R.layout.icon_pref);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.IconPreference, defStyle, 0);
mIcon = a.getDrawable(R.styleable.IconPreference_icon);
}
@Override
public void onBindView(View view) {
super.onBindView(view);
textView = (TextView) view.findViewById(android.R.id.summary);
titleView = (TextView) view.findViewById(android.R.id.title);
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
if (imageView != null && mIcon != null) {
imageView.setImageDrawable(mIcon);
}
}
public void setIcon(Drawable icon) {
if ((icon == null && mIcon != null) || (icon != null && !icon.equals(mIcon))) {
mIcon = icon;
notifyChanged();
}
}
public Drawable getIcon() {
return mIcon;
}
@Override
public void setValue(String value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
super.setValue(value);
} else {
String oldValue = getValue();
super.setValue(value);
if (!TextUtils.equals(value, oldValue)) {
notifyChanged();
}
}
}
public void changeSummary(String name)
{
textView.setText(name);
titleView.setText(name);
notifyChanged();
}
public float getAlpha(){
return alpha;
}
public void setAlpha(int opacity){
if (this.alpha != opacity) {
alpha = opacity;
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
textView.startAnimation(alpha);
textView.invalidate();
titleView.startAnimation(alpha);
titleView.invalidate();
}
// And then on your layout
}
}
But that is NullPointerException
in the textView.startAnimation(alpha);
string.
This code I'm using for the custom RelativeLayout
with many child elements and that's works properly for it.
How can I implement the alpha
to the ListPreference
?
I solved this by using the flag in the onBindView of the custom ListPreference :
@Override
public void onBindView(View view) {
super.onBindView(view);
textView = (TextView) view.findViewById(android.R.id.title);
titleView = (TextView) view.findViewById(android.R.id.summary);
this.view = view;
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
if (imageView != null && mIcon != null) {
imageView.setImageDrawable(mIcon);
}
if (transparancyFlag )
{
textView.setTextColor(context.getResources().getColor(R.color.translucent_black));
titleView.setTextColor(context.getResources().getColor(R.color.translucent_black));
}
}
and I'm using the setAlpha() method for the setting the transparancy to the specified ListPreference:
public void setAlpha(boolean flag){
if (flag)
transparancyFlag = true;
else transparancyFlag = false;
notifyChanged();
// And then on your layout
}