Skip to content
Davide Steduto edited this page Jan 4, 2017 · 18 revisions

In this page


Utils

This class has static methods to handle Accent color and Spannable text. Direct link Utils.java

/**
 * Sets a spannable text with the accent color (if available) into the provided TextView.
 * Internally calls {@link #fetchAccentColor(Context, int)}.
 */
public static void highlightText(@NonNull TextView textView,
				 @Nullable String originalText, @Nullable String constraint);

/**
 * Sets a spannable text with any highlight color into the provided TextView.
 */
public static void highlightText(@NonNull TextView textView, @Nullable String originalText,
				 @Nullable String constraint, @ColorInt int color);

/**
 * Reset the internal accent color to #INVALID_COLOR, to give the possibility
 * to re-fetch it at runtime, since once it is fetched it cannot be changed.
 */
public static void resetAccentColor();

/**
 * Optimized method to fetch the accent color on devices with at least Lollipop.
 * If accent color has been already fetched it is simply returned.
 */
public static int fetchAccentColor(Context context, @ColorInt int defColor);

/**
 * Finds the layout orientation of the RecyclerView,
 * no matter which LayoutManager is in use.
 */
public static int getOrientation(RecyclerView.LayoutManager layoutManager);

/**
 * Helper method to retrieve the number of the columns (span count) of
 * the given LayoutManager. All Layouts are supported.
 */
public static int getSpanCount(RecyclerView.LayoutManager layoutManager);

/**
 * Helper methods to find visible item positions.
 */
public static int findFirstCompletelyVisibleItemPosition(
				RecyclerView.LayoutManager layoutManager);
public static int findLastCompletelyVisibleItemPosition(
				RecyclerView.LayoutManager layoutManager);
public static int findFirstVisibleItemPosition(
				RecyclerView.LayoutManager layoutManager);
public static int findLastVisibleItemPosition(
				RecyclerView.LayoutManager layoutManager);

Usage

To use as following in the bindViewHolder():

@Override
public void bindViewHolder(final FlexibleAdapter adapter, ParentViewHolder holder,
					int position, List payloads) {
	...
	// In case of searchText matches with Title or with subTitle fields
	// this will be highlighted
	if (adapter.hasSearchText()) {
		Utils.highlightText(holder.mTitle, getTitle(), adapter.getSearchText());
		Utils.highlightText(holder.mSubtitle, getSubtitle(), adapter.getSearchText());
	} else {
		holder.mTitle.setText(getTitle());
		holder.mSubtitle.setText(getSubtitle());
	}
	...
}

DrawableUtils

This class has static methods to handle the Background color with ripple at runtime.
Direct link DrawableUtils.java

/**
 * Helper methods to set the background depending on the android version.
 */
public static void setBackgroundCompat(View view, Drawable drawable);
public static void setBackgroundCompat(View view, @DrawableRes int drawableRes);

/**
 * Helper method to extract the drawable by its resource depending
 * on the android version.
 */
public static Drawable getDrawableCompat(Context context, @DrawableRes int drawableRes);

/**
 * Helper to get the default selectableItemBackground drawable of the
 * R.attr.selectableItemBackground attribute of the overridden style.
 */
public static Drawable getSelectableItemBackground(Context context);

/**
 * Helper to get the system default Color Control Highlight. Returns the color
 * of the R.attr.colorControlHighlight attribute in the overridden style.
 */
@ColorInt
public static int getColorControlHighlight(Context context);

/**
 * Helper to get a custom selectable background with Ripple if device has at
 * least Lollipop.
 */
public static Drawable getSelectableBackgroundCompat(@ColorInt int normalColor,
							@ColorInt int pressedColor,
							@ColorInt int rippleColor);

/**
 * Adds a ripple effect to any background.
 */
public static Drawable getRippleDrawable(Drawable drawable, @ColorInt int rippleColor);

/**
 * Generate the ColorDrawable object from the provided Color.
 */
public static ColorDrawable getColorDrawable(@ColorInt int color);

Usage

With only 1 statement, we avoid the configuration of the XML 👍
To use as following in the bindViewHolder():

@Override
public void bindViewHolder(final FlexibleAdapter adapter, final ViewHolder holder,
				int position, List payloads) {
	Context context = holder.itemView.getContext();
	...
	Drawable drawable = DrawableUtils.getSelectableBackgroundCompat(
				Color.WHITE, Color.parseColor("#dddddd"), // Same color of divider
				DrawableUtils.getColorControlHighlight(context));
	DrawableUtils.setBackgroundCompat(holder.itemView, drawable);
	
	// OR
	Drawable drawable = DrawableUtils.getSelectableBackgroundCompat(
				status.getColor(),             // normal background
				Utils.getColorAccent(context), // pressed background
				Color.WHITE));                 // ripple color
	DrawableUtils.setBackgroundCompat(holder.itemView, drawable);
}
Clone this wiki locally