Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.View;
import java.lang.reflect.Method;

public class Main {
    /**
     * Applies stirng to View by Reflection
     *
     * @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) {
                        }
                    }
                }
            }
        }
    }

    /**
     * Generate capitalized string
     *
     * @param  line      string to be capitalized
     * @return capitalized string
     */
    private static String capitalize(final String line) {
        return Character.toUpperCase(line.charAt(0)) + line.substring(1);
    }
}