Practical Tips for Android Studio

Practical Tips for Android Studio

2018 Android Bus Global Developer Forum-Chengdu Station

The Android Bus Global Developer Forum Chengdu Station is about to start!

Introduction

I believe there are probably more articles introducing Android Studio shortcuts and usage tips than there are of us programmers, so having one more or one less doesn’t matter. Here are some shortcuts I use almost every day, hoping someone finds them useful. If you already know them, please go easy on me; after all, I am too young… (I am using a Mac, so please refer to Win/Linux accordingly, as they are quite similar).

Common Shortcuts

1. Cmd + C to copy, Cmd + Shift + C to copy file path.

2. Cmd + V to paste, Cmd + Shift + V to paste from clipboard, which retains the last five copied values. Usage scenario: When you copied value A, then copied value B, and now want to use value A again.

3. Cmd + F to find in file, Cmd + Shift + F to find in the entire project. Usage scenario: For example, if I want to find which file the “Network Error” is popping up in, I can use Cmd + Shift + F to enter the keyword, and it will list all files where that keyword appears.

4. Cmd + O to search for class files, Cmd + Opt + O to search for files in the entire project directory. Usage scenario: When you want to find a file but don’t want to open each folder, just use Cmd + Opt + O and enter the file name to find it.

5. Cmd + E to open recent files, Cmd + Shift + E to open recently edited files. Usage scenario: Lists all edited files for easy access.

Practical Tips for Android Studio
List of Recently Edited Files

6. Cmd + Shift + F7 to highlight code; some computers may require the fn key to activate. Usage scenario: Want to see where a class file is used, this operation makes it clear at a glance.

Practical Tips for Android Studio
Highlighting Stock

7. Cmd + Opt + L to format code. 8. Cmd + / for line comments, Cmd + Opt + / for block comments. 9. Exit these operations with Esc. 10. Cmd + Shift + – / + (this is your plus and minus key, just to reiterate, it’s the two keys next to the Delete key) to fold/open methods. Usage scenario: When you want to find where a method is defined, and the current class has a lot of lines and is messy, then this shortcut can help clear things up!

Practical Tips for Android Studio
Cmd + Shift + ‘-‘ to Fold
Practical Tips for Android Studio
Cmd + Shift + ‘+’ to Open

Here’s a screenshot of the shortcuts, with the GitHub link below.

Practical Tips for Android Studio
Shortcuts

XML File Related

When writing XML layout files, you often encounter issues such as ImageView Lint missing contentDescription prompts, API version mismatches, and the value of android:text=”” flashing on the screen. We can use tools to solve these issues; of course, these do not affect compilation, but for someone like me who has a coding cleanliness obsession, they are hard to tolerate. The tools attribute can be divided into two types: one that affects Lint prompts, and one that is related to XML layout design.

Tools Lint Prompts

1. tools:text: When writing layouts, sometimes the IDE can see the preview effect, but some effects can only be seen after running, such as this case: TextView in XML has no character set, but the text is set in the activity. Therefore, to preview the effect in the IDE, you must set the android:text attribute in XML for the TextView control.

    <TextView
            android:id="@+id/tvDaV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/honor_title"
            android:textSize="13sp"
            android:text="三金西瓜霜" />

Then tell yourself to delete it before going live; it’s such a naive thought, I almost never delete it. I believe you are like this too, so it’s better to use tools:text=”三金西瓜霜”. This value will show in the preview, but will not display on the real device. Just two steps: a. Add namespace xmlns:tools=”http://schemas.android.com/tools” at the root layout, b. Use tools:text=”三金西瓜霜” in the TextView. Tools can override all standard properties of Android; just replace android: with tools:. At runtime, tools: itself is ignored and will not be included in the APK, done.

Practical Tips for Android Studio

2. tools:ignore: This attribute tells Lint to ignore certain warnings in XML. For example, if we write an ImageView without the android:contentDescription attribute, Lint will prompt that this ImageView is missing the contentDescription attribute. So we can use tools:ignore to ignore this warning:

             <ImageView
                android:id="@+id/image_back"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:padding="8dp"
                android:src="@drawable/back"
                tools:ignore="contentDescription"/>

3. tools:targetApi: Suppose minSdkLevel is 15, and you use controls from Api21 like RippleDrawable, Lint will prompt a warning.

  <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:color="@color/accent_color"
        tools:targetApi="LOLLIPOP" />

4. tools:locale (local language): By default, strings in res/values/strings.xml will undergo spell check, and if not in English, will prompt a spelling error.

Practical Tips for Android Studio
tools:locale

Use the following code to inform Studio that the local language is not English, and there will be no prompts.

Practical Tips for Android Studio
No Error Prompt

Extract Resource

When writing code, we should try to reduce hard coding, meaning we should not directly use strings, specific size values, or color values in layouts, but should place them in resource files. However, for existing hard-coded methods, we may be too lazy to change or find it troublesome. Take a string as an example: first, we need to write an item in the String.xml file, then write the id, and the corresponding value, and then reference it in the layout using @string/xxxx… then silently tell ourselves to just write it directly. It’s actually not that complicated; just select the value, press Opt + Enter to bring up the menu, choose Extract String, and fill in the id in the pop-up window. If that id already exists, it will fail to generate.

Practical Tips for Android Studio
Extract Resource
Practical Tips for Android Studio

Finally, Here Are Some Links

Shortcuts: https://github.com/nisrulz/android-tips-tricks

What Everyone Is Watching

Android Login Page Imitation Lagou Smooth Animation Transition Effect

Latest Organized Android Open Source Libraries, Tools, and Open Source Projects Sharing

Android Gradle Plugin Development Guide

Custom Countdown Control Using Property Animation in Android

Welcome to the Android BusBlog Areato submit, technical growth through sharing

Looking forward to your commentsto discuss growth together!

Practical Tips for Android Studio

Leave a Comment