Example usage for android.content.res Resources getResourceTypeName

List of usage examples for android.content.res Resources getResourceTypeName

Introduction

In this page you can find the example usage for android.content.res Resources getResourceTypeName.

Prototype

public String getResourceTypeName(@AnyRes int resid) throws NotFoundException 

Source Link

Document

Return the type name for a given resource identifier.

Usage

From source file:Main.java

public static Uri getDrawableUri(Context context, int resId) {
    Resources resources = context.getResources();
    return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(resId)
            + '/' + resources.getResourceTypeName(resId) + '/' + resources.getResourceEntryName(resId));
}

From source file:Main.java

public static Uri getURIFromResrouceID(Context context, int resId) {
    Resources resources = context.getResources();
    Uri u = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(resId)
            + '/' + resources.getResourceTypeName(resId) + '/' + resources.getResourceEntryName(resId));
    return u;//www.  jav a 2s  . co m
}

From source file:Main.java

/**
 * Applies stirng to View by Reflection/* w  w w. j av  a  2 s . c  om*/
 *
 * @param context      Context
 * @param view         View to apply to.
 * @param attrs        Attributes of Layout
 */
public static void applyResourceStringToView(Context context, View view, AttributeSet attrs) {
    if (attrs != null) {
        Resources res = context.getResources();
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            int resId = attrs.getAttributeResourceValue(i, 0);
            if (resId > 0) {
                String resType = res.getResourceTypeName(resId);
                if (resType.equals("string")) {
                    String attrName = attrs.getAttributeName(i);
                    try {
                        Method method = view.getClass().getMethod("set" + capitalize(attrName),
                                CharSequence.class);
                        method.setAccessible(true);
                        method.invoke(view, res.getString(resId));
                    } catch (NoSuchMethodException e) {
                    } catch (Exception e) {
                    }
                }
            }
        }
    }
}

From source file:Main.java

/**
 * get uri to any resource type/*from w  w w.  j  a  va 2  s.c o m*/
 * @param context - context
 * @param resId - resource id
 * @throws Resources.NotFoundException if the given ID does not exist.
 * @return - Uri to resource by given id
 */
public static final Uri getUriToResource(@NonNull Context context, @AnyRes int resId)
        throws Resources.NotFoundException {
    /** Return a Resources instance for your application's package. */
    Resources res = context.getResources();
    /**
     * Creates a Uri which parses the given encoded URI string.
     * @param uriString an RFC 2396-compliant, encoded URI
     * @throws NullPointerException if uriString is null
     * @return Uri for this given uri string
     */
    Uri resUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + res.getResourcePackageName(resId)
            + '/' + res.getResourceTypeName(resId) + '/' + res.getResourceEntryName(resId));
    /** return uri */
    return resUri;
}

From source file:com.higgses.griffin.annotation.app.GinInjector.java

/**
 * ??/* w  w  w .java 2  s .  c o  m*/
 * 
 * @param object
 * @param field
 */
private void injectResource(Object object, Field field) {
    if (field.isAnnotationPresent(GinInjectResource.class)) {
        GinInjectResource resourceJect = field.getAnnotation(GinInjectResource.class);
        int resourceID = resourceJect.id();
        try {
            Activity activity = null;
            if (object instanceof Activity) {
                activity = (Activity) object;
            } else if (object instanceof Fragment) {
                activity = ((Fragment) object).getActivity();
            }
            field.setAccessible(true);
            Resources resources = activity.getResources();
            String type = resources.getResourceTypeName(resourceID);
            if (type.equalsIgnoreCase("string")) {
                field.set(activity, activity.getResources().getString(resourceID));
            } else if (type.equalsIgnoreCase("drawable")) {
                field.set(activity, activity.getResources().getDrawable(resourceID));
            } else if (type.equalsIgnoreCase("layout")) {
                field.set(activity, activity.getResources().getLayout(resourceID));
            } else if (type.equalsIgnoreCase("array")) {
                if (field.getType().equals(int[].class)) {
                    field.set(activity, activity.getResources().getIntArray(resourceID));
                } else if (field.getType().equals(String[].class)) {
                    field.set(activity, activity.getResources().getStringArray(resourceID));
                } else {
                    field.set(activity, activity.getResources().getStringArray(resourceID));
                }

            } else if (type.equalsIgnoreCase("color")) {
                if (field.getType().equals(Integer.TYPE)) {
                    field.set(activity, activity.getResources().getColor(resourceID));
                } else {
                    field.set(activity, activity.getResources().getColorStateList(resourceID));
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:android.support.car.app.CarFragmentActivity.java

private static String viewToString(View view) {
    StringBuilder out = new StringBuilder(128);
    out.append(view.getClass().getName());
    out.append('{');
    out.append(Integer.toHexString(System.identityHashCode(view)));
    out.append(' ');
    switch (view.getVisibility()) {
    case View.VISIBLE:
        out.append('V');
        break;/*w w w . j  a va2 s . co  m*/
    case View.INVISIBLE:
        out.append('I');
        break;
    case View.GONE:
        out.append('G');
        break;
    default:
        out.append('.');
        break;
    }
    out.append(view.isFocusable() ? 'F' : '.');
    out.append(view.isEnabled() ? 'E' : '.');
    out.append(view.willNotDraw() ? '.' : 'D');
    out.append(view.isHorizontalScrollBarEnabled() ? 'H' : '.');
    out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
    out.append(view.isClickable() ? 'C' : '.');
    out.append(view.isLongClickable() ? 'L' : '.');
    out.append(' ');
    out.append(view.isFocused() ? 'F' : '.');
    out.append(view.isSelected() ? 'S' : '.');
    out.append(view.isPressed() ? 'P' : '.');
    out.append(' ');
    out.append(view.getLeft());
    out.append(',');
    out.append(view.getTop());
    out.append('-');
    out.append(view.getRight());
    out.append(',');
    out.append(view.getBottom());
    final int id = view.getId();
    if (id != View.NO_ID) {
        out.append(" #");
        out.append(Integer.toHexString(id));
        final Resources r = view.getResources();
        if (id != 0 && r != null) {
            try {
                String pkgname;
                switch (id & 0xff000000) {
                case 0x7f000000:
                    pkgname = "app";
                    break;
                case 0x01000000:
                    pkgname = "android";
                    break;
                default:
                    pkgname = r.getResourcePackageName(id);
                    break;
                }
                String typename = r.getResourceTypeName(id);
                String entryname = r.getResourceEntryName(id);
                out.append(" ");
                out.append(pkgname);
                out.append(":");
                out.append(typename);
                out.append("/");
                out.append(entryname);
            } catch (Resources.NotFoundException e) {
            }
        }
    }
    out.append("}");
    return out.toString();
}

From source file:rocks.stalin.android.app.model.MusicProvider.java

private Collection<MediaBrowserCompat.MediaItem> createBrowsableMediaItemForRoot(Resources resources) {
    ArrayList<MediaBrowserCompat.MediaItem> rootMenuItems = new ArrayList<>();

    rootMenuItems.add(new MediaBrowserCompat.MediaItem(new MediaDescriptionCompat.Builder()
            .setMediaId(MEDIA_ID_ALL_MUSICS).setTitle(resources.getString(R.string.browse_all))
            .setSubtitle(resources.getString(R.string.browse_all_subtitle))
            .setIconUri(new Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
                    .authority(resources.getResourcePackageName(R.drawable.ic_allmusic_black_24dp))
                    .appendPath(resources.getResourceTypeName(R.drawable.ic_allmusic_black_24dp))
                    .appendPath(resources.getResourceEntryName(R.drawable.ic_allmusic_black_24dp)).build())
            .build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
    rootMenuItems.add(new MediaBrowserCompat.MediaItem(new MediaDescriptionCompat.Builder()
            .setMediaId(MEDIA_ID_MUSICS_BY_GENRE).setTitle(resources.getString(R.string.browse_genres))
            .setSubtitle(resources.getString(R.string.browse_genre_subtitle))
            .setIconUri(new Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
                    .authority(resources.getResourcePackageName(R.drawable.ic_by_genre))
                    .appendPath(resources.getResourceTypeName(R.drawable.ic_by_genre))
                    .appendPath(resources.getResourceEntryName(R.drawable.ic_by_genre)).build())
            .build(), MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));

    return rootMenuItems;
}