Android Studio: 'wrap in container' in XML layout - android

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 .

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"

Autoinsert right-to-left attributes Android Studio

I don't know all features of Android Studio, I have tried to search,but found nothing.
I wonder if it is possible to make Android Studio autoinsert attributes required for right-to-left support.
For example I have typed following line
android:layout_marginLeft="10dp"
Is it possible to make Android Studio insert marginStart attribute automatically ?
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
And vice-versa(for padding too).
This would save some time.
Maybe someone knows how to get such behavior, I will be grateful for any help. Thx.
Apparently, you can't do that. You have to write them yourself.
The difference between layout_marginLeft and layout_marginStart for example is that layout_marginLeft is executed for left to right languages like English, while `layout_marginStart' is only executed for right to left languages like Arabic.
So if you are going to add string translations to your app including arabic or any other right to left languages, you will need to write marginStart or marginEnd attributes... So anyway, if you didn't write layout_marginStart for example and your app doesn't support Arabic language or any other right to left languages, no error will occur to the user; it is just a warning that Android Studio tells you.
Definitely the same concept is applied to padding attributes. Hope that helps you.
Edit:
If you don't like to see Android Studio warning you these warnings, you can simply disable that by clicking on the yellow light bulb beside the yellow highlighted warning and selecting Edit 'Using left/right instead of start/end attributes' inspection settings, then uncheck it from the list.
But if you don't want to change the inspection settings, you can just add the following to your View that you don't want to use start/end attributes in it:
tools:ignore="RtlHardcoded"
and add that to your parent layout that contains that View:
xmlns:tools="http://schemas.android.com/tools"

Robojuice / Butterknife #InjectView AutoFormatting issue in Android Studio

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.

can't edit specific xml file in Eclipse

There are similar questions, but none address my specific issue.
I'm rather new to Android development and Eclipse but was able to follow the tutorials A OK.
In fact, I'm able to edit activity_main.xml from a tutorial, but now, in a test project I'm working on, I can't edit my new activity_main.xml (again, different project, same filename).
I created the file by right-clicking on the layout folder then selecting NEW > OTHER > Android XML Layout File. I was able to copy some of the other file so that my file now looks essentially like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
</LinearLayout>
When I type, the cursor moves around (looping through the text) but no characters appear on the screen. Again, in the other XML file I can edit with no problem.
I'm hoping that this is just some newbie problem.
I have had similar problems, though not this exact problem. Sometimes simply restarting Eclipse, or whatever editor you use, can help. Occasionally, Eclipse will not detect changes in your xml so you need to clean and rebuild the project. You can do this by
Winodow --> Project --> Clean...
and choose your project
I know there was an issue that I had which was similar with the editor jumping/replacing lines and such which I believe was fixed in a newer version of Eclipse. If I can find a source later I will edit
Clear all of text in layout activity_main.xml (in code view)
Drag and drop new LinearLayout in layout (in graphical view)
Go to Project -> Clean

Categories

Resources