Android Studio fbc Live Template - android

fbc live template can be used as described here:
android studio: how to use the fbc live template
But this is very long and time-consuming process. Also, the change described in the answer will create default variable name e.g. textView for TextView object and we again have to rename it. So again, this takes more time than expected and unnecessary cursor movement.
How to use fbc in a more efficient way such that it requires less key strokes?

We can achieve this by making only a small change in fbc live template.
Make a change in template:
Press Ctrl + Shift + A
Select Live Templates Settings
Select fbc under Android and click on Change and make sure that Statement and Expression are checked. Then, click OK.
Use the template:
Declare your variable:
TextView myVar;
write your variable name which you can do pretty fast using code completion and assign fbc and then
myVar = fbc + <TAB>
this would expand to
myVar = (TextView) findViewById(R.id.);
Now, press TAB, select id and press TAB. You are done.

Related

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.

How to make cursor jump to specific location in Android Studio live template?

I'm trying to modify the AndroidLog live templates in Android Studio 2.2.
The default logd template expands to
android.util.Log.d(TAG, "$METHOD_NAME$: $content$");
But usually, when I am logging, I want to log the variable values and I want auto-completion to help me with it. So I modified this template to the following:
android.util.Log.d(TAG, "$METHOD_NAME$: $content$" + $content$);
However, when I expand this, the cursor still lands on the first occurrence of $content$. I want it to land on the second occurrence so Android Studio can suggest the variable name via auto completion!
For example, if I want to log the user's login time from an instance of User, the log statement would something like this
Log.d(TAG, "isSessionExpired: user.getLoginTime():" + user.getLoginTime());
So I want the cursor to be after the plus sign for auto completion to help me.
How can I achieve this with Live Templates?
Please see how soutv live template is defined, you want something similar for your logging:

Android Studio shortcut to show difference of a class from within that class

Is there any keyboard shortcut in Android Studio to show difference of class file, as compared to last commit, from within that class.
Currently I can see file difference from VCS (Local Changes) tab only by either selecting file and pressing Ctrl + d or from menu, as shown in image. There is another way but it involve number of mouse clicks.
There is no such a shortcut by default, you could always assign it yourself, but there is the operation you need
(you could search for any operation using another shortcut Cmnd + Shift + A) :
In order to assign a shortcut to the operation, you should go to Preferences -> Keymap -> search for 'Compare with the Same Repository Version' and assign a shortcut on it.

Create shortcuts for methods in Android Studio

Is there a way to generate my out shortcuts, like sout or soutv and so on, in Android Studio ? I'd like to make something like mab and then press ctrl+spaced which will generate a predefined method and then let me fill out the params for it. (so basically the same as sout)
Yes you can, they're called Live Templates,
here is an example of someone doing it
https://github.com/manhluong/peng/blob/master/PengAndroid/src/com/luongbui/peng/android/InstallWatchAppTask.java#L50
Here is the IntelliJ docs
https://www.jetbrains.com/idea/help/live-templates.html
To create a new template from scratch
In the Settings dialog, open the Live Templates page, and expand the
template group where you want to create a new template.
Click add (+).
A new template item is added to the group and the focus moves to the
Template Text area.
Specify the new template abbreviation, type the
template body, define the variables and the template group, configure
the options, as described in the template modification procedure.
Click OK when ready.

IntelliJ Live Templates bug with completeSmart() function

I am trying to create a Live-Template in Android Studio that will somewhat speed up creation of findViewById(...)
Part of it is self-learning, and part is curious in creating such live-templates.
I want to be able to type in the following...
Button find + (enter)
...and that should create something like this...
Button btnAdd = (Button) findViewById(R.id.btnAddition);
My current implementation has issues...
...and these issues stem from the use of completeSmart(). Whenever you use completeSmart(), the template ignores everything after that call and ends editing.
For example, If I moved the CAST named-variable up one slot (above ID), then the cast would be automatically filled out and it would not let me easily edit ID anymore. Same as below picture, but without btnAddition. Just ...findViewById(R.id.);
With my current setup, I have CAST on the very bottom so I can easily edit all of the named-variables; however, the use of completeSmart() does not let the template end (Place the cursor by the $END$ marker - Line 29 in the picture below) when I am complete.
Instead, it places the cursor right after the cast, like so...
...when it should be placed on the start of the next line. This template does put a new line in, but the cursor does not go there at the end. Why?
So I want this...
1 ) Since I already but in Button, I want to cast to be Button. I should not have to type in in twice!
you should use method typeOfVariable()
you can change like this:

Categories

Resources