Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get class name only, not full path?

Tags:

android

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.

like image 447
sandalone Avatar asked May 15 '12 17:05

sandalone


2 Answers

This is all you need.

MainActivity.this.getClass().getSimpleName(); 
like image 179
RobGThai Avatar answered Oct 29 '22 05:10

RobGThai


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();  
like image 45
sandalone Avatar answered Oct 29 '22 06:10

sandalone