Why does Android Studio throw away my Code Style settings? - java

I am trying to write an Android app in Java. In Android Studio, I go to
File->Settings->Editor->Code Style->Java
Then for Project, I modify the curly braces (I can't read code with unaligned curly braces), click apply, and finally click OK.
But then I reopen the settings to find that my settings have been discarded and it has reverted my code style.
How do I prevent it from reformatting to a different style?

I'm not sure if I undestand properly your question but I think that you want to put "Braces" in the next line, isn't it?
You must edit at:
File->Settings->Editor->Code Style->Java
Then,
Select Project or Default -> Wrapping and Braces -> Braces Placement -> NextLine
Let me know if works?
For me, it is working fine:

Related

I can't figure out how to disable Netbean's code completion without disabling pop-up of suggested code

How do I disable code completion but keep suggestions enabled? Every time I type object.Method() without passing any parameters I get something similar to object.Method(datatype). Netbeans would complete the code after the first parenthesis, (. When I don't want to pass any parameters, I simply type ) once. I would get something like this thanks to auto complete: object.Method())
The null in between () would be replaced by the ) I typed.
I want to be able to see suggested code without ctrl-space and without having netbeans automatically insert the code for me. In netbeans I can disable the auto insertion of the second bracket } after I type the first {. I don't have an option to disable parentheses from auto inserting. The problem is that if I disable code completion, I also disable the automatic pop-up that gives me related code. Any advice?
If you do not want the IDE to perform the project updates, you can disable the build analyzer as follows:
Right-click the project node in the Projects window and select Properties.
In the Project Properties dialog box, click the Code Assistance category.
Deselect the Use Build Analyzer option.
https://netbeans.org/kb/docs/cnd/HowTos.html
It's been awhile since I used NetBeans (personally I prefer IntelliJ), but I believe you're using the wrong setting. Try this: under Tools>Options>Editor>General, uncheck "Insert Closing Bracket Automatically".
See also this other question: NetBeans curly braces auto-closing

Disabling Eclipse auto-complete but keeping auto-activation

I use Eclipse for Java development. I like the auto-activation feature because I can add all the letters and numbers so I can see class members while I'm typing; making it behave similarly to Visual Studio. If I don't add all of the letters and numbers to the auto-activation list, it will only pop up with class information when I'm typing the first letter after the '.' delimiter, and then it disappears.
The real problem arises when I'm typing in a variable name or something else. As soon as I hit space, the auto-complete types a bunch of crap following what I've typed in attempt to complete it. It's driving me crazy!
Any way to turn this off?
The checkbox you have to check is: "Disable insertion triggers exept 'Enter' "
(under Java -> Editor -> Content assist)
I'm on Eclipse 2018-12 and C_H's answer had no effect on the behavior:
I believe what you'll want is to deactivate "Insert single proposals automatically"
EDIT: It seems this answer is now outdated. It may work for old Eclipse versions. For any more recent versions refer to #ChrisP's answer from 2019.
Open menu Window, then Preferences. Follow path Java -> Editor -> Content assist. Now mess around with the settings to find your ideal setup. I believe what you'll want is to deactivate Insert single proposals automatically.
If anyone wants to know how to stop the auto-complete for brackets:
Toggle off: "Edit --> Smart Insert Mode"
Version: Eclipse Photon
In continuation to #G_H's answer :
This worked for me although I am sure there is another and a better way.
Window -> Preferences -> Java -> Editor -> Content Assist -> Set Auto Activation Delay(ms) : 200ms from 0ms in my case.
You may want to increase/decrease the value 200 depending on your typing speed.
The reason this worked is because of the hitting of "Spacebar" before the delay arrives.

Next / Last Edited Position Eclipse Shortcut - And Netbeans?

I remember in Eclipse there were buttons in the toolbar that took you to the last edited position in a document.
This was great cause you could write some code, click on a method to go check out the code there, and then click "back" to get back to where you were coding.
Any idea if this can be done in Netbeans?
Certainly, it's unbound by default but you find the setting in Tools -> Options -> KeyMap. Search for last for instance to find the setting.

AutoIndent in Eclipse possible?

I have been wracking my brain trying to figure this out. For the first time I used jEdit the other day and I was pleasantly surprised that it auto indented my code (meaning that I'd put in the following code:
int method () {
_ //<-- and it put me here automatically
I've tried to get the same thing working with eclipse but with no success. I got into the code formatter but I don't see how to make that happen.
Is it possible to do this? Also while I'm here, is there a such thing as a eclipse plugin that will allow you to search the methods and classes of the standard java library?
Thanks
Personally all I use for this is the format options Window->preferences under Java->Code Style ->Formatter.
I once took the time to tweek how I like my code to look like when I work and exported the whole thing. After that I just code without too much bother on what it looks like. When I find the code looks messy by pressing the combination ctrl+shift+f and the whole class becomes pretty again, comments and all.
After a while it pretty much became a reflex...
code code code
ctrl-s, ctrl-b (cause I disable auto build sometimes), ctrl-shift-f
code some more etc...
Once I got used to this I never really cared how it presented the code as i was typing because I knew it would look all pretty as soon as the loop/if/switch/method etc is finished
My clean eclipse install does this by default.
Have you changed any options? Make sure the file you are editing has the .java file extension. The preference options that control the typing automations are under Java -> Editor -> Typing in the Window -> Preferences menu.
Also, I find that the auto-indenting, and most of the other auto-complete functions of eclipse do not function well if the file I am editing has errors in it which prevent compilation. Make sure that your curly-braces are matched correctly, this is the main one that I've noticed blocks auto-indent.
Regarding searching through the standard Java libraries, use the Search -> Java.. menu option, and check the JRE libraries checkbox, then search away. You can also use the Hierarchy view to see how the classes relate. Also, in the Package and Project views you can expand the JRE System Library, and then expand rt.jar which holds pretty much all the standard Java pacakges.
Eclipse has always done this for me by default.
One really cool thing about eclipse is that you can search preference pages. Just right click and go to prefrences. Go to the "Window" menu, and click "Prefrences". Then at the top of the tree view there's a text box that says "type filter text". Replace that with "indent" and it should bring up the page where the indent option is.
Make sure that eclipse recognizes your file as a java file, that you're using the Java distribution, the latest version, etc.
Iv been trying to work around the eclipse indenting and other supposed features for years, and it seems that the bottom line is this ...
It only works for the programming style of the authors, so to use it you need to modify your style to comply.
This would be OK except that the authors of eclipse have some very strange ideas about common shortcut keys.
One horrid example is the search features, eg when did Ctrl+K become "Find Next occurrence" and why doesnt F3 or n work?
That all being said I use eclipse because if you have the time to wait around while it starts up - or never close it - and you can modify everything youve learned about using an editor - why why why - then it will certainly increase your efficiency.
Please note that there is a preference setting for indenting, it can be set for a project, a workspace, or globally, but no matter how you set it eclipse will still chuck tab characters in where you dont want them.
In fact its indent crazy, like it wants to indent everything, even if its already indented.
Like I said Iv been using it for years and it STILL drives me nuts with its random behavior.
Follow these steps for Eclipse:
Select all text: ctrl+A
Correct indentation: ctrl+I
You should check:
Hidden features/tricks for Eclipse?
What is your favorite hot-key in Eclipse?

Eclipse and curly braces

Is there a quick way to make Eclipse put curly brace on the next line (by itself) on a block of code?
Yes, edit your active profile (Java...Code Style...Formatter), and change the brace positions to the next line. Also, in Java..Editor..Typing, you can have it automatically insert your braces at the correct position.
For pre-written block of code, first do the settings as suggested by Don and then select that piece of code and right click Source Code->Format and the formatting would take place as per the settings done in the preferences.
Current versions of eclipse have a more convenient way of doing this.
Go to Preferences->Java->Code Style->Formatter
Click on edit, and on the new opened window, go to the Braces tab. Here you can choose how different types of blocks of code organize their curly braces.
Hope this helps any who google this, and find this post. (Like myself)
Regards to all!
The simplest and global way:
Go to Window -> Preferences.
Then in search put: "brace".
Select -> Java -> Code Style-> Formatter
After opening new window go to brace tab and change it as you prefer.
Coming from C# background I couldn't come to grips with built in Java formatting of Eclipse.
With some effort I managed to get most of what Visual Studio used to offer me. Here is an XML version of it. You can import the file..
Preferences->Java->Code Style->Formatter->Import->Apply
Additionally see arguments on which approach is better on our sister site..
In addition to the methods of changing the settings and ctrl-shift-f to apply, these settings can be done on a per project basis. Simply right-click on your project, choose properties, Java Code Style, and enable the checkbox for "enable project specific settings."
I find this invaluable for use between projects I work on to configure it to match a particular client style, along with other settings to try to match code formatting of existing code. This way, you won't upset other people but can still use your own style on other code bases. :)

Categories