How to get class name only, not full path?

I am aware that using Context and a method getClass().getName() I can get a string which represent full class name, like com.package1.package2.MainActivity.

How can I get only the last part, class name only? In this case it would be MainActivity string.

I can do it with a simple split() method, but maybe there is a better way, more reliable.


This is all you need.

MainActivity.this.getClass().getSimpleName();

To get only the name of the class, not full path you will use this expression:

String className =  this.getLocalClassName(); 
//or
String className = getBaseContext().getLocalClassName();  
//or
String className = getApplicationContext().getLocalClassName();