Live Template not working in Kotlin - android

Hope you have worked with Live Templates which is given by Android by default.
Like:
Generate debug log statement: "logd"+TAB
Generate error log statement: "loge"+TAB
Generate info log statement: "logi"+TAB
Generate TAG declaration: "logt"+TAB
Generate parameter logging: "logm"+TAB
Generate method return log: "logr"+TAB
Which it is not available in KOTLIN?
Is it not available in Android Studio 3.0 Canary Version?

Here is the step-by-step guide:
Firstly, Copy and paste AndroidLog templates to Kotlin (Just select them and use CMD+C, CMD+V (or Ctrl+C, Ctrl+V)
Secondly, You have to adjust them manually:
logd (loge, logv and others)
Select the logd item and press "Edit variables"
Change expression to: kotlinMethodName()
Also, remove ; from the end of the template, as you don't need it in Kotlin.
Now your method name will be shown correctly
logt
This one is a bit trickier.
Solution 1: TAG = class name.
Template text :
private val TAG = "$className$"
Edit variables -> Expression:
groovyScript("_1.take(Math.min(23, _1.length()));", kotlinClassName())
Solution 2: TAG = file name (can be used inside Companion)
Template text :
private const val TAG = "$className$"
or:
companion object {
private const val TAG = "$className$"
}
Edit variables -> Expression:
groovyScript("_1.take(Math.min(23, _1.length()));", fileNameWithoutExtension())

Yet not added log template in Kotlin Live templates section in Android Studio.
Settings -> Editor -> Live Templates -> Kotlin for kotlin templates.
Settings -> Editor -> Live Templates -> AndroidLog for AndroidLog templates
.
So you can't get the same AndroidLog templates in Kotlin code.
So now Question is How to use same Log Functions using templates in Kotlin?
Ans: You can add same Log templates (AndroidLog Templates) in Kotlin Templates section in Android studio as below example.
Then It will be available in your Kotlin code!
I hope in this way you can get an advantage of Log functions templates in Kotlin.

You don't need to copy paste anything as mentioned in the other answers. Locate the "Applicable in *" text in the Live Templates section in Android Studio - clicking on "Change" button gives a list of all scopes. Choose Kotlin (or one of it's child nodes) and apply. Note that some statements from Java will not work in Kotlin (like the ones using static keyword) - It's better to create a new template for those, rather than editing the existing one.

Here is the repository which contains all the templates for Android-Kotlin.
All you need to do this is download the repository, copy the templates directory and paste in your Android Studio config folder.
The config folder for your Android Studio can be found at
Windows: C:\Users\%userName%\.AndroidStudio<version>\config\templates
Linux: ~AndroidStudio<version>/config/templates
macOS: ~/Library/Preferences/AndroidStudio<version>/templates

This is the combination of all good answers by #pRaNaY #Leo and #Jaguar with bonus of TAG-free and variable-ready usage (no need to type "${}")
Template text: android.util.Log.d("$className$", "$methodName$ $$$contents$")
Applicable in Kotlin: top-level, statement, class, expression
variable expressions can be found in dropdown menu
here you can see whole picture

For logt the above two answers works but additionally need to add "define" as "class"

Related

How to search and replace all occurrences of a string in Android Studio

I am doing translations on android studio and now I need to replace some strings that I had wrongly translated. When I search using CTRL+SHIFT+F I indeed get all occurrences but when I use the replace shortcut CTRL+SHIFT+R and try to replace I get a quick popup saying Occurrences in project configuration files are skipped then there is a button which says Include them. When I click that button, the find window opens then nothing happens. How do I search and replace from the ENTIRE project including the configuration files?
Select string and use Alt+Shift+R as Eclipse shortcut and Refactor your string, like below screenshots.
There really is no point in editing config files (because all of them are generated by Android Studio) so just don't click the button on the popup you described.
If I am mistaken then tell me what kind of config files you might want to edit.
Edit -> Find -> Replace in path
and then In Project
In Android Studio:
Edit -> Find -> Replace in path

How to see the type of variable within Android Studio IDE

How to see the type of variable in Android Studio when using Kotlin without type annotations.
Currently I'm trying to see it by giving the variable a type I know is incorrect and the IDE informs me with an error about the type it expected.
val variable = dont().know().type().ofThis()
To give example, I can press CTRL+P to see the signature of a function, is there a similar way to see the inferred type of a variable?
You can enable Parameter name hints for that
To do so, go to File -> Settings -> Editor -> General -> Appearance and check the Show parameter name hints. Be sure to click configure button and at the Options panel, check the required options, including show local variable type hints:
There is a menu item in the View menu: "Type Info"
Ctrl+Shift+P in Android Studio Win 3.5.3
Hakan Karaduman, thank you for the question!
I see that you found the correct answer, however, the Android Studio IDE is changed its Settings menu. Therefore, I decided to provide an updated location for the options that you requested.
In Android Studio 2021.1.1 Patch 1 the required list of options is located File > Settings > Editor > Inlay Hints > Kotlin. That location consists of the following groups:
Parameter hints:
.. [v] Show parameter hints
...... Exclude list..
Types:
.. [v] Show hints for:
...... [v] Property types
...... [v] Local variable types
...... [v] Function return types
...... [v] Function parameter types
Lambdas:
.. [v] Show hints for:
...... [v] Return expressions
...... [v] Implicit receivers and parameters

Shortcut to generate "Created by.." template in Android Studio?

I have a project with a lot of classes in which I need to change the Created by template.
/**
* Created by johnnyfivedev on 19.07.16.
*/
Since there are a lot of such classes, copy and paste not an option. Rather I want to remove that generated template and regenerate it. Is there any default shortcut for doing this? If not, then how do I create one?
Rather I want to remove that generated template and regenerate it.
You can surely edit the template but you can't regenerate it again. I suggest editing the template for your future uses and creating a live template for those made prior to this template change.
Editing the template
Go to Settings -> Editor -> File and Code Templates -> Includes -> File Header
and override the ${USER} function like so:
#set( $USER = "Your name")
If you want, you can add a lot more variables to it.
NOTE: These changes will only take effect on new files. The ones made prior to this template change will have to be manually changed.
Press Ctrl+Alt+S to go directly to the File and Code Templates tab.
Creating the live template
Go to Settings -> Editor -> Live Templates
Press the green 'plus' sign to add a new template, and select Live Template.
In the abbreviation field, type what you want to, I typed annot, add a suitable description (optional) and insert the following code in the box:
/**
* Created by $USER$ on $DATE$.
*/
$END$
Select the Edit variables box and write the following expressions in the corresponding fields,
user() for USER
date("dd-MM-yyyy") for DATE
After that type define applicable contexts as you see fit. In your code, now type annot and press Enter to insert the template.
In the code, your cursor will end up at the place of $END$ upon pressing Enter after template insertion.
you can change your code generation template in settings of the android studio where you can change what name appears and date.
Here is a snapshot of where it is.
but this won't work for your existing files where you can change manually.

Remove dagger generated classes from Android Studio's global search?

It is really irritating. I need more time to find some class because of dagger 2.
If you are talking about the generated MembersInjector and Factory classes:
MyClass_MembersInjector.java
MyClass_Factory.java
you can prevent these from coming up in the Ctr-N or Cmd-O dialog by adding them to the ignored files list in File / Settings / Editor / FileTypes and adding the appropriate wildcards to the Ignore files and folders edittext:
*_MembersInjector.java; *_Factory.java; will cause most of the generated classes to be ignored:
Before:
After:
You can even add Dagger*.java to the list if you don't even want to see the generated component (even though this is rather useful for the project).
Update:
If you are talking about not having the classes appear in auto-import/auto-complete this is done through Settings / Editor / General / Auto Import:
David Rawson's answer doesn't help to get rid of not showing _Factory classes when performing Find Usages on the class name. This is what will be shown:
This can be resolved with creating a new scope which will disregard generated files.
Here's the regex for generated files in app module: !file[app]:build/generated//*. But you may as well use "Exclude recursively" button locating the directory you want to get rid of.
Now, change the search scope to newly created:
And this will be the output:
No _Factory classes. You may as well get rid of classes in test packages, thus only classes from production package will be found.
In Android Studio Dolphin there is a new option in Find Usages dialog, so you can ignore usages in generated code.
In Android Studio, go to
File -> Settings -> Editor -> File Types -> Ignored Files and Folders
and add wild cards. Enter apply.
For Hilt, you can add following flags/wildcards *_MembersInjector.java; *_Factory.java; *_Providers.java; *_Bindings.java; *_HiltComponents_*.java; *_Provide*.java

find and replace in android studio

I am looking for an efficient method to find and replace in android studio. where I can find a string using regex and replace the selected string with its substring.
e.g.
constants.LOG_FILE_PATH -> SETTINGS['LOG_FILE_PATH']
constants.LOG_DIR -> SETTINGS['LOG_DIR']
I have following regex for find
constants.[A-Z_]*
which selects constants.LOG_FILE_PATH and constants.LOG_DIR
but how to modify the replace string so that the above strings can be replaced by SETTINGS['LOG_FILE_PATH'] and SETTINGS['LOG_DIR'] respectively. I have to find and replace many strings in my project so manual find and replace is little overhead.
Edit 1: If you know how to do in pycharm or intellij-idea it will work in android studio also, thats why i have added tags pycharm and intellij-idea
Perhaps look at Structural Replace. Use the Find Actions shortcut (CMD + Shift + A) and type structural and the option should appear.
I guess something like the following may work:
Search template
constants.$log$
Replacement template
SETTINGS["$log$"]
Disclaimer, this is untested.

Categories

Resources