androidx.databinding.BindingAdapter Java Examples

The following examples show how to use androidx.databinding.BindingAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BindingAdapters.java    From PopularMovies with MIT License 6 votes vote down vote up
@BindingAdapter("items")
public static void setItems(ChipGroup view, List<Genre> genres) {
    if (genres == null
            // Since we are using liveData to observe data, any changes in that table(favorites)
            // will trigger the observer and hence rebinding data, which can lead to duplicates.
            || view.getChildCount() > 0)
        return;

    // dynamically create & add genre chips
    Context context = view.getContext();
    for (Genre genre : genres) {
        Chip chip = new Chip(context);
        chip.setText(genre.getName());
        chip.setChipStrokeWidth(UiUtils.dipToPixels(context, 1));
        chip.setChipStrokeColor(ColorStateList.valueOf(
                context.getResources().getColor(R.color.md_blue_grey_200)));
        chip.setChipBackgroundColorResource(android.R.color.transparent);
        view.addView(chip);
    }
}
 
Example #2
Source File: BindingAdapters.java    From lttrs-android with Apache License 2.0 6 votes vote down vote up
@BindingAdapter("android:text")
public static void setText(final TextView textView, final FullEmail.From from) {
    if (from instanceof FullEmail.NamedFrom) {
        final FullEmail.NamedFrom named = (FullEmail.NamedFrom) from;
        textView.setText(named.getName());
    } else if (from instanceof FullEmail.DraftFrom) {
        final Context context = textView.getContext();
        final SpannableString spannable = new SpannableString(context.getString(R.string.draft));
        spannable.setSpan(
                new ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorPrimary)),
                0,
                spannable.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        textView.setText(spannable);
    }
}
 
Example #3
Source File: ValidationBindings.java    From convalida with Apache License 2.0 6 votes vote down vote up
@BindingAdapter(value = {
        "futureDateErrorMessage",
        "futureDateDateFormat",
        "futureDateLimitDate",
        "futureDateAutoDismiss",
        "futureDateRequired"
}, requireAll = false)
public static void futureDateValidationBindings(
        @NonNull EditText field,
        @NonNull String errorMessage,
        @NonNull String dateFormat,
        @NonNull String limitDate,
        Boolean autoDismiss,
        Boolean required
) {
    addValidatorToField(field, new FutureDateValidator(
            field,
            errorMessage,
            dateFormat,
            limitDate,
            autoDismiss != null ? autoDismiss : true,
            required != null ? required : true
    ));
}
 
Example #4
Source File: SignaturePadBindingAdapter.java    From SSForms with GNU General Public License v3.0 6 votes vote down vote up
@BindingAdapter(value = {"onStartSigning", "onSigned", "onClear"}, requireAll = false)
public static void setOnSignedListener(SignaturePad view, final OnStartSigningListener onStartSigningListener, final OnSignedListener onSignedListener, final OnClearListener onClearListener) {
    view.setOnSignedListener(new SignaturePad.OnSignedListener() {
        @Override
        public void onStartSigning() {
            if (onStartSigningListener != null) {
                onStartSigningListener.onStartSigning();
            }
        }

        @Override
        public void onSigned() {
            if (onSignedListener != null) {
                onSignedListener.onSigned();
            }
        }

        @Override
        public void onClear() {
            if (onClearListener != null) {
                onClearListener.onClear();
            }
        }
    });
}
 
Example #5
Source File: BindingAdapters.java    From lttrs-android with Apache License 2.0 6 votes vote down vote up
@BindingAdapter("from")
public static void setThreadOverviewFrom(final ImageView imageView, final Map.Entry<String, ThreadOverviewItem.From> from) {
    if (imageView.isActivated()) {
        imageView.setImageResource(R.drawable.ic_selected_24dp);
        return;
    }
    if (from == null) {
        imageView.setImageDrawable(new AvatarDrawable(null, null));
    } else {
        final ThreadOverviewItem.From value = from.getValue();
        if (value instanceof ThreadOverviewItem.NamedFrom) {
            imageView.setImageDrawable(new AvatarDrawable(((ThreadOverviewItem.NamedFrom) value).name, from.getKey()));
        } else {
            imageView.setImageDrawable(new AvatarDrawable(null, null)); //TODO do something nice to indicate draft
        }
    }
}
 
Example #6
Source File: ValidationBindings.java    From convalida with Apache License 2.0 6 votes vote down vote up
@BindingAdapter(value = {
        "emailErrorMessage",
        "emailAutoDismiss",
        "emailRequired"
}, requireAll = false)
public static void emailValidationBindings(
        @NonNull EditText field,
        @NonNull String errorMessage,
        Boolean autoDismiss,
        Boolean required
) {
    addValidatorToField(field, new EmailValidator(
            field,
            errorMessage,
            autoDismiss != null ? autoDismiss : true,
            required != null ? required : true
    ));
}
 
Example #7
Source File: ValidationBindings.java    From convalida with Apache License 2.0 6 votes vote down vote up
@BindingAdapter(value = {
        "ipv6ErrorMessage",
        "ipv6AutoDismiss",
        "ipv6Required"
}, requireAll = false)
public static void ipv6ValidationBindings(
        @NonNull EditText field,
        @NonNull String errorMessage,
        Boolean autoDismiss,
        Boolean required
) {
    addValidatorToField(field, new Ipv6Validator(
            field,
            errorMessage,
            autoDismiss != null ? autoDismiss : true,
            required != null ? required : true
    ));
}
 
Example #8
Source File: ValidationBindings.java    From convalida with Apache License 2.0 6 votes vote down vote up
@BindingAdapter(value = {
        "cnpjErrorMessage",
        "cnpjAutoDismiss",
        "cnpjRequired"
}, requireAll = false)
public static void cnpjValidationBindings(
        @NonNull EditText field,
        @NonNull String errorMessage,
        Boolean autoDismiss,
        Boolean required
) {
    addValidatorToField(field, new CnpjValidator(
            field,
            errorMessage,
            autoDismiss != null ? autoDismiss : true,
            required != null ? required : true
    ));
}
 
Example #9
Source File: ValidationBindings.java    From convalida with Apache License 2.0 6 votes vote down vote up
@BindingAdapter(value = {
        "passwordErrorMessage",
        "passwordMinLength",
        "passwordPattern",
        "passwordAutoDismiss"
}, requireAll = false)
public static void passwordValidationBindings(
        @NonNull EditText field,
        @NonNull String errorMessage,
        Integer minLength,
        String pattern,
        Boolean autoDismiss
) {
    addValidatorToField(field, new PasswordValidator(
            field,
            errorMessage,
            minLength != null ? minLength : 0,
            pattern != null ? pattern : "",
            autoDismiss != null ? autoDismiss : true
    ));
}
 
Example #10
Source File: ValidationBindings.java    From convalida with Apache License 2.0 6 votes vote down vote up
@BindingAdapter(value = {
        "isbnErrorMessage",
        "isbnAutoDismiss",
        "isbnRequired"
}, requireAll = false)
public static void isbnValidationBindings(
        @NonNull EditText field,
        @NonNull String errorMessage,
        Boolean autoDismiss,
        Boolean required
) {
    addValidatorToField(field, new IsbnValidator(
            field,
            errorMessage,
            autoDismiss != null ? autoDismiss : true,
            required != null ? required : true
    ));
}
 
Example #11
Source File: Bindings.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@BindingAdapter("enrolmentIcon")
public static void setEnrolmentIcon(ImageView view, EnrollmentStatus status) {
    Drawable lock;
    if (status == null)
        status = EnrollmentStatus.ACTIVE;
    switch (status) {
        case ACTIVE:
            lock = AppCompatResources.getDrawable(view.getContext(), R.drawable.ic_lock_open_green);
            break;
        case COMPLETED:
            lock = AppCompatResources.getDrawable(view.getContext(), R.drawable.ic_lock_completed);
            break;
        case CANCELLED:
            lock = AppCompatResources.getDrawable(view.getContext(), R.drawable.ic_lock_inactive);
            break;
        default:
            lock = AppCompatResources.getDrawable(view.getContext(), R.drawable.ic_lock_read_only);
            break;
    }

    view.setImageDrawable(lock);

}
 
Example #12
Source File: ValidationBindings.java    From convalida with Apache License 2.0 6 votes vote down vote up
@BindingAdapter(value = {
        "patternErrorMessage",
        "patternPattern",
        "patternAutoDismiss",
        "patternRequired"
}, requireAll = false)
public static void patternValidationBindings(
        @NonNull EditText field,
        @NonNull String errorMessage,
        @NonNull String pattern,
        Boolean autoDismiss,
        Boolean required
) {
    addValidatorToField(field, new PatternValidator(
            field,
            errorMessage,
            pattern,
            autoDismiss != null ? autoDismiss : true,
            required != null ? required : true
    ));
}
 
Example #13
Source File: Bindings.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@BindingAdapter("eventWithoutRegistrationStatusText")
public static void setEventWithoutRegistrationStatusText(TextView textView, ProgramEventViewModel event) {
    switch (event.eventStatus()) {
        case ACTIVE:
            if (event.isExpired()) {
                textView.setText(textView.getContext().getString(R.string.event_editing_expired));
            } else {
                textView.setText(textView.getContext().getString(R.string.event_open));
            }
            break;
        case COMPLETED:
            if (event.isExpired()) {
                textView.setText(textView.getContext().getString(R.string.event_editing_expired));
            } else {
                textView.setText(textView.getContext().getString(R.string.event_completed));
            }
            break;
        case SKIPPED:
            textView.setText(textView.getContext().getString(R.string.event_editing_expired));
            break;
        default:
            textView.setText(textView.getContext().getString(R.string.read_only));
            break;
    }
}
 
Example #14
Source File: ValidationBindings.java    From convalida with Apache License 2.0 6 votes vote down vote up
@BindingAdapter(value = {
        "numericLimitErrorMessage",
        "numericLimitAutoDismiss",
        "numericLimitMin",
        "numericLimitMax",
        "numericLimitRequired"
}, requireAll = false)
public static void numericLimitValidationBindings(
        @NonNull EditText field,
        @NonNull String errorMessage,
        Boolean autoDismiss,
        @NonNull String min,
        @NonNull String max,
        Boolean required
) {
    addValidatorToField(field, new NumericLimitValidator(
            field,
            errorMessage,
            autoDismiss != null ? autoDismiss : true,
            min,
            max,
            required != null ? required : true
    ));
}
 
Example #15
Source File: Bindings.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@BindingAdapter("stateText")
public static void setStateText(TextView textView, State state) {
    switch (state) {
        case TO_POST:
            textView.setText(textView.getContext().getString(R.string.state_to_post));
            break;
        case TO_UPDATE:
            textView.setText(textView.getContext().getString(R.string.state_to_update));
            break;
        case ERROR:
            textView.setText(textView.getContext().getString(R.string.state_error));
            break;
        case SYNCED:
            textView.setText(textView.getContext().getString(R.string.state_synced));
            break;
        default:
            break;
    }
}
 
Example #16
Source File: ValidationBindings.java    From convalida with Apache License 2.0 6 votes vote down vote up
@BindingAdapter(value = {
        "cpfErrorMessage",
        "cpfAutoDismiss",
        "cpfRequired"
}, requireAll = false)
public static void cpfValidationBindings(
        @NonNull EditText field,
        @NonNull String errorMessage,
        Boolean autoDismiss,
        Boolean required
) {
    addValidatorToField(field, new CpfValidator(
            field,
            errorMessage,
            autoDismiss != null ? autoDismiss : true,
            required != null ? required : true
    ));
}
 
Example #17
Source File: BindingAdapters.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
@BindingAdapter("leftMargin")
public static void setLeftMargin(@NonNull View view, @NonNull @Dimension float margin) {
    if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        if (params.getMarginStart() != Math.round(margin)) {
            params.setMarginStart(Math.round(margin));
            view.setLayoutParams(params);
        }
    }
}
 
Example #18
Source File: ReviewsBinding.java    From PopularMovies with MIT License 5 votes vote down vote up
@BindingAdapter("items")
public static void setItems(RecyclerView recyclerView, List<Review> items) {
    ReviewsAdapter adapter = (ReviewsAdapter) recyclerView.getAdapter();
    if (adapter != null) {
        adapter.submitList(items);
    }
}
 
Example #19
Source File: TypeBindings.java    From data-binding-validator with Apache License 2.0 5 votes vote down vote up
@BindingAdapter(value = {"validateType", "validateTypeMessage", "validateTypeAutoDismiss"}, requireAll = false)
public static void bindingTypeValidation(TextView view, String fieldTypeText, String errorMessage, boolean autoDismiss) {
    if (autoDismiss) {
        EditTextHandler.disableErrorOnChanged(view);
    }
    TypeRule.FieldType fieldType = getFieldTypeByText(fieldTypeText);
    try {
        String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
                errorMessage, fieldType.errorMessageId);
        ViewTagHelper.appendValue(R.id.validator_rule, view, fieldType.instantiate(view, handledErrorMessage));
    } catch (Exception ignored) {}
}
 
Example #20
Source File: LengthBindings.java    From data-binding-validator with Apache License 2.0 5 votes vote down vote up
@BindingAdapter(value = {"validateMaxLength", "validateMaxLengthMessage", "validateMaxLengthAutoDismiss"}, requireAll = false)
public static void bindingMaxLength(TextView view, int maxLength, String errorMessage, boolean autoDismiss) {
    if (autoDismiss) {
        EditTextHandler.disableErrorOnChanged(view);
    }

    String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
            errorMessage, R.string.error_message_max_length, maxLength);
    ViewTagHelper.appendValue(R.id.validator_rule, view, new MaxLengthRule(view, maxLength, handledErrorMessage));
}
 
Example #21
Source File: LengthBindings.java    From data-binding-validator with Apache License 2.0 5 votes vote down vote up
@BindingAdapter(value = {"validateEmpty", "validateEmptyMessage", "validateEmptyAutoDismiss"}, requireAll = false)
public static void bindingEmpty(TextView view, boolean empty, String errorMessage, boolean autoDismiss) {
    if (autoDismiss) {
        EditTextHandler.disableErrorOnChanged(view);
    }

    String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
            errorMessage, R.string.error_message_empty_validation);
    ViewTagHelper.appendValue(R.id.validator_rule, view, new EmptyRule(view, empty, handledErrorMessage));
}
 
Example #22
Source File: BindingAdapters.java    From PopularMovies with MIT License 5 votes vote down vote up
@BindingAdapter({"imageUrl", "isBackdrop"})
public static void bindImage(ImageView imageView, String imagePath, boolean isBackdrop) {
    String baseUrl;
    if (isBackdrop) {
        baseUrl = Constants.BACKDROP_URL;
    } else {
        baseUrl = Constants.IMAGE_URL;
    }

    GlideApp.with(imageView.getContext())
            .load(baseUrl + imagePath)
            .placeholder(R.color.md_grey_200)
            .into(imageView);
}
 
Example #23
Source File: BindingAdapters.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
@BindingAdapter("items")
public static void bindAdapterWithDefaultBinder(@NonNull RecyclerView recyclerView, @Nullable ObservableList<Download> items) {
    DownloadsAdapter adapter = (DownloadsAdapter)recyclerView.getAdapter();
    if (adapter != null) {
        adapter.setDownloadsList(items);
    }
}
 
Example #24
Source File: BindingAdapters.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
@BindingAdapter(value={"textDrawable", "textString"})
public static void setSpannableString(@NonNull TextView textView, @NonNull Drawable drawable, String text) {
    SpannableString spannableString = new SpannableString(text);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
    spannableString.setSpan(span, spannableString.toString().indexOf("@"),  spannableString.toString().indexOf("@")+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    textView.setText(spannableString);
}
 
Example #25
Source File: BindingAdapters.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
@BindingAdapter("typeface")
public static void setTypeface(@NonNull TextView v, @NonNull String style) {
    switch (style) {
        case "bold":
            v.setTypeface(null, Typeface.BOLD);
            break;
        default:
            v.setTypeface(null, Typeface.NORMAL);
            break;
    }
}
 
Example #26
Source File: PasswordBindings.java    From data-binding-validator with Apache License 2.0 5 votes vote down vote up
@BindingAdapter(value = {"validatePassword", "validatePasswordMessage", "validatePasswordAutoDismiss"}, requireAll = false)
public static void bindingPassword(TextView view, TextView comparableView, String errorMessage, boolean autoDismiss) {
    if (autoDismiss) {
        EditTextHandler.disableErrorOnChanged(view);
    }

    String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
            errorMessage, R.string.error_message_not_equal_password);
    ViewTagHelper.appendValue(R.id.validator_rule, view,
            new ConfirmPasswordRule(view, comparableView, handledErrorMessage));
}
 
Example #27
Source File: ValidationBindings.java    From convalida with Apache License 2.0 5 votes vote down vote up
@BindingAdapter(value = {
        "requiredErrorMessage",
        "requiredAutoDismiss"
}, requireAll = false)
public static void requiredValidationBindings(
        @NonNull EditText field,
        @NonNull String errorMessage,
        Boolean autoDismiss
) {
    addValidatorToField(field, new RequiredValidator(
            field,
            errorMessage,
            autoDismiss != null ? autoDismiss : true
    ));
}
 
Example #28
Source File: BindingAdapters.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
@BindingAdapter("bindDate")
public static void bindDate(@NonNull TextView textView, long timestamp) {
    String androidDateTime = android.text.format.DateFormat.getDateFormat(textView.getContext()).format(new Date(timestamp)) + " " +
            android.text.format.DateFormat.getTimeFormat(textView.getContext()).format(new Date(timestamp));
    String AmPm = "";
    if(!Character.isDigit(androidDateTime.charAt(androidDateTime.length()-1))) {
        if(androidDateTime.contains(new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.AM])){
            AmPm = " " + new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.AM];
        }else{
            AmPm = " " + new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.PM];
        }
        androidDateTime=androidDateTime.replace(AmPm, "");
    }
    textView.setText(androidDateTime.concat(AmPm));
}
 
Example #29
Source File: BindingUtils.java    From android-mvvm-architecture with Apache License 2.0 5 votes vote down vote up
@BindingAdapter({"adapter"})
public static void addOpenSourceItems(RecyclerView recyclerView, List<OpenSourceItemViewModel> openSourceItems) {
    OpenSourceAdapter adapter = (OpenSourceAdapter) recyclerView.getAdapter();
    if (adapter != null) {
        adapter.clearItems();
        adapter.addItems(openSourceItems);
    }
}
 
Example #30
Source File: LengthBindings.java    From data-binding-validator with Apache License 2.0 5 votes vote down vote up
@BindingAdapter(value = {"validateMinLength", "validateMinLengthMessage", "validateMinLengthAutoDismiss"}, requireAll = false)
public static void bindingMinLength(TextView view, int minLength, String errorMessage, boolean autoDismiss) {
    if (autoDismiss) {
        EditTextHandler.disableErrorOnChanged(view);
    }

    String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
            errorMessage, R.string.error_message_min_length, minLength);
    ViewTagHelper.appendValue(R.id.validator_rule, view, new MinLengthRule(view, minLength, handledErrorMessage));
}