Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import java.lang.reflect.Field;

public class Main {
    public static Drawable getDrawableByName(String name, Context context) {
        try {
            Field field = Class.forName("com.poetic.emotion.R$drawable").getField(name);
            int drawableRes = field.getInt(field);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                return context.getDrawable(drawableRes);
            } else {
                return context.getResources().getDrawable(drawableRes);
            }
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }
}