Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-generate Android Log TAG in Android Studio/Intellij idea

In Intellij Idea when i typing psfs and then press Ctrl+J IDE was getting me a dialog :

enter image description here

And when i press Enter i get an

enter image description here

I know where i can customize my own output

enter image description here

But i can't any doc's how i can write my own live template.

In the end i want to get next result :

Typing : psfst -> press Ctrl+J -> press Enter

Result :

public static final String TAG = <currentClassName>.class.getSimpleName();

It will be so helpfull, because i have a habit to log my classes.

like image 886
Sergey Shustikov Avatar asked Mar 31 '15 21:03

Sergey Shustikov


4 Answers

I find a solution

1) Create a new live template in plain group
2) In template text :

private static final String TAG = $CLASS_NAME$.class.getSimpleName();

3) Define a usage scope :

enter image description here

4) Choose a shortcut :

enter image description here

finally click on Edit variables and change expression value to className()

enter image description here

Click Ok , Apply, Ok and use.

like image 92
Sergey Shustikov Avatar answered Oct 04 '22 22:10

Sergey Shustikov


There already exists such shortcut in Android Studio - write logt and enter while cursor is at the class scope.

more here :

for more see here

like image 34
Ofek Ron Avatar answered Oct 04 '22 22:10

Ofek Ron


It's super simple in Android studio, just type logt and press Tab.

It generates: private static final String TAG = "xyzActivity";

like image 15
CapTen101 Avatar answered Oct 04 '22 22:10

CapTen101


For those using Android Studio 3 and Kotlin it is necessary to change how Live Templates set:

Editor >> Live Templates >> AndroidLog

By default it is just for Java

enter image description here

Add Kotlin pressing "change" button and check Kotlin

enter image description here

and after that it will work again!

enter image description here


Updated: 2020

Android Studio: 3.6.2

enter image description here

Steps:

  1. Add
  2. Call logtk are whatever you want to call
  3. Use this code as template text
private val TAG = this::class.java.simpleName
  1. Add a description
  2. Do it applicable to Kotlin: in my case I used just for Class

Update June 2020

There's no need to set anything up anymore. Android Studio 4.0 comes with this functionality by default. The same with Toast

enter image description here

like image 11
Jorge Casariego Avatar answered Oct 04 '22 22:10

Jorge Casariego