You can create new Java classes and interfaces through the menu or by right-clicking on a package in the Project window. Regardless of whether you want to create a class, an interface, an enumeration, or any other sort of Java artifact, you always use the New Class dialog. Within that dialog, you can select what type of file you're creating. And then after creating the new file you can move it to another package using Android Studio's refactoring tools.
- [Voiceover] As with all Java-based environments, Androids apps tend to include more than one or two classes, and you can easily create new classes, interfaces, and other coding components from the Android Studio menu. I'm working with a new project I've called Simple App, to follow along, you can create your own new project or work with any other Android app. Go to the menu and choose file, new, Java class, notice there aren't any options here for interfaces, enums, or other styles of files, instead that choice is made here in the dialogue.
I'll create a simple class named "MyTextUTO" and I'll click okay. By default, the class is initially placed in your base package. If you want to move it to a different package, first create the new package and then move it there. I'll go to my base package, right-click, and choose new package. And I'll name the new package "Utilities". Then, I'll drag and drop my existing class into place. When I drop it, I see the move dialogue, and then I can click the refactor button and that will move the class and change any existing code to match the new location.
In this case, it changed the package declaration at the top of the class, I'll get rid of these comments. Now I should say that on Mac, when you do that drag and drop operation, you might run into a bug. If you do, it's being caused by the current version of the JDK combined with the current version of intellij idea. I didn't see it on my system because I've downgraded by version of Java on my computer to Java 8, update 51. If you're using a more recent version, you might see that bug.
So that's how you create new classes, interfaces, and other Java components in your app. Again, there's only one menu choice for all of them, and you make the choice of what you're trying to create in the dialogue box itself.
- Installing Android Studio on Mac and Windows
- Creating Android Studio projects
- Setting up the development environment, including HAXM and the new Android emulator
- Importing existing code into Android Studio projects
- Exploring the interface, including the editor and project windows
- Managing project builds and dependencies
- Creating new Java classes
- Refactoring code
- Using templates
- Using breakpoints and watch expressions
- Updating apps with Instant Run
- Using Git for version control
Skill Level Beginner
Q: This course was updated on 04/27/2017. What changed?
A: New videos were added that highlight the new features introduced in Android Studio 2.3. In addition, the following topic was updated: update apps with Instant Run.