Android Studio : How to reformat code of entire project? - android-studio

I am looking for a way to reformat code of entire project, which is available for individual files via code -> reformat code.

Right click on app folder in android studio , from left panel . You will see the option for Reformat Code

Select your sources folder like "java" and use the command Ctrl + Alt + L
OR
Right click on source folder say for instance "java" and select "Reformat Code". A dialog "Reformat Code" would be shown. Select checkbox "Include subdirectories" and hit "Run".

Related

Android Studio | Navigate to file in the project explorer from the editor

I find myself with many files open in the editor (in separated tabs), and sometimes I want to find specific file in the project explorer.
Does there is any shortcut to reveal the file in the project window ?
EDIT to do the question more clear I add a picture:
There isn't a single key combo that can do this, as far as I know, but you can go with
On Windows
Alt + F1 followed by either 1 or enter
On Linux (using Gnome)
Alt + Shift + 1 followed by either 1 or enter
On Mac OS
Option + F1 followed by either 1 or enter
Android Studio is based on intelliJ, which has a function to open the current file or symbol in various views. The default key combo (in Windows) is ALT + F1.
This offers to open the current file or symbol in a bunch of different views, but the first option (with shortcut key 1) is the project view.
It's the same as choosing the Navigate > Select in... menu option, followed by option 1.
Note that you can customise the key mappings, or select other defaults instead of the default intelliJ configuration, so this might not be the case with your set up.
You can check the key mapping in settings > Key map and search for Select In... or, more simply, click the navigate menu and look at the Select In... option to see the shortcut key displayed in the drop down.
You can go to Navigate -> Select In, which has default shortcut Option+F1 on macOS.
For Xcode, the shortcut is Cmd+Shift+J. If you want the same, follow these steps
Go to Android Studio -> Preferences -> Keymap
Search for Select In in Navigate
Right click and select Add Keyboard Shortcut
Press Cmd+Shift+J

How to create my own 'Reformat Code' style in Android Studio?

In Eclipse,(Window -> Preferences -> Java -> Code Style -> Formatter) there was a facility to create my own format style and apply it in the code. Is there a way to do that in Android Studio(for Java code) ?
To change formatter options go to File | Settings | Code Style | Java
To reformat your code manually, press Ctrl + Alt + L (or using the menu Code | Reformat code...). To reformat the whole source code of your module, just select the module folder first.
More information about reformating source code can be found on the IntelliJ Web Help
In Android Studio 3.3.2 (running in Mac OS) "Android Studio" -> "Preferences" -> "Editor" -> "Code Style" -> "Java".
I would be useful to find a way to export/import only code format settings in order to share them between the team.
Open global settings/preferences via ⌘, or Android Studio > Prefences.
In the left hand side bar, go to Editor > Code Style.
open the scheme: drop down menu and select Default IDE.
Click the cog menu to the right of the drop down menu and select duplicate....
Then rename your new profile, and change the general settings as well as the settings for each language in the sub menu of Code Style as you see fit.
Then with you new profile selected, reformat your code with ⌘ + ⌥ + L
These style are global and can be used in other projects.
Same method applies for Code Cleanup too.

shortcut in Android Studio to locate the current editing src file

Do you know how to check the location of current editing file in the project tree panel (the very left panel of the Android Studio), except manually. (The worst case is that all the folders there are collapsed)
The Navigate->Class shortcut can show me the src file in edit panel (central panel) quickly. Then I want to know the src file's logic location in project tree (left panel), so that I can add some new files in the same package quickly. (e.g. right-click the package and add new Class).
Currently, I have to expand the project tree and find it folder by folder.
Actually, in Xcode there is such a shortcut named "Reveal in Project Navigate"
What a convenient way it is.
I wonder whether this is also supported in Android studio.
Thanks
You can use the Scroll from Source button in the Project Toolbar in the left of Android Studio.
Another solution is Alt+F1+1. You can also read Locate current file in IntelliJ for more answers.
Just enable the following option!
In Android Studio 4 it is now Always Select Opened File under the Settings menu in the project view.
For automatic scrolling, you can do following:
You need to ensure Autoscroll to Source and Autoscroll from source is enabled.
Whenever you change the file the Project tree will be highlighted according.
I have observed some slowness and lag if project size is big(several modules or huge code base) and you use short cut Ctrl + N to navigate to a class.
So the alternative would be to either to click following icon:
OR
Use keyboard shortcut:
Alt + F1 + 1 or Alt + F1 + Enter
You can add shortcut (keymap) to "reveal current editing file in project explorer" command.
Press Ctrl + Alt + S, for Windows user, to open Settings
Select Keymap on the sidebar
Find the command using search box, type this into it: select in project view
Right click the result then select Add Keyboard Shortcut
Type any keyboard combination you want, for example, me prefer shortcut Ctrl + K to reveal current editing file on project explorer
Click OK button. It will prompt you to remove keyboard shortcut you typed from existing command, so just click Remove button.
I'm new to android development. I used to code in Sublime Text 3. One of first thing I do to make android development feels more enjoyable is binding all shortcut I used in Sublime Text 3 to Android Studio. Now, it feels like home :)
For Mac users: Option+fn+F1, then Enter.
Android Studio 3.6.3 (April 2020)
Go to Preferences > Keymap & search for "Select in Project View" and assign your keyboard shortcut.
Under options menu, select Auto scroll from Source
Navigate -> Select In.. -> Project View -> Project
open pref -> Keymap, then search 'select file in project view', not 'select opened file'.

Visual Studio Shortcut for building all T4 in solution

I just upgraded my VS2012 Proff to Ultimate and lost some settings. One I lost I can't find anywhere. It is a button I which builds all T4s in the solution. Anyone have an idéa?
I'm assuming it's somehow disappeared from the Build menu.
Right-click a toolbar or the menu bar and go to Customize.
Choose the Commands tab
Select Menu bar = "Build"
Press "Add Command"
Under Category "Build" you should see "Transform all T4 Templates"
Click ok to add it back to the build menu
[Optional] Move it up or down to where you want
Click close
The command should also be on the Build toolbar - you may need to do Add Or Remove Buttons or Customize to get it to come back. You can follow the same steps as above but instead choose Toolbar = "Build" in step 3.

No Android facet found, issues compiling with Android Studio 0.2

So, following the advice on this SO post (sorry, I would comment there expect I don't have enough reputation)
Gradle: FAILURE: Could not determine which tasks to execute
I removed the tags from my .iml files, which allows me to compile. But when I go to run the .apk on a device, I get the error
"Error running TestProject: No Android facet found for TestProject"
Can someone help please, just want to get back to writing Android apps!
Add an android facet to your module by following below steps.
1) Go To File Menu -> Project Structure, or press (Ctrl+Alt+Shift+S) shortcut to open "Project Structure".
2) Select "Facets" which is under the "Project Settings" tab. (First column)
3) Click on "+" button which is at the top of the Second Column to add new facets.
4) Select "Android" facet from that "Add "menu which will prompt another dialog box to select a module. (Select a module to which you want to apply this facet).
5) Select your module and that's it. :)
Hope this will solve your problem.
At the toolbar left of the run and debug button there is a drop down. Select your project main activiy.
In my case, it helped to just go to "Edit Configurations..." and delete the current configuration. Then I just created a new one with the default options.

Resources