Skip to content

Instantly share code, notes, and snippets.

@bblackbelt
Created September 18, 2017 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bblackbelt/de4d1250f3a4c72b920123e70c522613 to your computer and use it in GitHub Desktop.
Save bblackbelt/de4d1250f3a4c72b920123e70c522613 to your computer and use it in GitHub Desktop.
public class BooleanToVisibilityConverter implements IValueConverter {
public static final String CONVERTER_NAME = "ToVisibility";
public static final String PARAMETER_INVERT_TAG = "invert";
public static final String PARAMETER_INVISIBLE_TAG = "invisible";
@Override
public String getBindingName() {
return CONVERTER_NAME;
}
@Override
public Object convert(Object value, Class<?> targetType, Object parameter, Locale culture) {
boolean result = false;
if (value != null && value instanceof Boolean) {
result = ((Boolean) value);
} else if (value != null) {
result = true;
}
boolean invert = false;
boolean invisible = false;
if (parameter instanceof String) {
String localParameter = (String) parameter;
if (localParameter.toLowerCase().contains(PARAMETER_INVERT_TAG)) {
invert = true;
}
if (localParameter.toLowerCase().contains(PARAMETER_INVISIBLE_TAG)) {
invisible = true;
}
}
if (invert) {
result = !result;
}
return result ? View.VISIBLE : (invisible) ? View.INVISIBLE : View.GONE;
}
@Override
public Object convertBack(Object value, Class<?> targetType, Object parameter, Locale culture) {
return IBinding.noValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment