Using Scratch Files in Android Studio

AnDroid Developer
4 min readJul 27, 2021

--

An awesome feature that increases your productivity and coding experience

Sometimes you may need to create temporary notes or draft up some code outside of the project context. Managing these extra code snippets could often be messy and complex. Or worse you might even be using different applications to copy and paste these extra snippets. Instead of switching to a different application for this purpose or messing up with your project, you can use scratch files and scratch buffers.

If you are blocked by paywall please refer to this post on StackOverflow.

Many of you may have worked with or at least heard about Android Studio. It is the most popular Integrated Development Environment for Android Application Development and comes with a huge range of features. It is based on community version of IntelliJ IDEA IDE, which provides awesome features for including the one we are going to cover in this post.

IntelliJ IDEA is an integrated development environment written in Java for developing computer software. It is developed by JetBrains, and is available as an Apache 2 Licensed community edition, and in a proprietary commercial edition. Both can be used for commercial development.

Scratch files is an awesome features packed with IntelliJ IDEA that lets you save your code that you want to look at later or just for reference. Not only that, Scratch files are fully functional, runnable, and debuggable files, which support syntax highlighting, code completion, and all other features for the corresponding file type.

For example, while working on one project, you may come up with an idea for a method that you could later use in another project. You can create a scratch file with a draft of the method, which is not stored in your project directory (So even if the code in Scratch File is buggy or has errors, it won’t stop you for compiling and running your app) but can be accessed and opened from another project. You can use scratch files to draft Java code constructs, HTTP requests, JSON documents, and so on.

Say you are creating a class and you decide to change a section of code. You can create a new scratch file by right clicking on the navigation bar and selection New, and then scratch file. Copy and save the code in the scratch file. This file is not a part of your android studio project.

You can find this file under scratches scope or Scratches folder under Scratches and Consoles folder in Project Scope.

You can access scratch files in project navigator under Scratches and Consoles Directory in Project Scope

How to Create Scratch Files

It’s really easy and simple to create a scratch file.

  1. Do one of the following:
  • From the File menu, select New, and then click Scratch File,
  • Press Ctrl+Shift+Alt+Insert, or
  • Press Ctrl+Shift+A, start typing scratch file and then pick the corresponding action.

2. Select the language of the scratch file. Scratch files of the same type are automatically numbered and added to the Scratches and Consoles directory of the Project tool window .

If you are interested only in taking some notes or non-code text, you can simply create a scratch buffer. Scratch buffers are same as scratch files, except for one difference that scratch files support all the coding features like code highlighting, formatting, validation, etc. but scratch buffers don’t do that.

Note: There is no dedicated menu item for the action to create a new scratch buffer, but you can use the Find Action popup Ctrl+Shift+A and run the New Scratch Buffer action. However, you can create a shortcut for the New Scratch Buffer if you use it often.

--

--

AnDroid Developer

This blog focuses on Technology articles related to Android Development