Getters for compat resources

This commit is contained in:
Denis Karmyshakov 2018-03-30 16:24:27 +03:00
parent 496abde610
commit 399ec7d7f5
1 changed files with 73 additions and 0 deletions

View File

@ -23,15 +23,22 @@ import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LifecycleRegistry;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.CallSuper;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@ -166,6 +173,72 @@ public class ViewController<
return getContainer().findViewById(id);
}
/**
* Return a localized string from the application's package's default string table.
*
* @param resId Resource id for the string
*/
@NonNull
public final String getString(@StringRes final int resId) {
return getActivity().getString(resId);
}
/**
* Return a localized formatted string from the application's package's default string table, substituting the format arguments as defined in
* {@link java.util.Formatter} and {@link java.lang.String#format}.
*
* @param resId Resource id for the format string
* @param formatArgs The format arguments that will be used for substitution.
*/
@NonNull
public final String getString(@StringRes final int resId, @NonNull final Object... formatArgs) {
return getActivity().getString(resId, formatArgs);
}
/**
* Return the color value associated with a particular resource ID.
* Starting in {@link android.os.Build.VERSION_CODES#M}, the returned
* color will be styled for the specified Context's theme.
*
* @param resId The resource id to search for data;
* @return int A single color value in the form 0xAARRGGBB.
*/
@ColorInt
public final int getColor(@ColorRes final int resId) {
return ContextCompat.getColor(activity, resId);
}
/**
* Returns a color state list associated with a particular resource ID.
* <p>
* Starting in {@link android.os.Build.VERSION_CODES#M}, the returned
* color state list will be styled for the specified Context's theme.
*
* @param resId The desired resource identifier, as generated by the aapt
* tool. This integer encodes the package, type, and resource
* entry. The value 0 is an invalid identifier.
* @return A color state list, or {@code null} if the resource could not be resolved.
* @throws android.content.res.Resources.NotFoundException if the given ID
* does not exist.
*/
@Nullable
public final ColorStateList getColorStateList(@ColorRes final int resId) {
return ContextCompat.getColorStateList(getActivity(), resId);
}
/**
* Returns a drawable object associated with a particular resource ID.
* Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the
* returned drawable will be styled for the specified Context's theme.
*
* @param resId The resource id to search for data;
* @return Drawable An object that can be used to draw this resource.
*/
@Nullable
public final Drawable getDrawable(@DrawableRes final int resId) {
return ContextCompat.getDrawable(getActivity(), resId);
}
/**
* Calls when activity configuring ActionBar, Toolbar, Sidebar etc.
* If it will be called or not depends on {@link Fragment#hasOptionsMenu()} and {@link Fragment#isMenuVisible()}.