Robojuice / Butterknife #InjectView AutoFormatting issue in Android Studio - android

I am adopting the #InjectView theme throughout my project and have a minor issue when it comes to condensing these declarations. After I put in the initial code it looks great in one line like this..
#InjectView(R.id.tv_report_summary_name) TextView tvReportSummaryName;
However if I do an auto format (Ctrl + Shift + F), Android Studio is automatically replacing this with two lines so it looks like:
#InjectView(R.id.tv_report_summary_name)
TextView tvReportSummaryName;
Is there a way to avoid this or modify my formatting options to make this not happen? Any help is greatly appreciated.

When you open Preferences -> Code Style -> Java.
Choose Tab Wrapping and Braces.
After that scroll to bottom and there is option to set this style as is shown in the image below:
Choose which is wright for you.

Follow this :
File -> Settings -> Editor -> Code style -> Java -> Wrapping and Braces -- Field Annotation and select the checkbox (Do not wrap after single annotation)
Hopefully, it will solve your problem.

Related

Prevent android studio to create more than a tag in xml files

In the new android studio 3.3 or 3.2 if we create a for example button auto generate create a text like
<Button android:layout_width="" android:layout_height=""/>
but I would like something more like this
<Button
android:layout_width=""
android:layout_height=""/>
like old versions of Android studio is there a setting or something for that?
That is related to XML code wrapping. By default it should be on wrap always but maybe it's not in your Android Studio or they changed that in newer versions. Anyway you can find that by click on:
Preferences -> Code Style (expand it) -> XML -> Android(Tab) ->
Here you can find Layout Files and below options like: Wrap always, Don't wrap, Wrap if long etc. So set it on Wrap always and Apply changes and your XML code from now on should be wrapped.
It hints something that says reduce intent with 4 spaces. Try to configure that :)
Or use the hard way by pressing enter for the new row.

Android Studio, where is the style to modify the line return on the "foreach" command

I can't figure how to Android Studio handle the styling of the foreach live template by putting it on two line like this:
for ( :
) {
}
In the Editor -> Live templates -> Android -> foreach we can unselect Reformat according to style and it stays on one line but it looks like a temporary fix.
I want to find where (according to style) is, to be able to modify it.
Thanks
I think it's a bug related with 'Reformat according to style' option.
As you know, to prevent wrong wrapping, turn off 'Reformat according to style" option for 'foreach' Live Template.
Editor > Live Templates > Android > foreach > Reformat according to style
Here is my explanation:
There are styles for 'for()' statement in
Code Style > Java > Wrapping and Braces > 'for()' statement
It makes wrapping when using 'Live Templates' with 'Reformat according to style' option. It's not about Code Style rule because it doesn't wrap when already typed foreach loop.
After some testing with template text and applying Code Style, I figured out the problem is WHEN Code Style applied. 'Reformat according to style' option is applied BEFORE template text interpreted, so Code Style for 'for()' applied to below text and it wraps.
for ($i$ : $data$) {
$cursor$
}
Type that template text in editor and 'Reformat Code', it wraps like this.
for ($i$:
$data$) {
$cursor$
}
That's why live template works like that.
It's just a bug and you can use the temporary fix until bug fixed.
The solution for me was simply going to the given place
Editor -> Live templates -> Android -> foreach
and unchecking Reformat according to style
You can modify it at the very same path you mentioned in the question.
Editor -> Live templates -> Android -> foreach
select foreach and at the bottom you see a textarea with the formatting/style. Modify it as you wish and click Apply
Update
The style you are referring to is i think the code style.
Can be found at
Editor -> Code Styles
Check this screenshot"

How to comment my XML files in Android Studio

I am learning Android and want to be able to add line comments to my XML so I can remind myself what things are doing.
Is there an accepted practice for this, or is it possible in Android Studio? I recall reading that comments in XML are hard, so I wanted to know if that was also considered true here.
As mentioned above you can comment in XML just like this:
<!-- Comment -->
But in android studio you can very easily toggle comment just by selecting what you want to comment and then pressing:
CTRL + /
This not only for XML but also for your java code for example.
It is indeed possible. I'm not sure if there is an accepted practice, but you can accomplish leaving a comment like this
<!-- comment goes here -->
Same as in all XML applications:
Just use <!-- ... --> to comment.
Comments in XML aren't too tough. You have to do it like this:
For example
<!-- This is a comment -->
Please don't hesitate to ask for anymore help. The above should work in android studio.
If you are using a Mac then command is command(button which looks like Windows start button ) + /
If you are on Windows then it is
Cntrl + /
Try and see if it works
In Android Studio DOLPHIN version I have tried using the
and the file does NOT like it. It produces errors when used.
I don't know about XML files much, but this AndroidManifest.xml file does not like this. Any other tips that might work? This file is error free before the comment line is added.

Cannot enter unicode text in Android

I need to enter the text in unicode directly from the keyboard (Vietnamese for this time) but nothing happens. It once works with another simple app but I don't know what am I missing so this app won't allow unicode input.
I've tried
Properties the project -> Resource -> Text file encoding -> Other -> UTF-8
or <?xml version="1.0" encoding="utf-8"?> in every xml activity or even in AndroidManifest.xml but no luck.
Is there anything I missed? Most of the tutorials I found are listed above.
Thank you for your time.
Project properties only configures new files.
Edit the properties for each file and change the encoding to "UTF-8".

Android Studio: 'wrap in container' in XML layout

When editing XML layout files, Eclipse has a feature called 'wrap in container' (Reformat -> Android -> ...) that lets you select one or several Views and wrap a layout of your choice around it.
Is there something similar in Android Studio?
It's currently being implemented:
Issue 69000: Add the "Wrap In" visual refactoring - Android Issue Tracker
Tor Norbye, Android Issue Tracker Mantainer:
We should implement the "Wrap In" visual refactoring.
Just came across this, and to complete Simas' answer:
using AS1.2, this is easy to achieve...
to set up a Live Template, simply select the View in your layout XML
that you wanna surround. Hit Alt+Shift+Z (surround-with)... a little
popup appears. Click Configure Live Templates.... Or use File->Settings->Editor->Live Templates as described above.
Select the html/xml section and hit the little +. Give it a name and description, then fill in template text:
<LinearLayout
android:orientation="$ORIENTATION$"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
$SELECTION$
</LinearLayout>
Click Edit variables, set Expression:complete(), Default value:vertical... hit OK. Then, find the Define' button for applicable context... and select XML.
You're all set now. To use the template, select the View that you wanna surround in the XML, hit Alt+Shift+Z (surround-with)... a little popup appears. Select the template you just defined... Done!
You can use Live Templates for that. Here's a nice article about them.
Edit:
New templates can be added through File->Settings->Editor->Live Templates.
To find the hotkey for your keymap go to File->Settings and write "Live template". For me it's CTRL + J .

Categories

Resources