How to create my own 'Reformat Code' style in Android Studio? - 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.

Related

Where is Android Studio Markdown support plugin preview preference?

I want to show preview for markdown.
But it is not shown.
I am using Markdown support by JetBrains. And Android Studio 3.1
I found IntelliJ help.
https://www.jetbrains.com/help/idea/markdown.html
But I can not see that preference.
How can I see markdown preview?
Updating JetBrains Runtime (JBR) will solve the issue.
Start the IDE, use Help -> Find Action, type "Choose Boot Java Runtime for the IDE", press Enter.
Select the newest version with JCEF to install.
Click the OK button and wait until the file with JetBrains Runtime is downloaded and configured.
Restart the IDE and Enjoy.
Video(44S): https://youtu.be/Mtu3dUgK480
Go to File>Preferences>Editor>File Types then select Files Opened In AssociatedApplications and add *.md to the list
Reassign the wild card
Do likewise for the Text file type
Enable the Markdown
Go with Vladimir Schneider
Preview like a bawse!
UPDATE: As mentioned by #desgraci below. It's now a paid plugin. It doesn't even show up. It would ideally work as indicated in the updated documentation but some requests left in the reviews.
I'm unable to make it work.
I suggest giving up and opening it in Visual Studio Code.
Right-click tab with your file, select Split Right.
In second instance, right-click tab again and select Open in Preview.
Now you have text and preview at the same time.
Taken from https://issuetracker.google.com/issues/159933628?pli=1#comment7
In the Android Studio:
Find action (ctrl + shift + A / command + shift + A)
Search for Choose Boot Java Runtime for the IDE
Select the latest version in
the "New:" dropdown - e.g. 11.0.12+7-b1504.27 JetBrains Runtime with
JCEF OK Restart
Worked in: Android Studio Chipmunk | 2021.2.1 Patch 1 | Build #AI-212.5712.43.2112.8609683
There is another plugin which is "Markdown Navigator Enhanced" and that I tested on Android Studio 3.5.2, it enables a toolbar with some editing functions as well as the preview options.
You can Edit, preview or do both in a split window, you can even show the HTML code.
The plug-in is paid, but it has a 30-days trial that can be good for a short usage.
Download plugin first,and then you'd better do something to active it:
Windows:
AndroidStudio ->File -> Editor ->Coe Style -> File Types->Markdown-> + -> *.md -> ok -> ok
Mac:
AndroidStudio -> Preferences -> Editor -> FileTypes -> Markdown-> + -> *.md -> ok -> ok
Then enjoy writing in md.
This is not just a JavaFX issue, which can be resolved by installing Choose Runtime plugin and choosing a different JDK. The Markdown plugin itself also references classes from IntelliJ UI, which means that even with a runtime that includes JavaFX, it still requires IntelliJ and won't work properly with Android Studio:
Caused by: java.lang.ClassNotFoundException: com.intellij.ui.javafx.JavaFxHtmlPanel PluginClassLoader[org.intellij.plugins.markdown, 193.6494.42] com.intellij.ide.plugins.cl.PluginClassLoader#4fa7c8ac
at com.intellij.ide.plugins.cl.PluginClassLoader.loadClass(PluginClassLoader.java:77)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 93 more
It will still work with the default bundled JDK, just reduced functionality without the preview panel.
References:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/360003498520-Mardown-plugin-doesn-t-show-preview
https://intellij-support.jetbrains.com/hc/en-us/articles/206544879-Selecting-the-JDK-version-the-IDE-will-run-under
A little late but you can go to Settings/Preferences -> Editor -> File Types. Select Markdown Language from the list. Below, in the Registered Patterns section, add *.md, press ok, and restart Android Studio
Elijah's answer is great, but you may have to do ↓ after his actions.
Preferences -> Editor -> FileTypes -> Markdown-> + -> *.md
Since there is no good plugin for Android Studio IDE, If you have VS Code, I recommend Markdown Preview Enhanced plugins.
Or if you work a lot with Notepad++ , you can install the MarkdownViewerPlusPlus plugin
Follow folowing steps for Notepad++ MarkdownViewerPlusPlus plugin setup:
Download MarkdownViewerPlusPlus, you get a .dll file
Open notepad++ -> plugins -> open plugin folder
create subfolder "MarkdownViewerPlusPlus", paste your .dll file
restart notepad++, in plugins menu options now you should see MarkdownViewer++.
File -> Editor -> File Types->Markdown-> + -> *.md -> ok
File -> Editor -> File Types -> ASSOCIATE FILE TYPES WITH ANDROID STUDIO -> select all -> ok -> yes
File -> Plugins -> Marketplace -> search "Markdown" -> install -> APPLY -> OK
Help -> Find Action -> "Choose Boot Java Runtime for the IDE" -> Select the runtime that comes with JCEF support -> OK
restart Android Studio
My solution to this issue is to preview on VS Code
Go to File>Preferences>Editor>File Types then select Files Opened In AssociatedApplications and add *.md to the list
when you open the md file it will ask if you want to open it with your favorite file editor select vs code
Alternatively you can use markdown editors separately. Here are few options:
MacDown for Mac
Typora
GhostWriter
Few more online editors:
StackEdit
Dillinger
Editor.md
Draft
In the Android Studio:
Find action (ctrl + shift + A / command + shift + A)
Search for Choose Boot Java Runtime for the IDE
Select the latest version in the "New" dropdown - e.g.:
11.0.16.8-2043.64-jcef JetBrains Runtime with JCEF
OK
Restart
Worked in:
Android Studio Electric Eel | 2022.1.1
Build #AI-221.6008.13.2211.9477386, built on January 11, 2023

Android Studio add watch shortcut

Is there any shortcut for Add Watch in android studio?
While debug windows is focused I can use Alt + Insert, but this is not working in code editor.
There is no direct shortcut for Add to Watches but you can define your own.
Go to File -> Settings -> Keymap
Expand 'Other' folder at last and find 'Add to Watches'.
Double Click / Right Click on it and select 'Add Keyboard Shortcut'.
Enter your keys and Done!
Did you try to assign a combination to your liking under preferences -> keymap -> search for "watches"?

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'.

How to format code in Android Studio (IntelliJ IDEA)

I want to format code in a freshly created Android application.
I messed up the code in this manner:
private static final
int AUTO_HIDE_DELAY_MILLIS
= 3000;
I want to format it with Ctrl + Alt + L, but it is not repairing the code. I use Windows 8.1.
Just right-click on the project or directory, and select Reformat Code
If you want to:
Order/FormatCode: Ctrl + Alt + L
Implement Methods: Ctrl + I
Override Methods: Ctrl + O
There isn't any need for them to be capital letters. This is just for the example.
First you will need to configure how your code is formatted. This can be done by going to
[Main Menu] -> File -> Settings
then go to
(Project Settings) -> Code Style -> Java
In this area you can define all the formatting rules that you would like to apply to your code.
Now you should check (or change) the keyboard shortcut for reformatting code by going to
[Main Menu] -> File -> Settings
then go to
(IDE Settings) -> Keymap
Just type Reformat Code into the search box at the top right to find it.
Once this is done, highlight the code you want to reformat and use the keyboard shortcut to reformat your code based on the rules you defined.
In Android Studio (on the Mac), I select the code chunk, and then select the Code menu item and click Auto-Indent Lines.
See screenshot:
Shortcut key (on Mac) is Ctrl + Alt + I.
At least on my Android Studio version, "keep when formatting - line breaks" was enabled by default. So you need to go to
Preferences → Editor → Code Style → Java and disable that checkbox (see the below screenshot).
Alt + Ctrl + Shift + L is the shortcut key for arranging the code in Ubuntu (Android Studio).

Android studio auto fix

I don't know what the exact name is for s feature like this, but I will just describe it.
I am using android studio and I added some java classes from a different project. Now it says
"missing package statement: yada yada". But there is not option in the error popup to just automatically add the missing package statement. I know it seems lazy to not type it, but I like to do things quickly.
When there is an error like this, is there a way for android studio to automatically added the needed lines of code like eclipse would?
This is a common frustration. While not a perfect solution, I found https://stackoverflow.com/a/16616085/967131 to work for me.
For Windows/Linux, you can go to File -> Settings -> Editor -> General
-> Auto Import -> Java and make the following changes:
change "Insert imports on paste" dropdown value to "All"
check "Add unambiguous imports on the fly" option
On a Mac, do the same thing in Android Studio -> Preferences
That way, as you type, or when you copy and paste, many imports will be added automatically. Those that aren't are ambiguous; put the cursor on the class, press alt+enter and select which version of the class you meant.
For Windows/Linux, you can go to File -> Settings -> Editor -> General -> Auto Import -> Java and make the following changes:
change "Insert imports on paste" dropdown value to "All"
check "Add unambiguous imports on the fly" option
On a Mac, do the same thing in Android Studio -> Preferences

Resources