Load Class by name - Android java.lang.reflect

Android examples for java.lang.reflect:Class

Description

Load Class by name

Demo Code


import android.util.Log;

public class Main {
  public static final String TAG = Main.class.getSimpleName();

  public static Class<?> LoadClass(String className) {
    Class<?> result = null;

    Log.d(TAG, "Attempting to load call: " + className);
    ClassLoader classLoader = Main.class.getClassLoader();

    try {//  w w w .  j  a  v a  2 s . c om
      result = classLoader.loadClass(className);
      Log.d(TAG, "Class loaded");
    } catch (ClassNotFoundException ex) {
      Log.d(TAG, "Class failed to load");
      Log.d(TAG, ex.getMessage());
    }

    return result;
  }
}

Related Tutorials