Getting class by its name Getting class by its name android android

Getting class by its name


use forName instead..

something like this..

 try {    Class<?> act = Class.forName("com.bla.TestActivity"); } catch (ClassNotFoundException e) {        e.printStackTrace();}


You can use Class::forName to get a class object of unknown type.If you want to get a typed class, you can use Class::asSubclass on the class returned by Class::forName:

Class<? extends Activity> activityClass = Class.forName("com.example.TestActivity")                                               .asSubclass(Activity.class);

Of course you will also have to handle a bunch of different types of exceptions. As is usual when dealing with reflection.


The Class.forName seems to have exceptions on it. This is just to expand upon the above to address this issue.

try { t = Class.forName("com.package.classname"); } catch (Exception ignored){}