Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: how to use the fbc live template

Android Studio has support for macros—which they like to call live templates—which is nice, but nobody anywhere bothers to explain how to use them, least of all Android Studio. It is obvious that the "fbc" live template was designed to make findViewById easier to use, but I can't figure out how to use it. How exactly do you use this template to produce a piece of boilerplate like this, for instance:

EditText e = (EditText) findViewById(R.id.m);
like image 993
enigmaticPhysicist Avatar asked Apr 04 '16 01:04

enigmaticPhysicist


1 Answers

After much fiddling, I did eventually figure it out. On a blank line, you type "fbc" then hit tab. That gets you this:

    (|) findViewById(R.id.);

with a red cursor placed at the "|". You enter the object type, possibly using tab or enter to autocomplete. You might need to hit tab or enter again after that to move onto the next field:

    (EditText) findViewById(R.id.|);

Repeat for the ID. That gets you this:

    (EditText) findViewById(R.id.m)|;

The whole line will be underlined because it is an expression and not a statement. Any time you have a line with an expression on it by itself, though, you can hit Alt-Enter, then Enter again to select "Introduce Local Variable" and assign the expression to a new variable, thus making a statement:

    EditText |viewById| = (EditText) findViewById(R.id.m);

It generates a new variable name automatically. If you're fine with it, just hit enter to finalise. If you want a different variable name, start typing the new variable name before hitting enter. Their variable name will automatically be replaced, giving the final result:

    EditText e = (EditText) findViewById(R.id.m);|

And that's how you use the "fbc" live template! IMO, this should be a part of every Android tutorial.

Edit: I later realised the fbc template was poorly made and it's far easier just to fix it, going into the settings and replacing its template text with this:

$cast$ $var$ = ($cast$) findViewById(R.id.$resId$);

That does the whole thing all at once. It just looks a little weird until after the values are filled in.

like image 166
enigmaticPhysicist Avatar answered Sep 22 '22 19:09

enigmaticPhysicist