Android Studio Live Templates , Just What You Want!

Excellence is not enough!

Android Studio Live Templates , Just What You Want!


Notice: Undefined offset: 1 in /home/multithr/public_html/wp-includes/media.php on line 70

In this post i’m gonna talk a little bit about android studio/Intellij Trick That makes you faster in every day programming and makes your IDE’s environment more friendly .

that is Android Studio Live Templates .

maybe you’ve used some of android studios convenient phrases to type down frequently used methods like :

logd : Log.d()

loge : Log.E()

Toast : Toast.makeToast() , etc…

but what if you define your own templates to use every day , that could be awesome.

so let me say that i’ve defined a method in a dedicated class for debugging :

public class LoggingUtils {
    private static final String TAG = "LoggingUtils";

    public static void debugD(String tag, String message) {
        if (BuildConfig.DEBUG) {
            Log.d(tag, message);
        }
    }

    public static void debugE(String tag, String message, Exception e) {
        if (BuildConfig.DEBUG) {
            Log.e(tag, message, e);
        }
    }
}

 

it’s a better logging method because  it will rip off the logging content from code in release version , but when you proceed along to use this , you will find it harder to use because it requires you to include all of logging attributes ( TAG , “Message” , … ) , so it definitely slows down your speed . the solution is here :

go to File > Settings > Editor > Live Templates . (in Windows)

Android Studio Live Templates

Here you can find all of predefined ide’s live templates ,you can skim over to find more phrases to use later . but now we want to define our Template.

select android and on the left pane click green plus ( + ) button and select Live Templete . in the ” abbreviation ” edit text type your desired abbreviate  of  method. then in the ” Description ” type something that you later find out what this template is for .

in the ” Template Text ” you have to define what you’ve got in your method also you should pay attention that there is no import option so you have to include all of your objects reference path and fully qualify your class paths ,  like :

// include refrence path in your code 
my.refrence.to.path.utils.LoggingUtils.debugE(TAG ,"$METHOD_NAME$: $content$", $exception$);

to define dynamic(changeable) part of our code use ” $ ” sign before and after phrase :

"$METHOD_NAME$: $content$"

you can also put some annotation to your code to indicate that for example you want IDE to include something like ” method name ”  automatically to your code ,  for this click on the ” Edit Variable ”  on the right in the opened dialog you can choose an ” Expression ” for example methodName() or className() , etc…  to be included automatically into your template .

there is some other options but didn’t use the tool further than  above , so if you know more than this bring it forward and out it on the comments to be added later into this article .

 

 

Comments are closed.