Create shortcuts for methods in Android Studio - android

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.

Related

Why is still Android Studio using Hello World TextView?

Everytime I create a new project in Android Studio, by default I get in my MainActivity, a TextView containing "Hello World", which every single time I need to remove it. Why that? Is there any option that can allow me to create a new project witout it? Thanks!
Everytime I create a new project in Android Studio, by default I get in my MainActivity, a TextView containing "Hello World",
That is because of the specific choices that you made in the new-project wizard. You chose to create an activity, and you chose an activity template that has that particular content.
Is there any option that can allow me to create a new project witout it?
Opt not to create an activity when you create the new project. In other words, in this screen, choose "Add No Activity":

Shortcut to generate "Created by.." template in Android Studio?

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.

Android Studio fbc Live Template

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.

Can I design the UI in Xamarin.Forms by using XAML

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.

Android Graphical UI Builders - Connect event

Maybe this question has been ask already, but could not find any answer for almost 2hours of internet search.
There is a graphical UI designer wich is coming along with the last android SDK.
Looks pretty cool and well done.
Nevertheless I * cannot find how to attach an event to the control through the graphical editor.
Of course I can add it manually into the xml, but in that case, what's the purpose of having such tool without that function ?
I mean all the other SDK I had in other languages always include that function.
I've also not been able to find doc about how to use this tool. Quite sad...
Thanks
If you want to add a click event handler, select the button (widget) in the GUI that you want to listen for, and look for the property onClick. Enter the name of the method you want to call when the user clicks on that widget, like.. onMyButtonClick
Then add the method to your Activity
public void onMyButtonClick(View v) {
// I heard the button click
}
The GUI builder is getting there, and is not yet as easy to use as the one in XCode, but it's not hard when you get used to it.

Categories

Resources