I have a project with a lot of classes in which I need to change the Created by template.
/**
* Created by johnnyfivedev on 19.07.16.
*/
Since there are a lot of such classes, copy and paste not an option. Rather I want to remove that generated template and regenerate it. Is there any default shortcut for doing this? If not, then how do I create one?
Rather I want to remove that generated template and regenerate it.
You can surely edit the template but you can't regenerate it again. I suggest editing the template for your future uses and creating a live template for those made prior to this template change.
Editing the template
Go to Settings -> Editor -> File and Code Templates -> Includes -> File Header
and override the ${USER} function like so:
#set( $USER = "Your name")
If you want, you can add a lot more variables to it.
NOTE: These changes will only take effect on new files. The ones made prior to this template change will have to be manually changed.
Press Ctrl+Alt+S to go directly to the File and Code Templates tab.
Creating the live template
Go to Settings -> Editor -> Live Templates
Press the green 'plus' sign to add a new template, and select Live Template.
In the abbreviation field, type what you want to, I typed annot, add a suitable description (optional) and insert the following code in the box:
/**
* Created by $USER$ on $DATE$.
*/
$END$
Select the Edit variables box and write the following expressions in the corresponding fields,
user() for USER
date("dd-MM-yyyy") for DATE
After that type define applicable contexts as you see fit. In your code, now type annot and press Enter to insert the template.
In the code, your cursor will end up at the place of $END$ upon pressing Enter after template insertion.
you can change your code generation template in settings of the android studio where you can change what name appears and date.
Here is a snapshot of where it is.
but this won't work for your existing files where you can change manually.
Related
I am doing translations on android studio and now I need to replace some strings that I had wrongly translated. When I search using CTRL+SHIFT+F I indeed get all occurrences but when I use the replace shortcut CTRL+SHIFT+R and try to replace I get a quick popup saying Occurrences in project configuration files are skipped then there is a button which says Include them. When I click that button, the find window opens then nothing happens. How do I search and replace from the ENTIRE project including the configuration files?
Select string and use Alt+Shift+R as Eclipse shortcut and Refactor your string, like below screenshots.
There really is no point in editing config files (because all of them are generated by Android Studio) so just don't click the button on the popup you described.
If I am mistaken then tell me what kind of config files you might want to edit.
Edit -> Find -> Replace in path
and then In Project
In Android Studio:
Edit -> Find -> Replace in path
fbc live template can be used as described here:
android studio: how to use the fbc live template
But this is very long and time-consuming process. Also, the change described in the answer will create default variable name e.g. textView for TextView object and we again have to rename it. So again, this takes more time than expected and unnecessary cursor movement.
How to use fbc in a more efficient way such that it requires less key strokes?
We can achieve this by making only a small change in fbc live template.
Make a change in template:
Press Ctrl + Shift + A
Select Live Templates Settings
Select fbc under Android and click on Change and make sure that Statement and Expression are checked. Then, click OK.
Use the template:
Declare your variable:
TextView myVar;
write your variable name which you can do pretty fast using code completion and assign fbc and then
myVar = fbc + <TAB>
this would expand to
myVar = (TextView) findViewById(R.id.);
Now, press TAB, select id and press TAB. You are done.
Hope you have worked with Live Templates which is given by Android by default.
Like:
Generate debug log statement: "logd"+TAB
Generate error log statement: "loge"+TAB
Generate info log statement: "logi"+TAB
Generate TAG declaration: "logt"+TAB
Generate parameter logging: "logm"+TAB
Generate method return log: "logr"+TAB
Which it is not available in KOTLIN?
Is it not available in Android Studio 3.0 Canary Version?
Here is the step-by-step guide:
Firstly, Copy and paste AndroidLog templates to Kotlin (Just select them and use CMD+C, CMD+V (or Ctrl+C, Ctrl+V)
Secondly, You have to adjust them manually:
logd (loge, logv and others)
Select the logd item and press "Edit variables"
Change expression to: kotlinMethodName()
Also, remove ; from the end of the template, as you don't need it in Kotlin.
Now your method name will be shown correctly
logt
This one is a bit trickier.
Solution 1: TAG = class name.
Template text :
private val TAG = "$className$"
Edit variables -> Expression:
groovyScript("_1.take(Math.min(23, _1.length()));", kotlinClassName())
Solution 2: TAG = file name (can be used inside Companion)
Template text :
private const val TAG = "$className$"
or:
companion object {
private const val TAG = "$className$"
}
Edit variables -> Expression:
groovyScript("_1.take(Math.min(23, _1.length()));", fileNameWithoutExtension())
Yet not added log template in Kotlin Live templates section in Android Studio.
Settings -> Editor -> Live Templates -> Kotlin for kotlin templates.
Settings -> Editor -> Live Templates -> AndroidLog for AndroidLog templates
.
So you can't get the same AndroidLog templates in Kotlin code.
So now Question is How to use same Log Functions using templates in Kotlin?
Ans: You can add same Log templates (AndroidLog Templates) in Kotlin Templates section in Android studio as below example.
Then It will be available in your Kotlin code!
I hope in this way you can get an advantage of Log functions templates in Kotlin.
You don't need to copy paste anything as mentioned in the other answers. Locate the "Applicable in *" text in the Live Templates section in Android Studio - clicking on "Change" button gives a list of all scopes. Choose Kotlin (or one of it's child nodes) and apply. Note that some statements from Java will not work in Kotlin (like the ones using static keyword) - It's better to create a new template for those, rather than editing the existing one.
Here is the repository which contains all the templates for Android-Kotlin.
All you need to do this is download the repository, copy the templates directory and paste in your Android Studio config folder.
The config folder for your Android Studio can be found at
Windows: C:\Users\%userName%\.AndroidStudio<version>\config\templates
Linux: ~AndroidStudio<version>/config/templates
macOS: ~/Library/Preferences/AndroidStudio<version>/templates
This is the combination of all good answers by #pRaNaY #Leo and #Jaguar with bonus of TAG-free and variable-ready usage (no need to type "${}")
Template text: android.util.Log.d("$className$", "$methodName$ $$$contents$")
Applicable in Kotlin: top-level, statement, class, expression
variable expressions can be found in dropdown menu
here you can see whole picture
For logt the above two answers works but additionally need to add "define" as "class"
Is there a way to generate my out shortcuts, like sout or soutv and so on, in Android Studio ? I'd like to make something like mab and then press ctrl+spaced which will generate a predefined method and then let me fill out the params for it. (so basically the same as sout)
Yes you can, they're called Live Templates,
here is an example of someone doing it
https://github.com/manhluong/peng/blob/master/PengAndroid/src/com/luongbui/peng/android/InstallWatchAppTask.java#L50
Here is the IntelliJ docs
https://www.jetbrains.com/idea/help/live-templates.html
To create a new template from scratch
In the Settings dialog, open the Live Templates page, and expand the
template group where you want to create a new template.
Click add (+).
A new template item is added to the group and the focus moves to the
Template Text area.
Specify the new template abbreviation, type the
template body, define the variables and the template group, configure
the options, as described in the template modification procedure.
Click OK when ready.
I'm trying to create a cross platform app using Xamarin.Forms. As far as I know, the UI will be created from the code and the .axml file will be generated automatically.
Can I modify the .axml file to edit the UI? I tried editing but all that comes up is what is written in the code. ie: hello forms
UPDATE
public static Page GetMainPage ()
{
return new simplerow ();
}
In Xamarin.Forms you can create your pages from markup definitions that are shared across all platforms.
Typically you will write all your content pages using Xamarin.Forms, however you can mix-and-match native pages into an application should you so wish.
These shared common pages, written in Xamarin.Forms, will reside maybe in a PCL project, or a Shared Project so these can then be re-used in the platform-specific projects, each targeting a specific platform OS.
You can write these common pages, either in code, or in XAML. You can even choose to write some pages one way, and some the other if you so choose.
A Xamarin.Forms page is processed at runtime through the interpretation of the page composition that has been created.
Each control that is specified on a page, has its own platform specific renderer, behind the scenes, that will produce output that is targetted to that OS.
When writing Xamarin.Forms pages, for-the-most, you will start to learn a new way of creating pages that is abstracted from the intricacies of creating mobile applications on each different platform OS.
There is therefore no editable .axml that is generated etc as you will write your pages using Xamarin.Forms markup and controls, and even your own or other custom-controls to produce your own application pages.
The following link shows some examples of how to write XAML pages.
The following link shows some examples of how to write from code-behind pages.
Along with the previous answer re: .xaml instead of .axml, you need to remember to change the startup code in app.cs to use your new .xaml form. Replace the "new ContentPage {...};" with "new MyForm();" (where "MyForm" is the name of your shiny new XAML form).
EDIT: Downloaded the project from the dropbox link. Comments below...
I see several issues here. I think you may need to go through the walkthroughs and sample applications provided by Xamarin to get up to speed with the concepts behind XF apps.
First, you are trying to use an Activity as your application's page. In a Xamarin Forms app, it must be a View of some sort, not a platform-specific visual such as Activity.
Second, remove the "test.xml" file from your Android project's Resources/layout folder; while XAML files are indeed XML, they have an 1) have a file extension of .xaml and 2) belong in the shared project.
Here's what you need to do to get your project working: (I'm assuming you're using VS here, under Xamarin Studio, it's slightly different.)
Right-click your "testforms" shared project
Click Add from the context menu and select "New Item"
In the dialog that appears, select "Forms XAML Page" and in the Name area enter a name (such as "MyForm")
(If you're using XS, choose "New File" and "Forms ContentPage")
This will add two files to your project: a XAML file containing your layout (e.g.: MyForm.xaml), and a code-behind file (e.g.: MyForm.xaml.cs).
Open the XAML file, and modify the Label element so that the Text attribute is
Text = "Hello, World!"
Modify the body of GetMainPage in your App.cs to the following:
return new MyForm();
Run the app
Hope this helps!
You got it wrong. Forms are created either through code or XAML. No axml or anything persistent is generated at platform level, everything is done in runtime(XAML is sort of compiled at compile time).
So, modify either code or XAML if you wish to change something. Or, if you need something more demanding, than consider either subclassing an existing Renderer or create you own.