Pages

Thursday, October 10, 2013

Create Code Template for Log.d in Eclipse and Android Studio

Hi,

I've just found a way to save a lot of time while developing Android apps using Eclipse or Android Studio, by creating a shortcut for the typical Log.d statement.

Eclipse (or ADT)

In Eclipse go to Preferences > Java > Editor > Templates and click on "New...".
This will let you create a new Code Template.

Fill in the form as followed:

  • Name = logd
  • Context = Java statements
  • Select 'Automatically insert'
  • Description = Log.d shortcut
  • Pattern = ${:import(android.util.Log)}Log.d("${enclosing_type}", "${enclosing_method}: ${cursor}");
Press OK to save the template and choose OK to close and apply the changes.

Now go inside your Java-file where you want to place a new Log.d, type "logd" and press CTRL + Space.

The code template will be printed in your editor and you can start writing what you want. For example it will print Log.d("MainActivity", "onCreate: "); and your cursor will be ready after "onCreate: "
It will also automatically import android.Util.Log.

Android Studio (or intelliJ)

In Android Studio it's pretty much the same. Go to Settings and select Live Templates in the list under IDE Settings. I've selected output in the list of Live Templates and pressed the plus sign on the right-hand side to add a new Live Template.

Fill in the form as followed:
  • Abbreviation = logd
  • Description = Prints a Log.d statement
  • Template text = android.util.Log.d("$CLASS_NAME$", "$METHOD_NAME$ (line $LINE$): $MESSAGE$");$END$
  • Shorten FQ names = checked
Next, go to Edit Variables and fill in the table as followed:
  • MESSAGE
    • Default value: "Write a log message"
  • LINE
    • Expression: lineNumber()
    • Skip if defined: checked
  • METHOD_NAME
    • Expression: methodName()
    • Skip if defined: checked
  • CLASS_NAME
    • Expression: groovyScript("if(_1.length() > 23) { return _1.substring(0, 23)} else { return _1}", className())
    • Skip if defined: checked
The reason why I've used a groovy script is because Android Studio will give an error if the tag name is longer than 23 characters.
Last but not least, select an applicable context. I've just selected the whole Java field.
Here's a screenshot of what it should look like


Apply changes and you're done.
Just type logd and hit tab (unless you've changed it to something else). Your Log.d message will be displayed, start typing the preferred message and hit Enter. Here's an example of output:
Log.d("MainActivity", "onCreateOptionsMenu (line 43): Write a log message");
Or
Log.d("MyWayTooLongClassNameAc", "onCreateOptionsMenu (line 43): Write a log message");

Once again, the Log class will automatically be added to the imports.

Good luck with this and don't forget to share ;-)

9 comments:

  1. Learn How to Create an app with AppQuick App Builder. Create an app for Andriod and IOS With no codding. We learn you also how to earn Money from app.

    For More Information Visit Here -- how to make an app

    ReplyDelete
  2. I want to learn this kind of layout! Teach me.

    ReplyDelete
  3. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this More info

    ReplyDelete
  4. Wow what a Great Information about World Day its incredibly charming instructive post. soap 2 day

    ReplyDelete
  5. Indulge your senses with the irresistible charm of Pumpkin Flavor of Love. From its sweet aroma to its rich, creamy texture, this delectable treat embodies the essence of autumn. Whether enjoyed in a cozy latte or a decadent dessert, Pumpkin Flavor of Love evokes feelings of warmth and comfort.

    ReplyDelete