Configure Your Android Studio

Source: Internet
Author: User

Source: Medium Translation Source: Liukun (@TheFresher)

The main content of this article comes from Dmytro Danylyk in Medium's article configuring Android Studio. Thanks here! Although the use of Android Studio has a certain amount of time, but the text of some of the settings are not known, and the original in the Great Wall, so in the translation of the original based on a number of additions, if new discoveries will be updated immediately. Of course, the original text of the individual skills is not high, out of respect for the author, still translated all over, I hope to everyone, especially just contact with Android Studio friends have a little help.

Previous words

Android Studio provides a very handy feature to help us import or export settings. So we usually import a previous setting when installing a new Android Studio . But, a tragic thing happened recently-I lost My settings file (gloating a bit like ~).

My advice: Instantly backup your settings files to cloud disks and other places.

When I am configuring my Android Studio , some of the following configuration tips may be helpful to you.

Show line Numbers

When I first started my Android Studio , the first thing I wanted to do was I wanted to see the line numbers in the file, and I always wondered if this basic configuration was not turned on by default?!
I just want to say four words about it:

Line number is not displayed

Show line Numbers

Configuration method

    • File | Settings Open Settings
    • Select Editor General | appearance
    • Tick Show Line numbers

Set up

PS: Select Show Line Numbers on the left-hand side of the editing area to let the currently open file display the row number, but this is a temporary setting that expires when the current file is closed.

Hump selection

in Android development, we usually name the variable using the hump naming method , but when we change the character selection by Ctrl + left/Right C18>android Studio does not support the choice of ' hump ' words by default.

Hump Selection Not supported

Support Hump Selection

Configuration method

    • File | Settings Open Settings
    • Select Editor General | Smart Keys
    • Select the use "camelhumps" words

Set up

Note: If you still want the entire word to be selected when the mouse is double-clicked on a word, you need to set the following:

    • File | Settings Open Settings
    • Select Editor | General
    • Uncheck ' Honor Camel humps words settings when selecting on double click '
Naming prefixes

We usually follow the official Android Guidelines for coding style to name the field. In the Android source code we can see that the usual member variables start with ' m '. In fact, Android Studio can automatically add a custom prefix to help us generate field names, such as:

    • Non-common, non-static member variables start with ' m '
    • Static member variables start with ' s '

Naming prefixes

Configuration method

    • File | Settings Open Settings
    • Select EditorCode Style | Java
    • Select the Code Generation Tag
    • Add a ' m ' prefix to the normal Field and add a ' s ' prefix to the Static filed

Set up

Quick Guide Package

In Android Studio , we can use ALT + Enter and Control + ALT + O to guide the package and clear the useless guide package, but we all live in 2016 years, these things should When done quickly and automatically.

Imports on the fly is not turned on

Open imports on the fly

Configuration method

    • File | Settings Open Settings
    • Select Editor General | Auto Import
    • Tick Optimize Imports on the fly
    • Tick Add Unambiguous imports on the fly

Set up

PS: Here I add, about this configuration, individual friends are asking, if my project in two or even more than one package has a class called Imageloader , the results of auto-import is not possible not I want to under the package? Brother, you can rest assured that you find out what unambiguous means. For cases where the package name is uncertain, Android Studio will choose No can no import, you can do up, will not be smart, but will let you manually import. So, please feel free to use.

Log Color

The default color of Logcat in the darcula theme is only red and white, which is not very convenient for us to distinguish the type of Log.

Darcula Theme Color

I suggest you use the vivid colors of the previous Android holo Theme

Holo Theme Color

Configuration method

    • File | Settings Open Settings
    • Select EditorColor & Fonts | Android Logcat
    • Click on Save as ... button to create a new color scheme
    • Modify the corresponding color according to the table below ( you need to uncheck the use inherited attributes before modifying)
Log Level Color
Assert: #AA66CC
Debug: #33B5E5
Error: #FF4444
Info: #99CC00
Verbose: #FFFFFF
Warning: #FFBB33

Ms. Cang's career line

Here are some of the personal additions:

Code color

The default code color in Android Studio The personal feeling is already very harmonious, but this thing is in the beholder. For example, some friends will feel that the default white of local variables in Java code is not easy to distinguish quickly with other code, this time you need to customize the Java code color, here is a local variable for example.

Default color

Custom Color Matching

Configuration method

    • File | Settings Open Settings
    • Select EditorColor & Fonts | Java
    • Click on Save as ... button to create a new color scheme
    • Expand the Variables below to select Local variable
    • Set the Foreground color on the right

Set up

Project templates

Android Studio created the Module without having to generate all the files that are commonly used in Android development, such as creating only one drawable folder by default, commonly known as folders like c4>drawable-hdpi need to be created by ourselves. As the author of the previous article said, it's been 2016 years, and these things should be done automatically, after all, we're all ' lazy '!

Default structure

Custom structure

Configuration Method 1

    • Enter the Android Studio installation directory
    • Go to pluginsAndroidLibTemplatesgradle-projectsNewandroidmoduleRoot | Res
    • Create folders such as drawable-hdpi under the Res folder (Optional: Copy iclauncher.png from the corresponding Mipmap folder to the drawable folder you created )
    • Go back to the newandroidmodule directory and use the editor to hit the recipe.xml.ftl file
    • Add the following configuration

Configuration Method 2

    • Enter the Android Studio installation directory
    • Go to pluginsAndroidLibTemplatesgradle-projects | Newandroidmodule
    • Open the recipe.xml.ftl file with the editor and add the following configuration

The difference between the two methods is that the first way to add the corresponding picture in the folder, but the configuration is slightly cumbersome, the second way is simple to configure, but only to create a directory, cannot contain the default picture.

Of course, we can do a lot of things in a similar way when we create the Module , such as:

    • Generate common colors in the colors.xml file
    • Build a custom configuration in the build.gradle file
    • Generate a custom ignore configuration in a . Gitignore File
    • Wait a minute...
Activity templates

There are many very handy activity templates (Live Templates) that are available by default in Android Studio , for example, when we enter sout and press the enter key, Android Studio will automatically help us write System.out.println ();

In fact, Sout is an active template with as its own.

As you can see, the activity template is an abbreviation of our common code. There's a lot of code in development that repeats itself, so customizing the right activity template can largely avoid many of our repetitive physical work. So here's the question, how do I customize it?
Here we take Handler as an example. Here's what a qualified Handler should look like in the Activity :

Java
123456789101112131415 private static class myhandler extends Handler { Private weakreference activityweakreference; Public myhandler(mainactivity activity) { activityweakreference = new weakreference(activity) ;         } @Override Public void handlemessage(Message msg) { mainactivity activity = activityweakreference. Get(); if (activity! = null) {             }        }    }

As for the reasons I will not repeat, there are too many articles on the internet has been said. Now if I only want to enter a ' PSH ' to automatically appear above this code, I should do this:

Configuration method

    • File | Settings Open Settings
    • Select EditorCode Style | Live Templates
    • Click the plus sign on the far right and select Template Group
    • Enter a name for the activity template grouping in the popup dialog box, such as custom
    • On the left, select the custom group you created in the previous step, and click the plus sign on the right
    • Select Live Template to PSH the input in abbreviation
    • Enter a description for this activity template in Description
    • Enter the following code in the Template text

    • Click the Define button below to select Java to represent this template for Java code
    • Click Edit variables on the right
    • Select className in the Expression drop-down box and tick the Skip if ...

The purpose of this operation is that as will automatically replace the className we wrapped with the ' $ ' character in the previous step with the class name that the current class does not contain the package name

    • Click Apply and Ok to let the settings take effect.

At this point, the psh activity template in one of our custom template groups is defined. Let us verify that the time has come to witness the miracle:

Configure Your Android Studio

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.