Java tutorial
//package com.java2s; /* This file is part of Roboject Copyright (c) 2010-2011 akquinet A.G. Contact: http://www.akquinet.de/en Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import android.content.Context; import android.content.res.ColorStateList; import android.content.res.Resources; import android.content.res.XmlResourceParser; import android.graphics.Movie; import android.graphics.drawable.Drawable; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import java.lang.reflect.Field; public class Main { public static Object getResource(Context context, Field field, int value) { Resources resources = context.getResources(); Class type = field.getType(); if (type.isAssignableFrom(Boolean.TYPE) || type.isAssignableFrom(Boolean.class)) return resources.getBoolean(value); else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) { return resources.getInteger(value); } else if (type.isAssignableFrom(ColorStateList.class)) return resources.getColorStateList(value); else if (type.isAssignableFrom(XmlResourceParser.class)) return resources.getXml(value); else if (type.isAssignableFrom(Float.TYPE) || type.isAssignableFrom(Float.class)) return resources.getDimension(value); else if (type.isAssignableFrom(Drawable.class)) return resources.getDrawable(value); else if (type.isAssignableFrom(Animation.class)) return AnimationUtils.loadAnimation(context, value); else if (type.isAssignableFrom(Movie.class)) return resources.getMovie(value); else if (type.isAssignableFrom(String.class)) return resources.getString(value); else if (type.isArray()) { if (type.getName().equals("[I")) { return resources.getIntArray(value); } else if (type.isAssignableFrom(String[].class)) { return resources.getStringArray(value); } } return null; } }