diff --git a/emptyrecyclerview/src/main/java/com/app/dr1009/emptyrecyclerview/EmptyRecyclerView.java b/emptyrecyclerview/src/main/java/com/app/dr1009/emptyrecyclerview/EmptyRecyclerView.java index 1b749fc..ad12425 100644 --- a/emptyrecyclerview/src/main/java/com/app/dr1009/emptyrecyclerview/EmptyRecyclerView.java +++ b/emptyrecyclerview/src/main/java/com/app/dr1009/emptyrecyclerview/EmptyRecyclerView.java @@ -6,10 +6,15 @@ import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; import androidx.annotation.LayoutRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.annotation.StringRes; +import androidx.annotation.StyleRes; +import androidx.appcompat.view.ContextThemeWrapper; import androidx.recyclerview.widget.RecyclerView; public class EmptyRecyclerView extends RecyclerView { @@ -30,8 +35,8 @@ public EmptyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EmptyRecyclerView, 0, 0); int resId = a.getResourceId(R.styleable.EmptyRecyclerView_emptyView, 0); - setEmptyView(resId); a.recycle(); + setEmptyView(resId); } @Override @@ -66,7 +71,7 @@ public void onDraw(Canvas c) { } /** - * Sets the view to show if the adapter is empty + * Set a view to show if the adapter is empty * * @param emptyView {@link View} */ @@ -75,7 +80,7 @@ public void setEmptyView(@Nullable View emptyView) { } /** - * Sets the view to show if the adapter is empty + * Set a view to show if the adapter is empty * * @param resId empty view's layout id */ @@ -86,4 +91,107 @@ public void setEmptyView(@LayoutRes int resId) { mEmptyView = LayoutInflater.from(getContext()).inflate(resId, null); } + + /** + * Create TextView from a string resource and Gravity, + * add to EmptyRecyclerView to show if the adapter is empty. + * + * @param emptyMessageResId resource id of message to show + * @param gravity {@link android.view.Gravity} + */ + public void setEmptyString(@StringRes int emptyMessageResId, int gravity) { + setEmptyString(emptyMessageResId, gravity, null); + } + + /** + * Create TextView from a string resource and Gravity and LayoutParams, + * add to EmptyRecyclerView to show if the adapter is empty. + * + * @param emptyMessageResId resource id of message to show + * @param gravity {@link android.view.Gravity} + * @param params {@link ViewGroup.LayoutParams} + */ + public void setEmptyString(@StringRes int emptyMessageResId, int gravity, @Nullable ViewGroup.LayoutParams params) { + setEmptyString(emptyMessageResId, gravity, params, 0); + } + + /** + * Create TextView from a string resource and Gravity and Style resource, + * add to EmptyRecyclerView to show if the adapter is empty. + * + * @param emptyMessageResId resource id of message to show + * @param gravity {@link android.view.Gravity} + * @param styleRes resource id of view style + */ + public void setEmptyString(@StringRes int emptyMessageResId, int gravity, @StyleRes int styleRes) { + setEmptyString(emptyMessageResId, gravity, null, styleRes); + } + + /** + * Create TextView from a string resource, Gravity, LayoutParams and Style resource, + * add to EmptyRecyclerView to show if the adapter is empty. + * + * @param emptyMessageResId resource id of message to show + * @param gravity {@link android.view.Gravity} + * @param params {@link ViewGroup.LayoutParams} + * @param styleRes resource id of view style + */ + public void setEmptyString(@StringRes int emptyMessageResId, int gravity, @Nullable ViewGroup.LayoutParams params, @StyleRes int styleRes) { + setEmptyString(getContext().getString(emptyMessageResId), gravity, params, styleRes); + } + + /** + * Create TextView from a string and Gravity, + * add to EmptyRecyclerView to show if the adapter is empty. + * + * @param emptyMessage message to show + * @param gravity {@link android.view.Gravity} + */ + public void setEmptyString(@Nullable String emptyMessage, int gravity) { + setEmptyString(emptyMessage, gravity, null); + } + + /** + * Create TextView from a string and Gravity and LayoutParams, + * add to EmptyRecyclerView to show if the adapter is empty. + * + * @param emptyMessage message to show + * @param gravity {@link android.view.Gravity} + * @param params {@link ViewGroup.LayoutParams} + */ + public void setEmptyString(@Nullable String emptyMessage, int gravity, @Nullable ViewGroup.LayoutParams params) { + setEmptyString(emptyMessage, gravity, params, 0); + } + + /** + * Create TextView from a string and Gravity and Style resource, + * add to EmptyRecyclerView to show if the adapter is empty. + * + * @param emptyMessage message to show + * @param gravity {@link android.view.Gravity} + * @param styleRes resource id of view style + */ + public void setEmptyString(@Nullable String emptyMessage, int gravity, @StyleRes int styleRes) { + setEmptyString(emptyMessage, gravity, null, styleRes); + } + + /** + * Create TextView from a string, Gravity, LayoutParams and Style resource, + * add to EmptyRecyclerView to show if the adapter is empty. + * + * @param emptyMessage message to show + * @param gravity {@link android.view.Gravity} + * @param params {@link ViewGroup.LayoutParams} + * @param styleRes resource id of view style + */ + public void setEmptyString(@Nullable String emptyMessage, int gravity, @Nullable ViewGroup.LayoutParams params, @StyleRes int styleRes) { + TextView textView = new TextView(new ContextThemeWrapper(getContext(), styleRes)); + textView.setText(emptyMessage); + textView.setGravity(gravity); + if (params != null) { + textView.setLayoutParams(params); + } + + mEmptyView = textView; + } }