Android studio auto fix - android-studio

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

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

How to set max line length in Android Studio code editor?

How can I set a maximum length for Android Studio's code editor, or may be set a word wrap?
In my Android Studio, no matter how long a statement I type on a single line, it never automatically goes to the next line, but rather a horizontal scroll bar appears.
When I autoformat using the Ctrl+Alt+L (reference), nothing happens.
My SO or Google search found a lot of material on this for Eclipse, but not for Android Studio.
If you need not a soft wrap,
but actually, brake one long line onto several lines you need:
Go to Preferences(mac) or Settings(windows) -> Editor -> Code style -> Java/Kotlin -> Wrapping and braces (tab) -> Check "Ensure that right margin is not exceeded"
Now try to reformat your code:
press
OPTION (ALT) + CMD + L on your Mac
(or CTRL + ALT + L on PC)
Edit 13.02.19
As noted in comments this option seems not available in settings for Kotlin.
However, there is a workaround to manually add it.
Here is what it takes to make it work:
Export the settings scheme.
Open saved xml file.
Find <codeStyleSettings language="kotlin"> tag (or create)
Add the WRAP_LONG_LINES setting manually:
<codeStyleSettings language="kotlin">
...
<option name="WRAP_LONG_LINES" value="true" />
</codeStyleSettings>
Save edits and and import back the scheme.
Here you go!
MacOS:
Click on Android Studio ( left top corner )
Click Preferences
Type Code Style in search
Click on Code Style
There is a filed right to the 'Hard wrap at' - you can enter needed about of symbols you need
In File | Settings, select "Editor", then "Code Style". There is an option "Wrap when typing reaches right margin".
Edit: Just tried it, and it doesn't seem to work. Anyway, maybe the option "Use soft wrap" in the "General" group is more what you want.
#Solace, actually #Fahim Faysal's answer is quite close to what you need, but since you want to solve the problem no matter how long a statement I type on a single line, it never automatically goes to the next line, 2 more steps you need to follow:
[Step 9] type the max length you prefer at `"Hard wrap at" ()
[Step 10] change "Wrap on typing" from Default: No or No to Yes
With version 4.1.2 and with Dart language you need to go to "File/Settings/Editor/Code Style/Dart" and modify the "Line length" to the preferred number.
In Android Studio:
File->Setting->
Editor->code Style->java->Wrapping and braces(tab:right side)->keep when reformatting->Line break(make unchecked)
You can change the max column width from:
File > Settings > Editor > Code Style > Right Margin (columns)
Android Studio 3.5:
Mac -
Android Studio > Preferences > Editor > CodeStyle > HardWrap at: ____
The existing answers already answer the question in straight forward way. As a different approach you could set the style guide to kotlin style guide code style. Then if you do alt + ctrl + l as you did, you'll see auto wrap as you expected.
Just open the settings, search for kotlin and look for kotlin under code style.
On the top right hand side of the settings window you'll see Set from..., click on it.
Then on predifined style > kotlin style guide
If you'd like to follow the kotlin formatting style this could reduce more effort. For more information: https://developer.android.com/kotlin/style-guide
My Environment:
Ubuntu 18.04
AndroidStudio 3.3.2
My operate:
File -> Settings -> Editor -> Code Style
Scheme -> select Project
Hard wrap at '120'
Apply
To move the grey vertical line, that tells Android Studio where to wrap your code, go to
Settings- Editor- Code Style- Java or Kotlin- Wrapping and Braces- Hard wrap at
and enter the number of characters, where the line should be.
For dart
settings => code style => Dart => Line length
In settings/code style/kotlin/wrapping and braces replace the "do not wrap" values with "wrap if long"
You can change the Hard wrap at param

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.

How to disable automatic check out from Visual Studio 2012?

Whenever I open any solution in VS2012 from local path (which is mapped in TFS), the files are automatically checking out. I want to disable this feature.
I tried to disconnect from TFS and to work on it, But it is closing the solution. Plesase help me out in achieving this. I didn't get this problem before when I was using VS2010
You need to unbind your project(s) from TFS source control.
Select the project or solution from the Solution Explorer
From the main menu select File->Source Control->Advanced->Change Source Control...
Select one or all projects and press the "Unbind" button on the top of the dialog
select ok to the warning popup
You can later bind the projects again, personally I don't do this so I can't say how well it works. I found some more info here
Here are the visual steps:
This brings up the dialog to select projects to bind/unbind.
I had to select one at a time.
Select "Unbind" for each project
Here's what it should look like when unbound.
You can later come back and bind the projects again and TFS will try and reconcile your changes.
Try rather change Checked-in items (Saving, Editing) to 'Prompt for check out'
Tools -> Options -> Source Control -> Enviroment -> Checked-in items
Disable the following option. It should solve your problem.
Tools -> Options -> Source Control -> Enviroment -> Get everything when a solution or project is opened

Remove unused imports in Android Studio

I recently started Android Studio IDE to make my android apps. I find shortkey to remove unused import in Android Studio is not working (Ctrl+Shift+O)
What is shortcut key to do same in Android Studio?
Simple, right click on your project in Android Studio, then click on the Optimize Imports that should work.
Update
To do same thing which I described above, you can do same just pressing Ctrl+Alt+O, it will optimize imports of your current file and your entire project depends on your selection in a dialog.
You can do it on the fly. You don't need to call (Ctrl+Shift+O) or "Project/Optimize Imports..." each time.
Just set this checkbox in Settings -> Editor -> General -> Auto Import -> Optimize Imports on the fly.
On OSX: Preferences -> Editor -> General -> Auto Import -> Optimize imports on the fly
Press Ctrl + Alt + O.
A dialog box will appear with a few options. You can choose to have the dialog box not appear again in the future if you wish, setting a default behavior.
It is very Simple Just Follow the below step.
Switch your project in Project Mode.
Then right-click on project name.
The final step is to select the Optimize imports from popup menu.
Enjoy!!
On Mac use control + option + O
Sorry for the late answer.. For mac users command + option + o
Try this.. It is working for me..
Ctrl+Alt+O works pretty well and removes unused imports
there is also an Android Studio (1.4) setting to optimze imports on the fly: see Settings->Editor->General->Auto Import.
I think Ctrl + Alt + O works when the import is unused but a valid import. However, say you try to import a class from a package that does not exist or no longer exists (which can happen during refactoring), the shortcut command does not work (atleast it didn't for me). If you have more than one file like this, things can get problematic.
To solve this problem, click on Analyse -> Inspect code -> (select your module / project). Let it perform the analysis. Go down to Imports -> Unused imports. Click on the "Delete unnecessary import" button that appears on the right.
here Android Studio Chipmunk | 2021.2.1 Patch 1
windows 10
ctrl+alt+o
worked perfectly
Press Alt + Enter with the cursor on top of the import. The Optimize imports menu will show. Press Enter again. Your unused imports will be removed.
Since Android Studio 3+, this can be done by open the option "Optimize imports".
Alt+Enter the select "Optimize imports".
This must be enough to removed the unused imports.
On MacOS (Monterey) + Android Studio (Arctic Fox 2020.3.1):
To Remove unused imports, follow below procedures.
Android Studio -> Code -> Optimize Imports
Shortcut key: Option + Command + O
Please see screenshot for more clarification. Thanks.
Specifically for Flutter Just set this checkbox Settings -> Languages & Frameworks -> Flutter -> Organize imports on save
you can use Alt + Enter in Android Studio as Shortcut Key

Resources