I am trying to create a Live-Template in Android Studio that will somewhat speed up creation of findViewById(...)
Part of it is self-learning, and part is curious in creating such live-templates.
I want to be able to type in the following...
Button find + (enter)
...and that should create something like this...
Button btnAdd = (Button) findViewById(R.id.btnAddition);
My current implementation has issues...
...and these issues stem from the use of completeSmart(). Whenever you use completeSmart(), the template ignores everything after that call and ends editing.
For example, If I moved the CAST named-variable up one slot (above ID), then the cast would be automatically filled out and it would not let me easily edit ID anymore. Same as below picture, but without btnAddition. Just ...findViewById(R.id.);
With my current setup, I have CAST on the very bottom so I can easily edit all of the named-variables; however, the use of completeSmart() does not let the template end (Place the cursor by the $END$ marker - Line 29 in the picture below) when I am complete.
Instead, it places the cursor right after the cast, like so...
...when it should be placed on the start of the next line. This template does put a new line in, but the cursor does not go there at the end. Why?
So I want this...
1 ) Since I already but in Button, I want to cast to be Button. I should not have to type in in twice!
you should use method typeOfVariable()
you can change like this:
Related
I'm trying to modify the AndroidLog live templates in Android Studio 2.2.
The default logd template expands to
android.util.Log.d(TAG, "$METHOD_NAME$: $content$");
But usually, when I am logging, I want to log the variable values and I want auto-completion to help me with it. So I modified this template to the following:
android.util.Log.d(TAG, "$METHOD_NAME$: $content$" + $content$);
However, when I expand this, the cursor still lands on the first occurrence of $content$. I want it to land on the second occurrence so Android Studio can suggest the variable name via auto completion!
For example, if I want to log the user's login time from an instance of User, the log statement would something like this
Log.d(TAG, "isSessionExpired: user.getLoginTime():" + user.getLoginTime());
So I want the cursor to be after the plus sign for auto completion to help me.
How can I achieve this with Live Templates?
Please see how soutv live template is defined, you want something similar for your logging:
In Eclipse, I can put my cursor on a method name and hit ctrl-space and I get a list of available template proposals.
In Android Studio, when I'm typing a method name and open paren, I get a list of template proposals, but afterwards, how do I see the list again? I've already got show quick doc on mouse move, but that only shows the method signature I selected at first. I want to see alternatives to what I selected the first time through.
How do I show alternative method signatures, aka alternative template proposals?
EDIT:
Thanks to AndroidMechanic for answering Ctrl-P.
Here is what it looks like in Android Studio:
Here is what it looks like in Eclipse: . Notice that you're presented with the documentation in Eclipse, which I find helpful. But Ctrl-P gets me most of the way there.
In android studio to see all method overloads click inside the parenthesis and hit Ctrl + P. This will show you the different combinations you could pass as parameters to a method.
Is there a way to make android studio perform automatic type casting for objects without the need to do that manually?
I don't think there's a way to do this automatically and IMHO this would be not that useful. There might be some cases where you don't want a cast (or a cast different to the one Android Studio suggests). If it would be done automatically you might not notice it it could lead to strange (i.e. unexpected) behaviour which is quite difficult to detect later.
What Android Studio can do for you: It can give you a warning and suggest a cast. For example, when you do a findViewById() call and want to assign the result to a View object you do something like this:
Button btn = findViewById(R.id.button);
Android Studio will highlight the line because you need to cast the returned View object to a Button.
By moving the cursor to the line and pressing Alt+Enter you can select the option "Add cast.." and it will insert the cast for you:
Button btn = (Button) findViewById(R.id.button);
My requirement is to put in a place name in a text field and show that in the map, so i used geocomplete js, which works well.
Now my user should be able to put in user defined places like 'my house', for that I need to remove the geocomplete on clicking a 'x' button on top of the map.
How can I implement this?
Thanks in advance
I wouldn't customize the package! When a new version comes out, you'll have to make the same changes you before.
Since you haven't provided any code, I can give you an idea of what I've done with JQuery validation method overrides.
You'll simply have to find the listener (something like $('#listenToThis').on('click', function(){ doThings(); }) in the geocomplete.js file, then override it in a file that is included after geocomplete.
If you're using bundles, just include your file after the geocomplete listener response is defined.
So, after you find those, you can do something similar to the following:
$.validator.methods.number = function (value, element) {
value = floatValue(value);
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}
The function above allowed me to validate client-side numbers that were formatted as currency in JQuery ($).
This overrides the JQuery.validator.methods.number function (a cheating way to override the function without changing the package code), so if you can pinpoint the geocomplete.addSomethingToMap or geocomplete.reactToClick function, you should override it and it will essentially work that way.
Warning, though: you will need to reinclude the changes when you want to reenable the feature. You'll have to override, override, override again. This may not be the best way if they're going to be adding hundreds of different new locations on one screen, but for up to a small unit, such as a dozen, it should be a good solution.
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.